实现百度搜索使用的前五后四原则,效果如下。

 

 下面贴出代码,复制到前端即可,只需要域中放置page对象就可以。(springdatajpa自带的page 注意:第一页是按0开始算的)

                  <div class="modal-footer no-margin-top">
                                <div class="pull-left">
                                    <div class="form-group form-inline">
                                        总共<span th:text="${page.totalPages}"></span> 页,共<span th:text="${page.totalElements}"></span>  条数据。
                                    </div>
                                </div>
                                <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.pageable.getPageNumber()+3 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 th:if="${page.totalPages gt 10 && page.pageable.getPageNumber() gt 4 && page.pageable.getPageNumber()+4 gt page.totalPages}" 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 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>
                            </div>

版权声明:本文为super-hu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/super-hu/p/12189681.html