Thymeleaf + BootStrap分页

 <div class="panel">
        <div class="panel-body">
            <div th:if="${#lists.isEmpty(pagination.data)}">
                <h4>搜索条件:&nbsp;<strong th:text="${title}" class="text-info"></strong>&nbsp;暂无匹配的结果。</h4>
            </div>
            <div class="media" th:each="article:${pagination.data}">
                <div class="media-body">
                    <div class="media-body">
                        <h4 class="media-heading">
                            <a th:href="@{'/article/detail/'+ ${article.id}}" th:utext="${article.title}"></a>
                        </h4>
                        <p class="text-desc">
                            <span th:text="${article.commentCount}"></span> 次回复 • &nbsp;
                            发表时间: &nbsp;<span th:text=" ${#dates.format(article.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
                        </p>
                    </div>
                </div>
            </div>
            <nav aria-label="Page navigation" th:if="${!#lists.isEmpty(pagination.data)}">
                <ul class="pagination">
                    <!-- 首页 -->
                    <li th:if="${!pagination.isFirstPage()}">
                        <a th:href="@{/search(page=1, title=${title})}" aria-label="Previous" class="page-item">
                            <span aria-hidden="true">首页</span>
                        </a>
                    </li>
                    <!-- 上一页 -->
                    <li th:if="${pagination.hasPrevious}">
                        <a th:href="@{/search(page=${pagination.currentPage - 1}, title=${title})}" class="page-item"
                           aria-label="Previous">
                            <span aria-hidden="true">上一页</span>
                        </a>
                    </li>
                    <!-- 遍历页码范围 -->
                    <li th:each="page : ${pagination.pageNums}"
                        th:class="${page == pagination.currentPage} ? 'active':'' ">
                        <a th:href="@{/search(page=${page}, title=${title})}" th:text="${page}" class="page-item"></a>
                    </li>
                    <!-- 下一页 -->
                    <li th:if="${pagination.hasNext}">
                        <a th:href="@{/search(page=${pagination.currentPage + 1 }, title=${title})}" class="page-item"
                           aria-label="Previous">
                            <span aria-hidden="true">下一页</span>
                        </a>
                    </li>
                    <!-- 尾页 -->
                    <li th:if="${!pagination.isLastPage()}">
                        <a th:href="@{/search(page=${pagination.totalPage}, title=${title})}" class="page-item"
                           aria-label="Previous">
                            <span aria-hidden="true">尾页</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ThymeleafBootstrap都是常用的前端技术,可以结合使用实现分页功能。下面是一种实现ThymeleafBootstrap分页的方法: 1. 首先,在你的Thymeleaf模板中引入Bootstrap的样式和脚本文件。可以通过CDN引入,也可以下载到本地引入。 2. 在你的Controller中,准备好分页所需的数据。你可以使用Spring Data JPA或其他后端框架来查询数据库并获取数据列表。 3. 在Controller中,计算总页数和当前页码,并将它们传递给Thymeleaf模板。 4. 在Thymeleaf模板中,使用Bootstrap分页组件来显示分页导航栏。你可以使用`th:each`循环来遍历页码,并设置`th:href`属性来生成分页链接。 以下是一个简单的示例: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> <head> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"> </head> <body> <div class="container"> <h1>分页示例</h1> <table class="table"> <!-- 显示数据列表 --> <!-- ... --> </table> <nav aria-label="Page navigation"> <ul class="pagination"> <!-- 显示页码 --> <li class="page-item" th:classappend="${pageNumber == 1} ? disabled"> <a class="page-link" th:href="@{'?page=' + (${pageNumber} - 1)}">Previous</a> </li> <li class="page-item" th:classappend="${pageNumber == totalPages} ? disabled"> <a class="page-link" th:href="@{'?page=' + (${pageNumber} + 1)}">Next</a> </li> </ul> </nav> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html> ``` 请注意,上述示例中的链接地址是简化的,你需要根据你的实际情况进行相应的修改。此外,你还需要在Controller中获取当前页码和总页数,并将它们传递给Thymeleaf模板。 希望对你有所帮助!如果你有任何其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值