From 0cdd11ae6855db77ad7c2b7b24d9189bdbe05306 Mon Sep 17 00:00:00 2001 From: morning-star <26325820+Sight-wcg@users.noreply.github.com> Date: Sun, 14 Apr 2024 10:16:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(treeTable):=20=E6=B7=BB=E5=8A=A0=E5=92=8C?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=8A=82=E7=82=B9=E6=97=B6=EF=BC=8C=E6=9C=AA?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=8A=82=E7=82=B9=E5=9B=BE=E6=A0=87=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#1784)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(treeTable): 添加和删除节点时,未更新节点图标的问题 * update code --- src/modules/treeTable.js | 49 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index c009b2f8..c6697170 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -47,7 +47,11 @@ layui.define(['table'], function (exports) { } } - // 获取当前实例 + /** + * 获取当前实例 + * @param {string} id 表格id + * @returns {Class} + */ var getThisTable = function (id) { var that = thisTreeTable.that[id]; if (!that) hint.error(id ? ('The treeTable instance with ID \'' + id + '\' not found') : 'ID argument required'); @@ -80,7 +84,10 @@ layui.define(['table'], function (exports) { var LAY_ASYNC_STATUS = 'LAY_ASYNC_STATUS'; var LAY_CASCADE = ['all', 'parent', 'children', 'none']; - // 构造器 + /** + * 构造器 + * @class + */ var Class = function (options) { var that = this; that.index = ++treeTable.index; @@ -945,6 +952,31 @@ layui.define(['table'], function (exports) { treeTable.resize(id); } + /** + * 更新节点图标 + * @param {JQuery} scopeEl tr 元素 + * @param {Boolean} isExpand 是否是展开图标 + * @param {Boolean} isParent 是否是父节点图标 + */ + Class.prototype.updateNodeIconByTrElem = function(scopeEl, isExpand, isParent){ + var that = this; + var options = that.getOptions(); + var treeOptions = options.tree || {}; + // 处理折叠按钮图标 + 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'); + // 处理节点图标 + if(treeOptions.view.showIcon){ + var nodeIcon = isParent + ? (isExpand ? treeOptions.view.iconOpen : treeOptions.view.iconClose) + : treeOptions.view.iconLeaf; + scopeEl.find('.layui-table-tree-nodeIcon:not(.layui-table-tree-iconCustom)') + .toggleClass('layui-table-tree-iconLeaf', !isParent) + .html(nodeIcon); + } + } + Class.prototype.renderTreeTable = function (tableView, level, sonSign) { var that = this; var options = that.getOptions(); @@ -1265,6 +1297,14 @@ 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]; @@ -1462,6 +1502,11 @@ layui.define(['table'], function (exports) { expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + parentIndex + '"]')}, true) } that.updateCheckStatus(parentNode); + // 更新父节点图标状态 + if(parentNode){ + var trEl = tableViewElem.find('tr[lay-data-index="' + parentNode[LAY_DATA_INDEX] + '"]'); + that.updateNodeIconByTrElem(trEl, true, true); + } treeTable.resize(id); if (focus) { // 滚动到第一个新增的节点