表格分页时需维持联动
var selectedId = new Array();
var currentPage = new Array();
table.render({
...
, done: function (res, cur, cnt) {
currentPage = res.data;
setRowId(currentPage);
}
});
table.on('checkbox(layerTableFilter)', function (obj) {
if (obj.checked == true) {
if (obj.type == 'one') {
selectedId.push(obj.data.id);
addListOption(obj.data);
} else {
currentPage.forEach(function(item){
if(!selectedId.includes(item.id)){
selectedId.push(item.id);
addListOption(item);
}
})
}
} else {
if (obj.type == 'one') {
removeSelectedId(obj.data.id);
removeListOption(obj.data);
} else {
currentPage.forEach(function(item){
removeSelectedId(item.id);
removeListOption(item);
})
}
}
})
function setRowId(currentPage) {
var index = 0;
$(".layui-table-body tr").each(function () {
$(this).attr("id",currentPage[index++].id);
})
}
function addListOption(obj) {
var option = '<li class="layer-list" data-name="' + obj.name + '" data-id="' + obj.id + '"><i class="layui-icon layui-icon-layer"></i>' + obj.name + '<i class="layui-icon layui-icon-close"></i></li>'
$("#rightList").append(option);
addLayerListEven();
}
function removeListOption(obj) {
var option = ".layer-list[data-id='" + obj.id + "']";
$(option).remove();
}
function addLayerListEven() {
$(".layer-list .layui-icon-close").off().click(function () {
$(this).parent().remove();
var optionId = $(this).parent().attr("data-id");
removeSelectedId(optionId);
var selectRow = "tr[id='" + optionId + "']";
$(selectRow).find("input").attr("checked", false);
form.render();
})
}
function removeSelectedId(optionId) {
var index = selectedId.indexOf(optionId);
if (index > -1) {
selectedId.splice(index, 1);
}
}