全局JS声明变量
var checkDatas = [];
var dataArr= [];
var eqVal='lineId';
监听的复选框函数
table.on('checkbox(test)', function(obj) {
let checked = obj.checked;
let tempAllCheckDatas=[];
if(checked){
if( obj.type == 'one'){
checkDatas.push(obj.data);
checkDatas.forEach(row=>{
tempAllCheckDatas.push(row[eqVal])
})
}
else{
checkDatas.forEach(row=>{
tempAllCheckDatas.push(row[eqVal])
})
for(var i=0;i<dataArr.length;i++){
if(tempAllCheckDatas.indexOf(dataArr[i][eqVal])<0){
tempAllCheckDatas.push(dataArr[i][eqVal])
checkDatas.push(dataArr[i])
}
}
}
}
else{
if(obj.type == 'one'){
checkDatas.map((item, index) => {
if (obj.data.eqVal == item.eqVal) {
checkDatas.splice(index, 1);
}
})
}
else{
dataArr.forEach(row=>{
for(let i=0;i<checkDatas.length;i++){
if(checkDatas[i][eqVal]==row[eqVal]){
checkDatas.splice(i, 1);
}
}
})
}
}
});
table的渲染处理
table.render({
elem: '#LAY_table_id(你的tableId)',
url: (你的接口链接)
,defaultToolbar:[]
,toolbar: '#toolbarHtml'
,title: '用户数据表',
cols: [
[
{checkbox: true, fixed: true},
{
title: '序号',
width: 80,
sort: true,
fixed: true,
type: 'numbers'
},{
field: 'lineId',
title: '线路id',
sort: true,
width: 110,
hide:true
}
]
]
,done: function(res, curr, count) {
dataArr = res.data;
set();
}
});
抽取的回显函数
function set(){
var len = dataArr.length;
var chooseNum = 0;
for (var i = 0; i < dataArr.length; i++) {
for (var j = 0; j < checkDatas.length; j++) {
if(dataArr[i][eqVal] === checkDatas[j][eqVal]){
dataArr[i]["LAY_CHECKED"] = 'true';
var index=i;
$('.layui-table tr[data-index=' + i + '] input[type="checkbox"]').prop('checked', true);
$('.layui-table tr[data-index=' + i + '] input[type="checkbox"]').next().addClass('layui-form-checked');
chooseNum++;
}
}
}
if (len !== 0 && chooseNum === len) {
alert("本页已被全选")
$(' .layui-table-header tr input[lay-filter="layTableAllChoose"]').prop('checked', true);
$(' .layui-table-header tr input[lay-filter="layTableAllChoose"]').next().addClass('layui-form-checked');
}
};
|