From 747c786040f555b13eb476108a7320b261a16f57 Mon Sep 17 00:00:00 2001 From: morning-star <26325820+Sight-wcg@users.noreply.github.com> Date: Fri, 19 Apr 2024 12:02:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(treeTable):=20=E5=88=A0=E9=99=A4=E5=AD=90?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=97=B6=EF=BC=8C=E6=9C=AA=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=20isParent=20Key=20=E7=9A=84=E9=97=AE=E9=A2=98=20(#1792)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 66 ++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index c6697170..8e7e8d8a 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -514,9 +514,7 @@ layui.define(['table'], function (exports) { var update = function(data, parentIndex){ 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_PARENT_INDEX] = parentIndex = parentIndex || ''; var dataIndex = item1[LAY_DATA_INDEX] = (parentIndex ? parentIndex + '-' : '') + i1; @@ -576,14 +574,11 @@ layui.define(['table'], function (exports) { // 找到表格中的同类节点(需要找到lay-data-index一致的所有行) var trsElem = tableViewElem.find('tr[lay-data-index="' + dataIndex + '"]'); - // 处理折叠按钮图标 - var flexIconElem = trsElem.find('.layui-table-tree-flexIcon'); - flexIconElem.html(trExpand ? treeOptions.view.flexIconOpen : treeOptions.view.flexIconClose) - trData[isParentKey] && flexIconElem.css('visibility', 'visible'); - // 处理节点图标 - 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); + treeTableThat.updateNodeIcon({ + scopeEl: trsElem, + isExpand: trExpand, + isParent: trData[isParentKey] + }); trData[LAY_EXPAND] = trExpand; var trDataId = trData[customName.id]; trDataId !== undefined && (treeTableThat.status.expand[trDataId] = trExpand); @@ -952,26 +947,38 @@ layui.define(['table'], function (exports) { treeTable.resize(id); } + /** + * @typedef updateNodeIconOptions + * @prop {JQuery} scopeEl - tr 元素 + * @prop {boolean} isExpand - 是否是展开图标 + * @prop {boolean} isParent - 是否是父节点图标 + */ /** * 更新节点图标 - * @param {JQuery} scopeEl tr 元素 - * @param {Boolean} isExpand 是否是展开图标 - * @param {Boolean} isParent 是否是父节点图标 + * @param {updateNodeIconOptions} opts */ - Class.prototype.updateNodeIconByTrElem = function(scopeEl, isExpand, isParent){ + Class.prototype.updateNodeIcon = function(opts){ var that = this; var options = that.getOptions(); var treeOptions = options.tree || {}; + var scopeEl = opts.scopeEl; + var isExpand = opts.isExpand; + var isParent = opts.isParent; + // 处理折叠按钮图标 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){ + var nodeIconElem = scopeEl.find('.layui-table-tree-nodeIcon:not(.layui-table-tree-iconCustom)'); var nodeIcon = isParent ? (isExpand ? treeOptions.view.iconOpen : treeOptions.view.iconClose) : treeOptions.view.iconLeaf; - scopeEl.find('.layui-table-tree-nodeIcon:not(.layui-table-tree-iconCustom)') + + nodeIconElem .toggleClass('layui-table-tree-iconLeaf', !isParent) .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'); var nodeP = that.getNodeDataByIndex(delNode[LAY_PARENT_INDEX]); 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]); layui.each(delNodesFlat, function (i2, delNode) { 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); }) 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); @@ -1505,7 +1513,11 @@ layui.define(['table'], function (exports) { // 更新父节点图标状态 if(parentNode){ 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); if (focus) {