十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
layui table默认跳页后不存在源页面选中的状态。如果要实现记录源页面的选中状态,需要定义一个全局变量来记录数据选中状态。
10年积累的网站建设、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先制作网站后付款的网站建设流程,更有新荣免费网站建设让你可以放心的选择与我们合作。
1、修改table.js
;layui.define(["laytpl", "laypage", "layer", "form"], function (e) { "use strict"; var t = layui.$, i = layui.laytpl, a = layui.laypage, l = layui.layer, n = layui.form, o = layui.hint(), r = layui.device(), d = { //layuiPageCheckedIds变量记录选中的数据id,格式是“,1,2,3,4,”的字符串 config: {checkName: "LAY_CHECKED", indexName: "LAY_TABLE_INDEX", layuiPageCheckedIds: ","}, cache: {}, index: layui.table ? layui.table.index + 1e4 : 0, set: function (e) { var i = this; return i.config = t.extend({}, i.config, e), i }, on: function (e, t) { return layui.onevent.call(this, s, e, t) } } ... S.prototype.pullData = function (e, i) { var a = this, n = a.config, o = n.request, r = n.response, d = function () { "object" == typeof n.initSort && a.sort(n.initSort.field, n.initSort.type) }; if (a.startTime = (new Date).getTime(), n.url) { var c = {}; //在发送请求前将选中的数据放入条件中 if(n.layuiPageCheckedIds != undefined && "" != n.layuiPageCheckedIds){ n.where.layuiCheckedIds = n.layuiPageCheckedIds; } if(tableRender){ var currPageNumber = getPageNumber(); c[o.pageName] = currPageNumber; tableRender = false; }else{ setPageNumberCookie(e); c[o.pageName] = e; } c[o.limitName] = n.limit; t.ajax({ type: n.method || "get", url: n.url, data: t.extend(c, n.where), dataType: "json", success: function (t) { if(null != startLoading){ layer.close(startLoading); startLoading = null; } return t[r.statusName] != r.statusCode ? (a.renderForm(), a.layMain.html('' + (t[r.msgName] || "返回的数据状态异常") + "")) : (a.renderData(t, e, t[r.countName]), d(), n.time = (new Date).getTime() - a.startTime + " ms", i && l.close(i), void("function" == typeof n.done && n.done(t, e, t[r.countName]))) }, error: function (e, t) { a.layMain.html('数据接口请求异常'), a.renderForm(), i && l.close(i) } }); } else if (n.data && n.data.constructor === Array) { if(null != startLoading){ layer.close(startLoading); startLoading = null; } var s = {}, u = e * n.limit - n.limit; s[r.dataName] = n.data.concat().splice(u, n.limit), s[r.countName] = n.data.length, a.renderData(s, e, n.data.length), d(), "function" == typeof n.done && n.done(s, e, s[r.countName]) } } ... //在文件的最后添加设置选中变量的方法 //设置初始选中值 ,d.initPageCheckedIds=function(e,i){ if(i == undefined || i == ""){ i = ""; } if(i.length > 0 && "," != i.substring(0,1)){ i = "," + i; } if(i.length > 0 && "," != i.substring(i.length - 1, i.length)){ i = i + ","; } c.config[e].layuiPageCheckedIds = i; }, //获取选中的ids, 参数e表示table的id d.getLayuiPageCheckedIds=function (e) { return c.config[e].layuiPageCheckedIds; }, //设置选中的ids,e表示table的id,obj表示选中的行数据,v表示记录哪一个参数 d.setLayuiPageCheckedIds=function (e,obj,v) { var a = c.config[e]; var layuiPageCheckedIds = a.layuiPageCheckedIds; //全选按钮 if(obj.type == "all"){ //全选 if (obj.checked) { var checkStatus = d.checkStatus(e); var datas = checkStatus.data; for(var i = 0; i < datas.length; i++) { layuiPageCheckedIds = d.setPageSelected(layuiPageCheckedIds, datas[i][v]); } } //取消全选 else { var cache = d.cache; var datas = cache[e]; for(var i = 0; i < datas.length; i++) { layuiPageCheckedIds = d.cancelPageSelected(layuiPageCheckedIds, datas[i][v]); } } } //单行选中 else { //选中 if (obj.checked) { layuiPageCheckedIds = d.setPageSelected(layuiPageCheckedIds, obj.data[v]); } //取消选中 else { layuiPageCheckedIds = d.cancelPageSelected(layuiPageCheckedIds, obj.data[v]); } } a.layuiPageCheckedIds = layuiPageCheckedIds; }, //添加选中项 d.setPageSelected=function(layuiPageCheckedIds, seleted){ if(layuiPageCheckedIds == undefined || layuiPageCheckedIds == ""){ return "," + seleted + ","; }else{ return layuiPageCheckedIds + seleted + ","; } }, //取消选中项 d.cancelPageSelected=function (layuiPageCheckedIds, seleted) { var match = "," + seleted + ","; var pos = layuiPageCheckedIds.indexOf(match); if (pos >= 0) { return layuiPageCheckedIds.substring(0, pos) + layuiPageCheckedIds.substring(pos + match.length - 1); } } });
2、后台根据id设置数据选中状态
@RequestMapping(value = "/search", method = RequestMethod.GET) @ResponseBody public Mapsearch(Pageable pageable, @RequestParam List layuiCheckedIds) { Map rst = new HashMap<>(4); rst.put("code", 0); rst.put("msg", ""); Page page = personService.findPage(pageable); rst.put("count", page.getTotalSize()); List
3、前端用法
layui.use("table", function(){ var table = layui.table; //设置初始选中项 var checkedIds = "1,2,3,4"; table.render({ elem: '#test', url:"./person/search", where: {"layuiCheckedIds": checkedIds }, ,cols: [[ {type:'checkbox', width:'10%'}, {field:'id', title: 'ID', width:'20%'}, {field:'name', title: '姓名', width:'15%'} ]], page: true, id:'idTest' }); //初始化选中项 table.initPageCheckedIds('idTest', checkedIds ); //监听表格复选框选择 table.on('checkbox(demo)', function(obj){ //设置多页面选中效果 //第一个参数表示table的id值,第二个参数表示复选框选中的数据,第三个参数表示要解析的是哪一个属性值(这里是将数据模型中的id属性进行解析)。 table.setLayuiPageCheckedIds('idTest',obj, "id"); //获取所有选中的id值(例如:“,1,2,5,7,3,15,22”) //alert(table.getLayuiPageCheckedIds('idTest')); }); });
以上这篇layui table 复选框跳页后再回来保持原来选中的状态示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持创新互联。