Last active
March 3, 2021 11:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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"; | |
} |
Thanks for your contribution! 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Jakub, thank you for the simple and dynamic pager.
I also needed to have the first and last button. So, I edited it as below:
I hope it may help other people :)