fix(treeTable): 删除子节点时,未更新 isParent Key 的问题 (#1792)

pull/1813/head
morning-star 7 months ago committed by GitHub
parent 13b0b42528
commit 747c786040
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -514,9 +514,7 @@ layui.define(['table'], function (exports) {
var update = function(data, parentIndex){ var update = function(data, parentIndex){
layui.each(data, function (i1, item1) { layui.each(data, function (i1, item1) {
if (!(isParentKey in item1)) { item1[isParentKey] = !!(item1[childrenKey] && item1[childrenKey].length);
item1[isParentKey] = !!(item1[childrenKey] && item1[childrenKey].length);
}
item1[LAY_DATA_INDEX_HISTORY] = item1[LAY_DATA_INDEX]; item1[LAY_DATA_INDEX_HISTORY] = item1[LAY_DATA_INDEX];
item1[LAY_PARENT_INDEX] = parentIndex = parentIndex || ''; item1[LAY_PARENT_INDEX] = parentIndex = parentIndex || '';
var dataIndex = item1[LAY_DATA_INDEX] = (parentIndex ? parentIndex + '-' : '') + i1; var dataIndex = item1[LAY_DATA_INDEX] = (parentIndex ? parentIndex + '-' : '') + i1;
@ -576,14 +574,11 @@ layui.define(['table'], function (exports) {
// 找到表格中的同类节点需要找到lay-data-index一致的所有行 // 找到表格中的同类节点需要找到lay-data-index一致的所有行
var trsElem = tableViewElem.find('tr[lay-data-index="' + dataIndex + '"]'); var trsElem = tableViewElem.find('tr[lay-data-index="' + dataIndex + '"]');
// 处理折叠按钮图标 treeTableThat.updateNodeIcon({
var flexIconElem = trsElem.find('.layui-table-tree-flexIcon'); scopeEl: trsElem,
flexIconElem.html(trExpand ? treeOptions.view.flexIconOpen : treeOptions.view.flexIconClose) isExpand: trExpand,
trData[isParentKey] && flexIconElem.css('visibility', 'visible'); isParent: trData[isParentKey]
// 处理节点图标 });
treeOptions.view.showIcon && trsElem
.find('.layui-table-tree-nodeIcon:not(.layui-table-tree-iconCustom,.layui-table-tree-iconLeaf)')
.html(trExpand ? treeOptions.view.iconOpen : treeOptions.view.iconClose);
trData[LAY_EXPAND] = trExpand; trData[LAY_EXPAND] = trExpand;
var trDataId = trData[customName.id]; var trDataId = trData[customName.id];
trDataId !== undefined && (treeTableThat.status.expand[trDataId] = trExpand); trDataId !== undefined && (treeTableThat.status.expand[trDataId] = trExpand);
@ -952,26 +947,38 @@ layui.define(['table'], function (exports) {
treeTable.resize(id); treeTable.resize(id);
} }
/**
* @typedef updateNodeIconOptions
* @prop {JQuery} scopeEl - tr 元素
* @prop {boolean} isExpand - 是否是展开图标
* @prop {boolean} isParent - 是否是父节点图标
*/
/** /**
* 更新节点图标 * 更新节点图标
* @param {JQuery} scopeEl tr 元素 * @param {updateNodeIconOptions} opts
* @param {Boolean} isExpand 是否是展开图标
* @param {Boolean} isParent 是否是父节点图标
*/ */
Class.prototype.updateNodeIconByTrElem = function(scopeEl, isExpand, isParent){ Class.prototype.updateNodeIcon = function(opts){
var that = this; var that = this;
var options = that.getOptions(); var options = that.getOptions();
var treeOptions = options.tree || {}; var treeOptions = options.tree || {};
var scopeEl = opts.scopeEl;
var isExpand = opts.isExpand;
var isParent = opts.isParent;
// 处理折叠按钮图标 // 处理折叠按钮图标
var flexIconElem = scopeEl.find('.layui-table-tree-flexIcon'); var flexIconElem = scopeEl.find('.layui-table-tree-flexIcon');
flexIconElem.html(isExpand ? treeOptions.view.flexIconOpen : treeOptions.view.flexIconClose);
flexIconElem.css('visibility', isParent || treeOptions.view.showFlexIconIfNotParent ? 'visible' : 'hidden'); flexIconElem
.css('visibility', isParent || treeOptions.view.showFlexIconIfNotParent ? 'visible' : 'hidden')
.html(isExpand ? treeOptions.view.flexIconOpen : treeOptions.view.flexIconClose);
// 处理节点图标 // 处理节点图标
if(treeOptions.view.showIcon){ if(treeOptions.view.showIcon){
var nodeIconElem = scopeEl.find('.layui-table-tree-nodeIcon:not(.layui-table-tree-iconCustom)');
var nodeIcon = isParent var nodeIcon = isParent
? (isExpand ? treeOptions.view.iconOpen : treeOptions.view.iconClose) ? (isExpand ? treeOptions.view.iconOpen : treeOptions.view.iconClose)
: treeOptions.view.iconLeaf; : treeOptions.view.iconLeaf;
scopeEl.find('.layui-table-tree-nodeIcon:not(.layui-table-tree-iconCustom)')
nodeIconElem
.toggleClass('layui-table-tree-iconLeaf', !isParent) .toggleClass('layui-table-tree-iconLeaf', !isParent)
.html(nodeIcon); .html(nodeIcon);
} }
@ -1297,14 +1304,6 @@ layui.define(['table'], function (exports) {
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); that.updateCheckStatus(nodeP);
// 更新父节点图标状态
if(nodeP){
var trEl = tableView.find('tr[lay-data-index="' + nodeP[LAY_DATA_INDEX] + '"]');
var isExpand = nodeP[treeOptions.customName.children].length > 0;
var isParent = nodeP[treeOptions.customName.children].length > 0;
that.updateNodeIconByTrElem(trEl, isExpand, isParent);
}
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, delNode) { layui.each(delNodesFlat, function (i2, delNode) {
var delNodeDataIndex = delNode[LAY_DATA_INDEX]; var delNodeDataIndex = delNode[LAY_DATA_INDEX];
@ -1348,6 +1347,15 @@ layui.define(['table'], function (exports) {
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 && formatNumber(that); options.hasNumberCol && formatNumber(that);
// 更新父节点图标状态
if(nodeP){
var trEl = tableView.find('tr[lay-data-index="' + nodeP[LAY_DATA_INDEX] + '"]');
that.updateNodeIcon({
scopeEl: trEl,
isExpand: nodeP[LAY_EXPAND],
isParent: nodeP[treeOptions.customName.isParent],
});
}
// 重新适配尺寸 // 重新适配尺寸
treeTable.resize(id); treeTable.resize(id);
@ -1505,7 +1513,11 @@ layui.define(['table'], function (exports) {
// 更新父节点图标状态 // 更新父节点图标状态
if(parentNode){ if(parentNode){
var trEl = tableViewElem.find('tr[lay-data-index="' + parentNode[LAY_DATA_INDEX] + '"]'); var trEl = tableViewElem.find('tr[lay-data-index="' + parentNode[LAY_DATA_INDEX] + '"]');
that.updateNodeIconByTrElem(trEl, true, true); that.updateNodeIcon({
scopeEl: trEl,
isExpand: parentNode[LAY_EXPAND],
isParent: parentNode[isParentKey],
});
} }
treeTable.resize(id); treeTable.resize(id);
if (focus) { if (focus) {

Loading…
Cancel
Save