圆月山庄资源网 Design By www.vgjia.com

layui table默认跳页后不存在源页面选中的状态。如果要实现记录源页面的选中状态,需要定义一个全局变量来记录数据选中状态。

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 "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 "' + f + '">' + (t[r.msgName] || "返回的数据状态异常") + "</div>")) : (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('<div class="' + f + '">数据接口请求异常</div>'), 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 Map<String, Object> search(Pageable pageable, @RequestParam List<Long> layuiCheckedIds) {
  Map<String, Object> rst = new HashMap<>(4);
  rst.put("code", 0);
  rst.put("msg", "");

  Page<Person> page = personService.findPage(pageable);
  
  rst.put("count", page.getTotalSize());
  List<Map<String, Object datas = new ArrayList<>(8);
  
  for (Person person : page.getContent()) {
   Map<String, Object> view = new HashMap<>(4);
   view.put("id", person.getId());
   view.put("name", person.getName());
 
 //如果person.getId()已经被选中,则设置选中状态
   if (!CollectionUtils.isEmpty(layuiCheckedIds) && layuiCheckedIds.contains(person.getId())) {
    view.put("LAY_CHECKED", true);
   }
   ...
   datas.add(view);
  }
  rst.put("data", datas);
  return rst;
 }

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 复选框跳页后再回来保持原来选中的状态示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
layui,table,复选框,跳页,选中

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com