Javaweb使用thymeleaf局部刷新結(jié)合Layui插件實(shí)現(xiàn)Html分頁
1、前言
最近個人在做開發(fā)的時候,需要實(shí)現(xiàn)前端的Html頁面分頁。由于好一段時間沒寫前端的代碼了,有些生疏了?,F(xiàn)就實(shí)踐成果,做下記錄與分享!
2、正文
2.1 開發(fā)環(huán)境介紹
后端:SpringBoot、Thymeleaf
前端:Html、Jquery、Layui插件
2.2 實(shí)現(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%">景點(diǎn)名稱</th>
<th width="10%">圖片</th>
<th>景點(diǎn)類型</th>
<th>門票價格</th>
<th>景點(diǎn)負(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">收費(fèi)</span>
<span th:case="2">免費(fèi)</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實(shí)例
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 實(shí)現(xiàn)流程說明
通過Layui的分頁插件代碼,點(diǎn)擊上下頁的時候,調(diào)用上面JS中的代碼。并獲取Layui當(dāng)前的分頁的參數(shù),請求后端列表接口。然后通過thymeleaf的
th:fragment="refreshList"
將后端返回的數(shù)據(jù),局部刷新到Html指定元素中。。。從而實(shí)現(xiàn)局部刷新的分頁實(shí)現(xiàn)?。?!
2.4 實(shí)現(xiàn)效果
3、總結(jié)
到此這篇關(guān)于Javaweb使用thymeleaf局部刷新結(jié)合Layui插件實(shí)現(xiàn)Html分頁的文章就介紹到這了,更多相關(guān)Javaweb Html分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java圖形化編程之JFrame疫苗接種系統(tǒng)詳解
GUI圖形界面設(shè)計是用戶和程序交互的工具,用戶通過圖形界面控制程序事件的發(fā)生。首先介紹Swing的基本體系結(jié)構(gòu),這是底層2021-09-09
spring-kafka使消費(fèi)者動態(tài)訂閱新增的topic問題
這篇文章主要介紹了spring-kafka使消費(fèi)者動態(tài)訂閱新增的topic問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
jdk8?FunctionalInterface注解源碼解讀
這篇文章主要介紹了jdk8?FunctionalInterface注解源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Java守護(hù)線程實(shí)例詳解_動力節(jié)點(diǎn)Java學(xué)院整理
在Java中有兩類線程:User Thread(用戶線程)、Daemon Thread(守護(hù)線程) 。下面通過本文給大家分享java守護(hù)線程實(shí)例詳解,需要的朋友參考下吧2017-06-06
Java微服務(wù)分布式調(diào)度Elastic-job環(huán)境搭建及配置
Elastic-Job在配置中提供了JobEventConfiguration,支持?jǐn)?shù)據(jù)庫方式配置,會在數(shù)據(jù)庫中自動創(chuàng)建JOB_EXECUTION_LOG和JOB_STATUS_TRACE_LOG兩張表以及若干索引,來記錄作業(yè)的相關(guān)信息2023-02-02
SpringBoot集成JPA持久層框架,簡化數(shù)據(jù)庫操作
JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化規(guī)范。主要是為了簡化持久層開發(fā)以及整合ORM技術(shù),結(jié)束Hibernate、TopLink、JDO等ORM框架各自為營的局面。JPA是在吸收現(xiàn)有ORM框架的基礎(chǔ)上發(fā)展而來,易于使用,伸縮性強(qiáng)。2021-06-06

