在页面上只显示最多5个页码:
主要思路其实就是分情况:
1. 总页数小于5页时
2. 总页数大于5页时,且当前页距离首页距离小于2
3. 总页数大于5页时,且当前页距离末页距离小于2
4. 总页数大于5页时,且当前页距离首页距离和末页距离都大于2
<ul class="pagination"> <li th:class="${pageinfo.isHasPreviousPage()}?'' : 'disabled'"><a href="@{'/all?pageNum=' + ${pageinfo.getPageNum()-1}}">上一页</a></li> <li><a th:if="${pageinfo.getPages()<= 5}" th:href="@{'/all?pageNum=' + ${i}}" th:each="i :${pageinfo.getNavigatepageNums()}" th:text=" ${i}" th:class="${pageinfo.getPageNum() == i}? 'page-item active' :' ' "></a></li> <li><a th:if="${pageinfo.getPages()> 5 && (pageinfo.getPages() - pageinfo.getPageNum()) <= 2}" th:href="@{'/all?pageNum=' + ${i}}" th:each="i :${#numbers.sequence(pageinfo.getPages() - 4, pageinfo.getPages())}" th:text=" ${i}" th:class="${pageinfo.getPageNum() == i}? 'page-item active' :' ' "></a></li> <li><a th:if="${pageinfo.getPages()> 5 && (pageinfo.getPageNum() - 1) <= 2}" th:href="@{'/all?pageNum=' + ${i}}" th:each="i :${#numbers.sequence(1, 5)}" th:text=" ${i}" th:class="${pageinfo.getPageNum() == i}? 'page-item active' :' ' "></a></li> <li><a th:if="${pageinfo.getPages()> 5 && ((pageinfo.getPageNum() - 1) > 2)&& (pageinfo.getPages() - pageinfo.getPageNum()) > 2}" th:href="@{'/all?pageNum=' + ${i}}" th:each="i :${#numbers.sequence(pageinfo.getPageNum() - 2, pageinfo.getPageNum() + 2)}" th:text=" ${i}" th:class="${pageinfo.getPageNum() == i}? 'page-item active' :' ' "></a></li> <li th:class="${pageinfo.isHasNextPage()}?'' : 'disabled'"><a href="@{'/all?pageNum=' + ${pageinfo.getPageNum()+1}}">下一页</a></li> </ul>