layui2.0使用table+laypage實(shí)現(xiàn)真分頁
前言:最近項(xiàng)目上使用layui做前端頁面,發(fā)現(xiàn)layui的table本身的分頁不能根據(jù)屏幕生成每頁行數(shù),所以研究了下文檔,更改分頁
簡(jiǎn)單解釋:
1.最開始是根據(jù)屏幕計(jì)算加載的每頁行數(shù)
2.framework可以糊了,由于是老項(xiàng)目還使用了sea.js
3.getUrlParam()是用于其他頁面帶參數(shù)跳轉(zhuǎn)的也可以忽略
function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //構(gòu)造一個(gè)含有目標(biāo)參數(shù)的正則表達(dá)式對(duì)象 var r = window.location.search.substr(1).match(reg); //匹配目標(biāo)參數(shù) if (r != null) return decodeURI(r[2]); return null; //返回參數(shù)值 }
4.templet屬性用于對(duì)后端接收到的數(shù)據(jù)進(jìn)行格式化,其他屬性請(qǐng)自行查看官方文檔
5.laypage中的jump方法!first必須加,否則會(huì)無限調(diào)用接口,無法結(jié)束初始化
具體js代碼如下:
function getData(page) { var rows = Math.floor(($(window).height() - $('.topNav').height() - $('.content .noBorderB').height() - $('.content .searchD').height() - $('.content .commonTb tr:eq(0)').height() - 100) / 30); rows = rows <= 0 ? 1 : rows; framework.hideLoading(); var objectIdSearch = getUrlParam("objectId"); if(objectIdSearch==''||objectIdSearch==undefined){ objectIdSearch = $.trim($("#idSearch").val()); } var pages=1; var counts=1; layui.use('table', function(){ var table = layui.table; var laypage=layui.laypage; table.render({ elem: '#test' // , url: { ,url:'/app/client/user/audio/listp' ,method:"get" ,where: { pageNo : page, pageSize : rows, objectId : objectIdSearch , userId : $.trim($("#userIdSearch").val()), userName : $.trim($("#userNameSearch").val()), objectName : $.trim($("#audioNameSearch").val()), chapterName : $.trim($("#chapterNameSearch").val()), createTime : $.trim($("#createTime").val()) } ,response:{ statusName: 'page' //數(shù)據(jù)狀態(tài)的字段名稱,默認(rèn):code ,statusCode: page //成功的狀態(tài)碼,默認(rèn):0 ,countName: 'records' //數(shù)據(jù)總數(shù)的字段名稱,默認(rèn):count ,dataName: 'rows' //數(shù)據(jù)列表的字段名稱,默認(rèn):data } // } , cellMinWidth: 80 //全局定義常規(guī)單元格的最小寬度,layui 2.2.1 新增 , cols: [[ {field: 'id', title: 'ID', align: 'center', sort: true,width:80} , {field: 'audioBook',width:80, align: 'center', title: '有聲書ID', templet: function(d){ return d.audioBook.id }} //width 支持:數(shù)字、百分比和不填寫。你還可以通過 minWidth 參數(shù)局部定義當(dāng)前單元格的最小寬度,layui 2.2.1 新增 , {field: 'audioBook',width:124, align: 'center', title: '有聲書名稱', sort: true,templet: function(d){ return d.audioBook.name }} , {field: 'objectType',width:80, align: 'center', title: '類型' , templet: function(d){ if(d=='20'){ return "有聲書" }else{ return "課程" } }} , {field: 'chapter',width:80, align: 'center', sort: true,title: '章節(jié)ID', templet: function(d){ return d.chapter.id }} , {field: 'chapter.title',width:180, title: '章節(jié)名稱', align: 'center', templet: function(d){ return d.chapter.title }} //單元格內(nèi)容水平居中 , {field: 'payTimes',width:120, title: '購買次數(shù)', sort: true, align: 'center'} //單元格內(nèi)容水平居中 , {field: 'createTime',width:180, title: '購買時(shí)間', sort: true, align: 'center', templet: function(d){ return longTrans2Date(d.createTime) }} , {field: 'userInfo',width:80, title: '用戶ID', sort: true, align: 'center', templet: function(d){ return d.userInfo.userId }} , {field: 'userInfo',width:160, title: '用戶名稱', sort: true, align: 'center', templet: function(d){ return d.userInfo.userName }} ]] , done: function(res, curr, count){ pages=res.page; counts=res.records; //完整功能 laypage.render({ elem: 'demo7' ,count: counts ,curr: pages ,limit:rows ,layout: ['count', 'prev', 'page', 'next', 'refresh', 'skip'] ,jump: function(obj,first){ // getData(obj.curr) if(!first){ getData(obj.curr) } } }); } }); }) }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Bootstrap基本插件學(xué)習(xí)筆記之Alert警告框(20)
這篇文章主要為大家詳細(xì)介紹了Bootstrap基本插件學(xué)習(xí)筆記之ALert警告框的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12輕量級(jí)JS Cookie插件js-cookie的使用方法
js-cookie插件是一個(gè)JS操作cookie的插件,源文件只有3.34 KB,非常輕量級(jí),js-cookie也支持npm和Bower安裝和管理,下面看看js-cookie的具體用法2018-03-03深入學(xué)習(xí)js函數(shù)的隱式參數(shù) arguments 和 this
這篇文章主要介紹了 深入學(xué)習(xí)js函數(shù)的隱式參數(shù) arguments 和 this,arguments是一個(gè)類數(shù)組結(jié)構(gòu),它保存了調(diào)用時(shí)傳遞給函數(shù)的所有實(shí)參;this是函數(shù)執(zhí)行時(shí)的上下文對(duì)象, 這個(gè)對(duì)象有些讓人感到困惑的行為。 下面分別對(duì)他們進(jìn)行討論。,需要的朋友可以參考下2019-06-06ES6中Array.copyWithin()函數(shù)的用法實(shí)例詳解
ES6為Array增加了copyWithin函數(shù),用于操作當(dāng)前數(shù)組自身,用來把某些個(gè)位置的元素復(fù)制并覆蓋到其他位置上去。下面重點(diǎn)給大家介紹ES6中Array.copyWithin()函數(shù)的用法,需要的朋友參考下2017-09-09javascript+css實(shí)現(xiàn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了javascript+css實(shí)現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03nest.js 使用express需要提供多個(gè)靜態(tài)目錄的操作方法
這篇文章主要介紹了nest.js 使用express需要提供多個(gè)靜態(tài)目錄的操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10使用uniapp打包微信小程序時(shí)主包和vendor.js過大解決(uniCloud的插件分包)
每個(gè)使用分包小程序必定含有一個(gè)主包,所謂的主包,即放置默認(rèn)啟動(dòng)頁面/TabBar頁面,以及一些所有分包都需用到公共資源/JS 腳本,下面這篇文章主要給大家介紹了關(guān)于使用uniapp打包微信小程序時(shí)主包和vendor.js過大解決的相關(guān)資料,,需要的朋友可以參考下2023-02-02