基于springboot+thymeleaf+springDataJpa自带的分页插件实现完整的动态分页
Posted super_胡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于springboot+thymeleaf+springDataJpa自带的分页插件实现完整的动态分页相关的知识,希望对你有一定的参考价值。
实现百度搜索使用的前五后四原则,效果如下。
下面贴出代码,复制到前端即可,只需要域中放置page对象就可以。(springdatajpa自带的page 注意:第一页是按0开始算的)
<div class="page">
<ul class="pagination">
<!--上一页数-->
<li class="page-item" th:if="${page.pageable.getPageNumber() != 0}">
<a th:onclick="pageRequest([[${page.pageable.getPageNumber()-1}]])" class="page-link"
data-th-attr="pageIndex=${page.number} - 1" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li class="page-item">
<a th:onclick="pageRequest(0)" class="page-link" >首页</a>
</li>
<!--前五后四分页实现-->
<!--总页数<10-->
<li th:if="${page.totalPages lt 11}" class="page-item" data-th-each="i : ${#numbers.sequence(0, page.totalPages)}"
data-th-classappend="${(page.number ) eq i} ? \'active\' : \'\'" >
<a class="page-link" th:onclick="pageRequest([[${i}]])">
<span data-th-text="${i+1}"></span>
</a>
</li>
<!--总页数>10-->
<li th:if="${page.totalPages gt 10 && page.pageable.getPageNumber() lt 5}" class="page-item" data-th-each="i : ${#numbers.sequence(0, 9)}"
data-th-classappend="${(page.number ) eq i} ? \'active\' : \'\'" >
<a class="page-link" th:onclick="pageRequest([[${i}]])">
<span data-th-text="${i+1}"></span>
</a>
</li>
<li th:if="${page.totalPages gt 10 && page.pageable.getPageNumber() gt 4 && page.totalPages lt page.pageable.getPageNumber()+5 }"
class="page-item" data-th-each="i : ${#numbers.sequence(page.totalPages-10, page.totalPages-1)}"
data-th-classappend="${(page.number ) eq i} ? \'active\' : \'\'" >
<a class="page-link" th:onclick="pageRequest([[${i}]])">
<span data-th-text="${i+1}"></span>
</a>
</li>
<li th:if="${page.totalPages gt 10 && page.pageable.getPageNumber() gt 4 && page.pageable.getPageNumber()+4 lt page.totalPages}"
class="page-item" data-th-each="i : ${#numbers.sequence(page.pageable.getPageNumber()-5, page.pageable.getPageNumber()+4)}"
data-th-classappend="${(page.number ) eq i} ? \'active\' : \'\'" >
<a class="page-link" th:onclick="pageRequest([[${i}]])">
<span data-th-text="${i+1}"></span>
</a>
</li>
<!--尾页-->
<li class="page-item">
<a th:onclick="pageRequest([[${page.getTotalPages() - 1}]])" class="page-link" >尾页</a>
</li>
<!--下一页-->
<li class="page-item" th:if="${page.pageable.pageNumber} != ${page.totalPages}-1">
<a class="page-link" th:onclick="pageRequest([[${page.pageable.pageNumber}+1]])">
»
</a>
</li>
</ul>
</div>
以上是关于基于springboot+thymeleaf+springDataJpa自带的分页插件实现完整的动态分页的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot整合Thymeleaf-基于SpringBoot2.X版本
基于角色 Springboot+Thymeleaf 禁用/启用 Html 元素