Skip to content

Instantly share code, notes, and snippets.

@jpomykala
Last active March 3, 2021 11:09
Show Gist options
  • Save jpomykala/e8e6a9cffad786cdd61cb2ab93cfa4bc to your computer and use it in GitHub Desktop.
Save jpomykala/e8e6a9cffad786cdd61cb2ab93cfa4bc to your computer and use it in GitHub Desktop.
Thymeleaf Pagination: page -> Spring Data Page // mapping -> path to list eg '/admin/log/list' // buttonPages -> max pages to show *2
<!-- usage -->
<div th:insert="template :: pagination(${logsList}, ${'/admin/log/list'}, ${100})"></div>
<!-- pagger -->
<div th:fragment="pagination(page, mapping, buttonPages)">
<div class="dataTables_paginate paging_simple_numbers">
<ul class="pagination">
<li class="paginate_button previous"
th:classappend="${page.hasPrevious()} ? '' : 'disabled'">
<a th:if="${page.hasPrevious()}" th:href="@{${mapping}(page=${page.getNumber()-1})}">Previous</a>
<a th:if="${!page.hasPrevious()}">Previous</a>
</li>
<li class="paginate_button"
th:if="${pageNumber > -1 && pageNumber < page.getTotalPages()}"
th:classappend="${pageNumber == page.getNumber()} ? 'active' : ''"
th:each="pageNumber : ${#numbers.sequence(page.getNumber()-buttonPages, page.getNumber()+buttonPages)}">
<a th:href="@{${mapping}(page=${pageNumber})}">[[${pageNumber}+1]]</a>
</li>
<li class="paginate_button next"
th:classappend="${page.hasNext()} ? '' : 'disabled'">
<a th:if="${page.hasNext()}" th:href="@{${mapping}(page=${page.getNumber()+1})}">Next</a>
<a th:if="${!page.hasNext()}">Next</a>
</li>
</ul>
</div>
</div>
<!-- Java method -->
@RequestMapping(value = "/admin/log/list")
public String showLogs(Model model, @RequestParam(required = false, defaultValue = "0") int page) {
Page<Log> logs = logService.pullLogs(new PageRequest(page, 15));
model.addAttribute("logsList", logs);
return "admin/log/list";
}
@jpomykala
Copy link
Author

Thanks for your contribution! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment