修复 treeTable 节点选中状态判断异常问题

pull/1260/head
sunxiaobin89 2 years ago
parent cdd8934d6d
commit 3772c6f1e8

@ -1125,7 +1125,7 @@ layui.define(['table'], function (exports) {
var indexArr = []; var indexArr = [];
delNode = that.getNodeDataByIndex(layui.type(node) === 'string' ? node : node[LAY_DATA_INDEX], false, 'delete'); delNode = that.getNodeDataByIndex(layui.type(node) === 'string' ? node : node[LAY_DATA_INDEX], false, 'delete');
var nodeP = that.getNodeDataByIndex(delNode[LAY_PARENT_INDEX]); var nodeP = that.getNodeDataByIndex(delNode[LAY_PARENT_INDEX]);
that.updateCheckStatus(nodeP, true); that.updateCheckStatus(nodeP);
var delNodesFlat = that.treeToFlat([delNode], delNode[treeOptions.customName.pid], delNode[LAY_PARENT_INDEX]); var delNodesFlat = that.treeToFlat([delNode], delNode[treeOptions.customName.pid], delNode[LAY_PARENT_INDEX]);
layui.each(delNodesFlat, function (i2, item2) { layui.each(delNodesFlat, function (i2, item2) {
indexArr.push('tr[lay-data-index="' + item2[LAY_DATA_INDEX] + '"]'); indexArr.push('tr[lay-data-index="' + item2[LAY_DATA_INDEX] + '"]');
@ -1148,7 +1148,7 @@ layui.define(['table'], function (exports) {
layui.each(table.cache[id], function (i4, item4) { layui.each(table.cache[id], function (i4, item4) {
tableView.find('tr[data-level="0"][lay-data-index="' + item4[LAY_DATA_INDEX] + '"]').attr('data-index', i4); tableView.find('tr[data-level="0"][lay-data-index="' + item4[LAY_DATA_INDEX] + '"]').attr('data-index', i4);
}) })
options.hasNumberCol && that.formatNumber(id); options.hasNumberCol && formatNumber(that);
} }
/** /**
@ -1279,7 +1279,7 @@ layui.define(['table'], function (exports) {
parentNode[LAY_ASYNC_STATUS] = 'local'; // 转为本地数据应该规定异步加载子节点的时候addNodes的规则 parentNode[LAY_ASYNC_STATUS] = 'local'; // 转为本地数据应该规定异步加载子节点的时候addNodes的规则
expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + parentIndex + '"]')}, true) expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + parentIndex + '"]')}, true)
} }
that.updateCheckStatus(parentNode, true); that.updateCheckStatus(parentNode);
treeTable.resize(id); treeTable.resize(id);
if (focus) { if (focus) {
// 滚动到第一个新增的节点 // 滚动到第一个新增的节点
@ -1415,7 +1415,7 @@ layui.define(['table'], function (exports) {
// 如有必要更新父节点们的状态 // 如有必要更新父节点们的状态
if (dataP) { if (dataP) {
var trsP = that.updateParentCheckStatus(dataP, checked); var trsP = that.updateParentCheckStatus(dataP, layui.type(checked) === 'boolean' ? checked : null);
layui.each(trsP, function (indexP, itemP) { layui.each(trsP, function (indexP, itemP) {
form.render(tableView.find('tr[lay-data-index="' + itemP[LAY_DATA_INDEX] + '"] input[name="layTableCheckbox"]:not(:disabled)').prop({ form.render(tableView.find('tr[lay-data-index="' + itemP[LAY_DATA_INDEX] + '"] input[name="layTableCheckbox"]:not(:disabled)').prop({
checked: itemP[checkName], checked: itemP[checkName],
@ -1455,7 +1455,7 @@ layui.define(['table'], function (exports) {
var dataRet = []; var dataRet = [];
dataP[LAY_CHECKBOX_HALF] = false; // 先设置为非半选,是否为半选又下面逻辑判断 dataP[LAY_CHECKBOX_HALF] = false; // 先设置为非半选,是否为半选又下面逻辑判断
if (checked) { if (checked === true) {
// 为真需要判断子节点的情况 // 为真需要判断子节点的情况
if (!dataP[childrenKey].length) { if (!dataP[childrenKey].length) {
checked = false; checked = false;
@ -1468,7 +1468,7 @@ layui.define(['table'], function (exports) {
} }
}); });
} }
} else { } else if (checked === false) {
// 判断是否为半选 // 判断是否为半选
layui.each(dataP[childrenKey], function (index, item) { layui.each(dataP[childrenKey], function (index, item) {
if (item[checkName] || item[LAY_CHECKBOX_HALF]) { // 只要有一个子节点为选中或者半选状态 if (item[checkName] || item[LAY_CHECKBOX_HALF]) { // 只要有一个子节点为选中或者半选状态
@ -1476,6 +1476,17 @@ layui.define(['table'], function (exports) {
return true; return true;
} }
}); });
} else {
// 状态不确定的情况下根据子节点的信息
checked = false;
var checkedNum = 0;
layui.each(dataP[childrenKey], function (index, item) {
if (item[checkName]) {
checkedNum++;
}
});
checked = dataP[childrenKey].length ? dataP[childrenKey].length === checkedNum : dataP[checkName]; // 如果没有子节点保留原来的状态;
dataP[LAY_CHECKBOX_HALF] = checked ? false : checkedNum > 0;
} }
dataP[checkName] = checked; dataP[checkName] = checked;
dataRet.push($.extend({}, dataP)); dataRet.push($.extend({}, dataP));
@ -1585,13 +1596,13 @@ layui.define(['table'], function (exports) {
} }
}) })
/** /**
* 设置行选中状态 * 设置行选中状态
* @param {String} id 树表id * @param {String} id 树表id
* @param {Object|String} index 节点下标 * @param {Object} opts
* @param {Boolean} checked 选中或取消 * @param {Object|String} opts.index 节点下标
* @param {Boolean} [callbackFlag] 是否触发事件回调 * @param {Boolean} opts.checked 选中或取消
* @param {Boolean} [opts.callbackFlag] 是否触发事件回调
* */ * */
treeTable.setRowChecked = function (id, opts) { treeTable.setRowChecked = function (id, opts) {
var that = getThisTable(id); var that = getThisTable(id);

Loading…
Cancel
Save