layui框架table 的 defaultToolbar 中实现筛选列记忆功能
编辑时间:2022-07-31 作者:贤心 浏览量:4082 来源:原创

用以下方法可以实现 filter 记忆功能:

table.render({
  elem: '#test'
  ,url: 'json/table/demo1.json'
  ,toolbar: 'default'
  ,cols: [function(){
    var arr = [
      {type: 'checkbox', fixed: 'left'}
      ,{field:'id', title:'ID', width:80, fixed: 'left'}
      ,{field:'username', title:'用户名', width:120}
      ,{field:'ip', title:'IP', width: 120}
      ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
    ];
    
    //初始化筛选状态
    var local = layui.data ('table-filter-test'); //此处的 test 与 table 的 id 参数对应
    layui.each(arr, function(index, item){
      if(item.field in local){
        item.hide = local[item.field];
      }
    });
    return arr;
  }() ]  
  ,done: function(){
    //记录筛选状态
    var that = this;
    that.elem.next().on('mousedown', 'input[lay-filter="LAY_TABLE_TOOL_COLS"]+', function(){
      var input = $(this).prev()[0];
      layui.data ('table-filter-'+ that.id, {
        key: input.name
        ,value: input.checked
      })
    });
  }
});

转自:https://gitee.com/layui/layui/issues/I40DGW

来说两句吧