LAYUI - TABLEFILTER
tableFilter交叉过滤组件,本地/服务端数据过滤,文本/单选/多选过滤类型,自动/自定义/AJAX过滤项,支持IE8
创建一个和平常一样的table实例,并且再创建一个tableFilter实例,挂载到table即可完成 本地过滤不建议开启分页和排序
和local本地过滤类似。只是将过滤逻辑交给服务端再返回给前端,刷新表格后显示结果数据
因为是本地JSON数据演示,返回数据不会变!可以打开F12控制台Network中查看请求的参数
table.reload('apitable')
tableFilter最核心的一个方法就是 tableFilter.render(options)
//1、加载CSS
//2、定义layui组件 得到 tableFilter 对象
var tableFilter = layui.tableFilter;
//3、定义一个tableFilter 挂载到 table 上
var tableFilterIns = tableFilter.render({
'elem' : '#localtable',//table的选择器
'mode' : 'local',//过滤模式
'filters' : [],//过滤项配置
'done': function(filters){
//结果回调
}
})
options参数说明
参数选项说明类型示例值
elem指定原始 table 容器的选择器。string"#table"mode设置过滤方式。
- local 本地数据过滤,不匹配的元素将被隐藏
- api 服务端过滤,将过滤值放于table where参数中 重新reload()
- 其他字符 自定义过滤,组件将不做处理,仅仅将过滤值放在 done回调中
filters参数说明
filters 是一个对象数组 定义哪几个列使用哪种过滤类型
参数选项说明类型示例值
field指定哪列使用过滤,与 table 中 cols 里的 field 同值。string"id"name用于api过滤方式,服务端需要的的参数名。 不填则取 field 的值string"ID"type设置过滤类型。
- input 输入框文本过滤
- radio 单选过滤
- checkbox 多选过滤
[{ "key":"1", "value":"有效"},{ "key":"0", "value":"失效"}]Array[{},{}]url用于单选/多选AJAX显示过滤项,暂只支持 GET 方式,请求url地址,务必返回格式如下。 非必填
{
"code": 0,
"msg": "",
"data": [
{ "key":"1", "value":"有效"},
{ "key":"0", "value":"失效"}
]
}
string"josn/filter.json"更新日志
2019/01/23 FIX 过滤项不自动更新,合计列不更新,合计列计算精度
2019/01/21 完成初始版本
嗯~单位项目有需求~开始写代码
layui.use(['jquery', 'table', 'code', 'tableFilter'], function(){
layui.code();
var $ = layui.jquery,
table = layui.table,
tableFilter = layui.tableFilter;
//本地演示
var localtable = table.render({
elem: '#localtable',
page: false,
height : '313',
totalRow : true,
url:'json/list.json',
cols: [[
{type: 'numbers', title: '序号', fixed: 'left', totalRowText: "合"},
{field: 'state', title: '状态(Tds)',fixed: 'left', width: 200},
{field: 'id', title: '学号', width: 200},
{field: 'username', title: '姓名(Ajax)', width: 200},
{field: 'sex', title: '性别(Tds)', width: 150},
{field: 'jf', title: '积分', width: 100, totalRow:true},
{field: 'class', title: '班级(Data)', width: 150}
]],
done: function(res, curr, count){
}
});
var localtableFilterIns = tableFilter.render({
'elem' : '#localtable',
'parent' : '#doc-content',
'mode' : 'local',
'filters' : [
{field: 'state', type:'checkbox'},
{field: 'id', type:'input'},
{field: 'username', type:'checkbox', url:'json/filter.json'},
{field: 'sex', type:'radio'},
{field: 'class', type:'checkbox', data:[{ "key":"12", "value":"十二班"}]}
],
'done': function(filters){}
})
//API演示
var apitable = table.render({
elem: '#apitable',
page: true,
height : '313',
url:'json/list.json',
cols: [[
{checkbox: true, fixed: 'left'},
{type: 'numbers', title: '序号', fixed: 'left'},
{field: 'state', title: '状态(Tds)',fixed: 'left', width: 200},
{field: 'id', title: '学号', width: 200},
{field: 'username', title: '姓名(Ajax)'},
{field: 'sex', title: '性别(Tds)', width: 150},
{field: 'class', title: '班级(Data)', width: 150}
]],
done: function(res, curr, count){
console.log("监听where:", this.where);
//非常重要!如果使table.reload()后依然使用过滤,就必须将过滤组件也reload()一下
apitableFilterIns.reload()
}
});
var apitableFilterIns = tableFilter.render({
'elem' : '#apitable',
'parent' : '#doc-content',
'mode' : 'api',
'filters' : [
{field: 'state', type:'checkbox'},
{field: 'id', type:'input'},
{field: 'username', type:'checkbox', url:'json/filter.json'},
{field: 'sex', type:'radio'},
{field: 'class', type:'checkbox', data:[{ "key":"12", "value":"十二班"}]}
],
'done': function(filters){
}
})
//reload演示
$('#r').on('click', function(e) {
table.reload('apitable')
})
});
一键复制
编辑
Web IDE
原始数据
按行查看
历史