基于LayUI分頁(yè)和LayUI laypage分頁(yè)的使用示例
本文介紹了LayUI分頁(yè),LayUI動(dòng)態(tài)分頁(yè),LayUI laypage分頁(yè),LayUI laypage刷新當(dāng)前頁(yè),分享給大家,具體如下:
效果圖:
一、引用js依賴
主要是jquery-1.11.3.min.js 和 layui.all.js , json2.js用來(lái)做json對(duì)象轉(zhuǎn)換的
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/plugin/layui/lay/dest/layui.all.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/js/json2.js"></script>
二、js分頁(yè)方法封裝(分頁(yè)使用模板laytpl)
1、模板渲染
/** * 分頁(yè)模板的渲染方法 * @param templateId 分頁(yè)需要渲染的模板的id * @param resultContentId 模板渲染后顯示在頁(yè)面的內(nèi)容的容器id * @param data 服務(wù)器返回的json對(duì)象 */ function renderTemplate(templateId, resultContentId, data){ layuiuse(['form','laytpl'], function(){ var laytpl = layui.laytpl; laytpl($("#"+templateId).html()).render(data, function(html){ $("#"+resultContentId).html(html); }); }); layui.form().render();// 渲染 };
2、layui.laypage 分頁(yè)封裝
/** * layuilaypage 分頁(yè)封裝 * @param laypageDivId 分頁(yè)控件Div層的id * @param pageParams 分頁(yè)的參數(shù) * @param templateId 分頁(yè)需要渲染的模板的id * @param resultContentId 模板渲染后顯示在頁(yè)面的內(nèi)容的容器id * @param url 向服務(wù)器請(qǐng)求分頁(yè)的url鏈接地址 */ function renderPageData(laypageDivId, pageParams, templateId, resultContentId, url){ if(isNull(pageParams)){ pageParams = { pageIndex : 1, pageSize : 10 } } $ajax({ url : url,//basePath + '/sysMenu/pageSysMenu', method : 'post', data : pageParams,//JSON.stringify(datasub) async : true, complete : function (XHR, TS){}, error : function(XMLHttpRequest, textStatus, errorThrown) { if("error"==textStatus){ error("服務(wù)器未響應(yīng),請(qǐng)稍候再試"); }else{ error("操作失敗,textStatus="+textStatus); } }, success : function(data) { var jsonObj; if('object' == typeof data){ jsonObj = data; }else{ jsonObj = JSON.parse(data); } renderTemplate(templateId, resultContentId, jsonObj); //重新初始化分頁(yè)插件 layui.use(['form','laypage'], function(){ laypage = layui.laypage; laypage({ cont : laypageDivId, curr : jsonObj.pager.pageIndex, pages : jsonObj.pager.totalPage, skip : true, jump: function(obj, first){//obj是一個(gè)object類型。包括了分頁(yè)的所有配置信息。first一個(gè)Boolean類,檢測(cè)頁(yè)面是否初始加載。非常有用,可避免無(wú)限刷新。 pageParams.pageIndex = obj.curr; pageParams.pageSize = jsonObj.pager.pageSize; if(!first){ renderPageData(laypageDivId, pageParams, templateId, resultContentId, url); } } }); }); } }); };
3、刷新當(dāng)前分頁(yè)的方法,可省略
/** * 分頁(yè)插件刷新當(dāng)前頁(yè)的數(shù)據(jù),必須有跳轉(zhuǎn)的確定按鈕,因?yàn)楦鶕?jù)按鈕點(diǎn)擊事件刷新 */ function reloadCurrentPage(){ $(".layui-laypage-btn").click(); };
三、頁(yè)面代碼
1、分頁(yè)表格及分頁(yè)控件
<!-- 分頁(yè)表格 --> <div class="layui-form"> <table class="layui-table"> <thead> <tr> <th class="w20"><input type="checkbox" name="checkBoxAll" lay-skin="primary" lay-filter="allChoose"></th> <th class="w200">許可名稱</th> <th class="w200">許可編碼</th> <th class="w200">菜單名稱</th> <th>許可鏈接</th> </tr> </thead> <tbody id="page_template_body_id"> </tbody> </table> </div> <!-- 分頁(yè)控件div --> <div id="imovie-page-div"></div>
2、分頁(yè)模板
<script id="page_template_id" type="text/html"> {{# layui.each(d.list, function(index, item){ }} <tr> <td><input type="checkbox" name="permissionId" lay-skin="primary" value="{{item.permissionId}}"></td> <td>{{item.permissionName || ''}}</td> <td>{{item.permissionCode || ''}}</td> <td>{{item.menuName || ''}}</td> <td>{{item.permissionUrl || ''}}</td> </tr> {{# }); }} </script>
3、分頁(yè)執(zhí)行代碼:
分頁(yè)參數(shù):
function getPageParams(){ var pageParams = { pageIndex : 1, pageSize : 2 }; pageParams.permissionName = $("input[name='permissionName']").val(); pageParams.permissionCode = $("input[name='permissionCode']").val(); pageParams.menuName = $("input[name='menuName']").val(); return pageParams; };
分頁(yè)執(zhí)行方法:
function initPage(){ renderPageData("imovie-page-div", getPageParams(), "page_template_id", "page_template_body_id", basePath + '/sysPermission/pageSysPermission'); };
頁(yè)面加載初始化分頁(yè):
$(function(){ initPage(); });
如果包括上面效果圖的查詢,如下:
Html頁(yè)面代碼
<div> <form class="layui-form layui-form-pane"> <div class="layui-form-item"> <div class="layui-inline"> <label class="layui-form-label">許可名稱</label> <div class="layui-input-inline"> <input type="text" name="permissionName" autocomplete="off" class="layui-input" placeholder="請(qǐng)輸入許可名稱" > </div> </div> <div class="layui-inline"> <label class="layui-form-label">許可編碼</label> <div class="layui-input-inline"> <input type="text" name="permissionCode" autocomplete="off" placeholder="請(qǐng)輸入許可編碼" class="layui-input"> </div> </div> <div class="layui-inline"> <label class="layui-form-label">菜單名稱</label> <div class="layui-input-inline layui-input-inline-0"> <input type="text" name="menuName" autocomplete="off" placeholder="請(qǐng)選擇菜單名稱" class="layui-input"> </div> </div> <div class="layui-inline"> <button id="btnSubmit" class="layui-btn" lay-submit="" lay-filter="formFilter">查詢</button> </div> </div> </form> </div>
查詢語(yǔ)句:
$(function(){ initPage(); layui.use(['form'], function(){ var form = layui.form(); //監(jiān)聽(tīng)提交 formon('submit(formFilter)', function(data){ initPage(); return false; }); }); });
四、懂 jquery 插件封裝的大神可以將其封裝成獨(dú)立的分頁(yè)插件,這樣更加容易使用。我表示不太懂,^_^
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery中使用了document和window哪些屬性和方法小結(jié)
未列出常見(jiàn)的比如document.getElementById(),object.addEventListener()等。2011-09-09JQuery實(shí)現(xiàn)帶排序功能的權(quán)限選擇實(shí)例
這篇文章主要介紹了JQuery帶排序功能的權(quán)限選擇,涉及jQuery鼠標(biāo)事件及json數(shù)據(jù)處理的相關(guān)技巧,需要的朋友可以參考下2015-05-05jQuery在IE下使用未閉合的xml代碼創(chuàng)建元素時(shí)的Bug介紹
這個(gè)偏門Bug是我在更新phZoom 1.29后發(fā)現(xiàn)的, 我先將之重現(xiàn)一下2012-01-01jquery實(shí)現(xiàn)圖片燈箱明暗的遮罩效果
這篇文章主要介紹了jquery實(shí)現(xiàn)圖片燈箱明暗的遮罩效果,有需要的朋友可以參考一下2013-11-11jQuery實(shí)現(xiàn)基本隱藏與顯示效果的方法詳解
這篇文章主要介紹了jQuery實(shí)現(xiàn)基本隱藏與顯示效果的方法,結(jié)合實(shí)例形式詳細(xì)分析了jQuery中hide()、show()及toggle()等方法用于實(shí)現(xiàn)顯示/隱藏效果的相關(guān)操作技巧,需要的朋友可以參考下2018-09-09分享一些常用的jQuery動(dòng)畫事件和動(dòng)畫函數(shù)
在jquery中,jquery動(dòng)畫事件和動(dòng)畫函數(shù)經(jīng)常用的到,今天小編抽時(shí)間給大家整理了些關(guān)于常用的jquery動(dòng)畫事件和動(dòng)畫函數(shù),對(duì)jquery動(dòng)畫函數(shù)和動(dòng)畫事件相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-11-11Js與Jq 獲取頁(yè)面元素值的方法和差異對(duì)比
這篇文章主要介紹了原生js獲取瀏覽器和對(duì)象寬高與jquery獲取瀏覽器和對(duì)象寬高的方法關(guān)系對(duì)比,十分實(shí)用,需要的朋友可以參考下2015-04-04jquery.tableSort.js表格排序插件使用方法詳解
這篇文章主要為大家詳細(xì)介紹了jquery.tableSort.js表格排序插件使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02