Javaweb使用thymeleaf局部刷新結(jié)合Layui插件實現(xiàn)Html分頁
1、前言
最近個人在做開發(fā)的時候,需要實現(xiàn)前端的Html頁面分頁。由于好一段時間沒寫前端的代碼了,有些生疏了?,F(xiàn)就實踐成果,做下記錄與分享!
2、正文
2.1 開發(fā)環(huán)境介紹
后端:SpringBoot、Thymeleaf
前端:Html、Jquery、Layui插件
2.2 實現(xiàn)代碼
html頁面代碼:
<html lang="zh-cn" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro"> ... <table class="table table-hover text-center" id="refreshList" th:fragment="refreshList"> <tr> <th width="100" style="text-align:left; padding-left:20px;">ID</th> <th width="10%">景點名稱</th> <th width="10%">圖片</th> <th>景點類型</th> <th>門票價格</th> <th>景點負(fù)責(zé)人</th> <th width="10%">創(chuàng)建時間</th> <th width="20%">操作</th> </tr> <tr th:each="view : ${viewList}" > <td style="text-align:left; padding-left:20px;"><input type="checkbox" name="id" value="" /></td> <td th:text="${view.viewTitle}"></td> <td ><img th:src="${'/upload/img/'+view.pictureUrl}" alt="" width="100" height="70" /></td> <td th:switch="${view.type}"> <span th:case="1">收費</span> <span th:case="2">免費</span> </td> <td th:text="${view.price == null or view.price == '' ? '暫無' : view.price}" ></td> <td th:text="${view.manager}"></td> <td th:text="${#dates.format(view.createTime,'yyyy-MM-dd HH:mm:ss')}"></td> <td><div class="button-group"> <a class="button border-main" th:href="${'/view/edit.do?viewId='+view.id}" rel="external nofollow" ><span class="icon-edit"></span> 修改</a> <a class="button border-red" href="javascript:void(0)" rel="external nofollow" th:onclick="del([[${view.id}]])"><span class="icon-trash-o"></span> 刪除</a> </div></td> </tr> </table>
Js代碼:
<script src="/js/jquery.js"></script> <script type="text/javascript" src="/layui/layui.js"></script> <script type="text/javascript" src="/layui/layui.all.js"></script> ... //分頁 layui.use('laypage', function () { var laypage = layui.laypage; var total = 0; var limit = 6; //獲取列表總數(shù)量 $.ajax({ url: '/view/count.do', type: 'POST', dataType: 'json', async: false, success: function (data) { if(data != null){ total = data; } } }); //執(zhí)行一個laypage實例 laypage.render({ elem: 'pageDiv', //注意,這里的 pageDiv 是 ID,不用加 # 號 count: total, //數(shù)據(jù)總數(shù),從服務(wù)端得到 limit: limit,//頁面展示數(shù)據(jù)條數(shù) theme: '33ccff',//主題樣式 jump: function (obj, first) { if (!first) { $.ajax({ url: '/view/list.do', type: 'POST', data: {'pageSize': obj.limit, 'pageIndex': obj.curr}, success: function (data) { if (data != null) { $("#refreshList").html(data); } } }); } } }); });
后端接口:
@PostMapping("/list.do") public String getList(PageBean pageBean, Model model){ if(Objects.isNull(pageBean)) pageBean = new PageBean(); pageBean.setPageIndex((pageBean.getPageIndex()-1)*pageBean.getPageSize()); List<View> viewList = viewService.getList(pageBean); model.addAttribute("viewList",viewList); //viewList是html頁面名稱,refreshList是html頁面內(nèi)定義的元素名稱,在html頁面內(nèi)可以看到 return "viewList::refreshList"; }
這里說明一下,初次進(jìn)入頁面的時候,我這邊使用的是另外一個GET類型的請求獲取的數(shù)據(jù),跟上面的POST請求接口幾乎一樣。
2.3 實現(xiàn)流程說明
通過Layui的分頁插件代碼,點擊上下頁的時候,調(diào)用上面JS中的代碼。并獲取Layui當(dāng)前的分頁的參數(shù),請求后端列表接口。然后通過thymeleaf的
th:fragment="refreshList"
將后端返回的數(shù)據(jù),局部刷新到Html指定元素中。。。從而實現(xiàn)局部刷新的分頁實現(xiàn)!??!
2.4 實現(xiàn)效果
3、總結(jié)
到此這篇關(guān)于Javaweb使用thymeleaf局部刷新結(jié)合Layui插件實現(xiàn)Html分頁的文章就介紹到這了,更多相關(guān)Javaweb Html分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC自定義攔截器登錄檢測功能的實現(xiàn)代碼
這篇文章主要介紹了SpringMVC自定義攔截器登錄檢測功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08SpringBoot使用MockMvc進(jìn)行Web集成測試的示例詳解
MockMvc?是一個測試框架,可以模擬?HTTP?請求和響應(yīng),在本文中,我們將介紹如何使用MockMvc進(jìn)行Web集成測試,以及如何編寫測試用例來測試Spring?MVC控制器,希望對大家有所幫助2023-06-06