layui 表格跨页选中复选框
if (obj.checked === true) {
if (shopArr.length == 0 && obj.type === "one") {
空数组直接push值
shopArr.push(obj.data.shopId)
}else if (obj.checked === true && obj.type === "all"){
// 全选的时候判断shopArr数组里是否已经存在当前页面的shopid
data.forEach(function (item){
if (shopArr.indexOf(item.shopId) == -1) {
shopArr.push(item.shopId)
}
})
}else if (obj.type === "one") {
// 点击单个的时候判断shopArr是否选中过,没有就选中
var flag = shopArr.some(function (item){
return item == obj.data.shopId
});
if (!flag) {
shopArr.push(obj.data.shopId)
}
}
}else if (obj.checked === false && obj.type === "all") {
全选取消选中
var newData = [],newShopArr = []
data.forEach(function (item){
newData.push(item.shopId)
})
for (var i=0; i<shopArr.length; i++) {
if (newData.indexOf(shopArr[i]) === -1) {
newShopArr.push(shopArr[i])
}
}
shopArr = newShopArr
}else if(obj.checked === false && obj.type === "one") {
单个取消选中
for (var i=shopArr.length-1; i>=0; i--) {
if (shopArr[i] == obj.data.shopId) {
shopArr.splice(i,1)
}
}
}
在表格done方法里
当前页的数据
var goodsData = res.data;
// 如果有选择的商品
if (shopArr.length > 0) {
goodsData.forEach(function(goodItem, i){
if (shopArr.includes(goodItem.shopId)) { //当前页面存在已经选择的数据,则勾选该条行
goodsData[i]["LAY_CHECKED"] = true;
var index = goodsData[i]['LAY_TABLE_INDEX'];
单个选中
$('#shopDecorationListTable').next('.layui-table-view').find('tr[data-index=' + index + '] input[type="checkbox"]').attr('checked', true);
$('#shopDecorationListTable').next('.layui-table-view').find('tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
}
})
var flag = goodsData.every(function (val){
return shopArr.includes(val.shopId)
})
flag 判断当前页面是否全选
if (flag) {
全选框选中
$('#shopDecorationListTable').next('.layui-table-view').find('.layui-table-header thead').children().find("input").attr('checked', true)
$('#shopDecorationListTable').next('.layui-table-view').find('.layui-table-header thead').children().find("input").next().addClass('layui-form-checked')
}
}
}