Skip to content Skip to sidebar Skip to footer

Spring + Thymeleaf Pagination

I try to build simple forum and i have problem with pagination in front end. I build page where got all my topics, for example there is 50, so choose 10 topics for every page so sh

Solution 1:

I had the same problem and here is what I did to solve it. I pass to my template an array with the length equals to the number of pages + the current page to be displayed:

model.addAttribute("pages", newint[pageAllGuests.getTotalPages()]);
model.addAttribute("currentPage", page);

then on my HTML page I make some tests to display or not the link to the page:

<ul class="pager">
                    <span th:unless="${currentPage == 0}">
                        <li><a title="First Page"
                            th:href="@{admin(status=${status}, jobdescription=${jobdescription},page=0,keyword=${keyword})}"
                            >&laquo;</a>
                        </li>
                        <li>
                            <a title="Previous Page"
                            th:href="@{admin(status=${status}, jobdescription=${jobdescription},page=${currentPage-1},keyword=${keyword})}"
                            >&lsaquo;</a>
                        </li>
                        <li th:if="${currentPage>3}">
                            <a title="Previous Pages" 
                            th:href="@{admin(status=${status}, jobdescription=${jobdescription},page=${currentPage-3},keyword=${keyword})}"
                            >...</a>
                        </li>
                    </span>
                    <li th:each="page,i:${pages}" th:class="${i.index==currentPage?'active':''}"  th:if="${i.index > currentPage-4 AND i.index < currentPage+4}" >
                        <a 
                        th:href="@{admin(status=${status}, jobdescription=${jobdescription},page=${i.index},keyword=${keyword})}"
                        th:text="${i.index+1}"> </a>
                    </li>
                    
                    <span th:unless="${currentPage == pages.length-1 || pages.length == 0}" >
                        <li th:if="${currentPage+4<pages.length}">
                            <a title="Next Pages" 
                            th:href="@{admin(status=${status}, jobdescription=${jobdescription},page=${currentPage+5},keyword=${keyword})}"
                            >...</a>
                        </li>
                        <li><a title="Next Page"
                            th:href="@{admin(status=${status}, jobdescription=${jobdescription},page=${currentPage+1},keyword=${keyword})}"
                            >&rsaquo;</a>
                        </li>
                        <li><a 
                            th:href="@{admin(status=${status}, jobdescription=${jobdescription},page=${pages.length-1},keyword=${keyword})}"
                            title="Last Page">&raquo;</a>
                        </li>
                    </span>
                </ul>

below is a picture of the result

pagination

This code may probably be improved but at least it is easy to read and understand. You will need to clean the code (parameters in href) and adapt it to your case.

Post a Comment for "Spring + Thymeleaf Pagination"