检索了很多跨页记忆的,基本上都涉及到了表格字段的编辑,每次使用需要复制大量重复代码,于是手动封装了一段公共js(逻辑不复杂,主要是可公用,不用每个界面写一堆代码)
使用的时候就在table初始化的done方法中加入一行代码即可
AOQ_LayuiTableCheckBoxOnPage(layui.table, "你的tableId建议需和Filter相同");
获取选中的数据
console.log(AOQ.checkData);
table.render({
cols: [[
{field:'id', title:'ID', width:100}
,{fixed: 'right', width:150, align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
]],
done: function (res, curr, count) {
//就这一行
AOQ_LayuiTableCheckBoxOnPage(layui.table, "tableIdFilter");
}
});
//============================================================================
//封装公共 js
//done 方法里面设置 AOQ_LayuiTableCheckBoxOnPage(table,"layuiTableId");
//获取选中的数据 console.log(AOQ.checkData);
function AOQ_LayuiTableCheckBoxOnPage(_layuiTable, _tableElemIdFilter)
{
if (!_layuiTable || !_tableElemIdFilter || !_layuiTable.cache || !_layuiTable.cache[_tableElemIdFilter]) { return; }
AOQ.tableData = _layuiTable.cache[_tableElemIdFilter];
if (typeof (AOQ.checkData) == "undefined") { AOQ.checkData = []; }
for (var i = 0; i < AOQ.tableData.length; i++)
{
for (var j = 0; j < AOQ.checkData.length; j++)
{
var _theItem = JSON.parse(JSON.stringify(AOQ.tableData[i]));
delete _theItem.LAY_TABLE_INDEX;
delete _theItem.LAY_CHECKED;
if (JSON.stringify(_theItem) == JSON.stringify(AOQ.checkData[j]))
{
AOQ.tableData[i]["LAY_CHECKED"] = 'true';
var index = AOQ.tableData[i]['LAY_TABLE_INDEX'];
$('.layui-table tr[data-index=' + index + '] input[type="checkbox"]').prop('checked', true);
$('.layui-table tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
}
}
}
var checkStatus = _layuiTable.checkStatus(_tableElemIdFilter);
if (checkStatus.isAll)
{
//是否全选
$('.layui-table th[data-field="0"] input[type="checkbox"]').prop('checked', true);
$('.layui-table th[data-field="0"] input[type="checkbox"]').next().addClass('layui-form-checked');
}
//绑定选定效果
if (!AOQ.isBindCheckEvent)
{
_layuiTable.on('checkbox(' + _tableElemIdFilter+')', function (obj)
{
var _theObjItem = JSON.parse(JSON.stringify(obj.data));
delete _theObjItem.LAY_TABLE_INDEX;
delete _theObjItem.LAY_CHECKED;
if (typeof (AOQ.checkData) == "undefined") { AOQ.checkData = []; }
if (obj.checked == true)
{
if (obj.type == 'one')
{
AOQ.checkData.push(_theObjItem);
} else
{
for (var i = 0; i < AOQ.tableData.length; i++)
{
var _theItem = JSON.parse(JSON.stringify(AOQ.tableData[i]));
delete _theItem.LAY_TABLE_INDEX;
delete _theItem.LAY_CHECKED;
AOQ.checkData.push(_theItem);
}
}
} else
{
if (obj.type == 'one')
{
for (var i = 0; i < AOQ.checkData.length; i++)
{
if (JSON.stringify(AOQ.checkData[i]) == JSON.stringify(_theObjItem))
{
AOQTools.deleteItemInArray(AOQ.checkData, AOQ.checkData[i]);
}
}
} else
{
for (var i = 0; i < AOQ.checkData.length; i++)
{
for (var j = 0; j < AOQ.tableData.length; j++)
{
var _theItem = JSON.parse(JSON.stringify(AOQ.tableData[j]));
delete _theItem.LAY_TABLE_INDEX;
delete _theItem.LAY_CHECKED;
if (JSON.stringify(AOQ.checkData[i]) == JSON.stringify(_theItem))
{
AOQTools.deleteItemInArray(AOQ.checkData, AOQ.checkData[i]);
}
}
}
}
}
});
AOQ.isBindCheckEvent = true;
}
}
var AOQ = {
checkData: [], //layui表格跨页复选框使用
tableData: [], //layui表格跨页复选框使用
isBindCheckEvent: false, //layui表格跨页复选框使用
}
//============================================================================