layui實現(xiàn)數(shù)據(jù)分頁功能(ajax異步)
最近項目要使用layUI的分頁,看了官方demo感覺還是不太清晰,摸索了一下,現(xiàn)在記錄一下。
一、引入layUI的相關(guān)資源
<link rel="stylesheet" href="${ctxPath}/vendor/layui-v2.4.5/layui/css/layui.css" >
<script src="${ctxPath}/vendor/layui-v2.4.5/layui/layui.js"></script>
<script src="${ctxPath}/vendor/jquery.min.js"></script>//引入jquery包
二、html頁面代碼
<div class="layui-main g-main">
<div class="layui-row">
<div class="layui-col-xs12">
<blockquote class="layui-elem-quote">
當(dāng)前系統(tǒng)排名:<span class="layui-badge-rim badge-number">第${scoreRecordUtil.rank}名</span>,
總積分:<span class="layui-badge-rim badge-number">${scoreRecordUtil.totalScore}分</span>,
和上一名相差:<span class="layui-badge-rim badge-number">${scoreRecordUtil.differenceTotal}分</span>,繼續(xù)加油!
</blockquote>
<table class="layui-table">
<thead>
<tr>
<th>積分類型</th>
<th>積分原因</th>
<th>積分值</th>
<th>創(chuàng)建時間</th>
</tr>
</thead>
<tbody>
//存放分頁加載數(shù)據(jù)
</tbody>
</table>
<div class="page"></div>
</div>
</div>
</div>
三、定義showReocrd()函數(shù)異步加載數(shù)據(jù)
function showReocrd(pageNo,pageSize){
$.get("${ctxPath}/score-record/showRecord",
{
pageNo:pageNo,
pageSize:pageSize
},
function (date) {
//加載后臺返回的List集合數(shù)據(jù)
for (var i = 0; i < date.length; i++) {
var td = $("<td></td>").text(date[i].typeName);
var td2 = $("<td></td>").text(date[i].operate);
var td3 = $("<td></td>").text(date[i].score);
var td4 = $("<td></td>").text(date[i].createTime);
var tr = $("<tr></tr>").append(td, td2, td3, td4);
$('tbody').append(tr);
}
},
"json"
);
}
四、分頁js
主要注意下:
count: total 代表總的數(shù)據(jù)量,
limit 代表每頁行數(shù),
curr 代表起始頁,但jump函數(shù)中的obj.curr取的是當(dāng)前頁數(shù)
jump 方法中obj參數(shù)可以取到上面的屬性和方法
first 為是否首次加載
//加載總頁數(shù)
var total = "${scoreRecordUtil.totalRecord}";
//先初始化加載首頁,十條數(shù)據(jù)
showReocrd(1,10);
layui.use(['laypage','jquery'], function() {
var laypage = layui.laypage,$ = layui.$;
$(".page").each(function(i,the){
laypage.render({
elem: the //注意,這里的 test1 是 ID,不用加 # 號
,count: total //數(shù)據(jù)總數(shù),從服務(wù)端得到
, limit: 10 //每頁顯示條數(shù)
, limits: [10, 20, 30]
, curr: 1 //起始頁
, groups: 5 //連續(xù)頁碼個數(shù)
, prev: '上一頁' //上一頁文本
, netx: '下一頁' //下一頁文本
, first: 1 //首頁文本
, last: 100 //尾頁文本
, layout: ['prev', 'page', 'next','limit','refresh','skip']
//跳轉(zhuǎn)頁碼時調(diào)用
, jump: function (obj, first) { //obj為當(dāng)前頁的屬性和方法,第一次加載first為true
//非首次加載 do something
if (!first) {
//清空以前加載的數(shù)據(jù)
$('tbody').empty();
//調(diào)用加載函數(shù)加載數(shù)據(jù)
showReocrd(obj.curr,obj.limit);
}
}
});
})
})
推薦:使用layui的table組件自帶分頁功能(異步,含條件查詢)點這里
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
教你巧用?import.meta?實現(xiàn)熱更新的問題
import.meta?是一個給?JavaScript?模塊暴露特定上下文的元數(shù)據(jù)屬性的對象,它包含了這個模塊的信息,這篇文章主要介紹了巧用?import.meta?實現(xiàn)熱更新的問題,需要的朋友可以參考下2022-04-04
JavaScript+css+HTML實現(xiàn)移動端輪播圖(含源碼)
這篇文章主要介紹了JavaScript+css+HTML實現(xiàn)移動端輪播圖并含源碼的分享,需要的小伙伴可以參考一下,希望對你有所幫助2022-01-01
JSON+HTML實現(xiàn)國家省市聯(lián)動選擇效果
實現(xiàn)國家省市聯(lián)動的方法有很多,本文要為大家介紹的JSON+HTML如何實現(xiàn),需要的朋友可以參考下2014-05-05
firefox事件處理之自動查找event的函數(shù)(用于onclick=foo())
在ie中,事件對象是作為一個全局變量來保存和維護的。 所有的瀏覽器事件,不管是用戶觸發(fā)的,還是其他事件, 都會更新window.event 對象。2010-08-08

