From 1585fd2d1d1b2bd8635613b047c248bc886e0795 Mon Sep 17 00:00:00 2001 From: morning-star <26325820+Sight-wcg@users.noreply.github.com> Date: Fri, 12 Apr 2024 17:50:06 +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=E5=86=85=E9=83=A8=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=9C=AA=E5=90=8C=E6=AD=A5=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=9B=E8=8A=82=E7=82=B9=E7=A7=BB=E5=8A=A8=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E4=BA=8B=E4=BB=B6=E4=B8=AD=E8=A1=8C=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E8=8E=B7=E5=8F=96=E9=94=99=E8=AF=AF=20(#1760)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(treeTable): 删除子节点时,内部缓存未同步的问题 * perf(treeTable): 减少不必要的缓存更新 * fix(treeTable): 节点移动后,点击事件中行索引获取错误 * refactor: 改进 724c011 --- src/modules/table.js | 3 ++- src/modules/treeTable.js | 51 ++++++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/modules/table.js b/src/modules/table.js index 33f5b92c..8f608e9c 100644 --- a/src/modules/table.js +++ b/src/modules/table.js @@ -2285,7 +2285,8 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){ //数据行中的事件返回的公共对象成员 var commonMember = that.commonMember = function(sets){ var othis = $(this); - var index = othis.parents('tr').eq(0).data('index'); + // treeTable 中使用 attr('data-index'), tr 更新时 jQuery 的缓存会导致获取到错误的 index + var index = othis.parents('tr').eq(0).attr('data-index'); var tr = that.layBody.find('tr[data-index="'+ index +'"]'); var data = table.cache[that.key] || []; diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index e3adec76..c009b2f8 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -95,7 +95,7 @@ layui.define(['table'], function (exports) { layui.each(data || tableCache, function (index, item) { var itemDataIndex = item[LAY_DATA_INDEX] || ''; if (itemDataIndex.indexOf('-') !== -1) { - tableCache[itemDataIndex] = item + tableCache[itemDataIndex] = item; } item[childrenKey] && updateCache(id, childrenKey, item[childrenKey]); }) @@ -505,15 +505,19 @@ layui.define(['table'], function (exports) { var isParentKey = customName.isParent; var childrenKey = customName.children; - layui.each(data, function (i1, item1) { - if (!(isParentKey in item1)) { - 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; - that.initData(item1[childrenKey] || [], dataIndex); - }); + var update = function(data, parentIndex){ + layui.each(data, function (i1, item1) { + if (!(isParentKey in item1)) { + 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; + update(item1[childrenKey] || [], dataIndex); + }); + } + + update(data, parentIndex); updateCache(tableId, childrenKey, data); @@ -1257,17 +1261,38 @@ layui.define(['table'], function (exports) { var tableView = options.elem.next(); var delNode; var indexArr = []; + var tableCache = table.cache[id]; 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); var delNodesFlat = that.treeToFlat([delNode], delNode[treeOptions.customName.pid], delNode[LAY_PARENT_INDEX]); - layui.each(delNodesFlat, function (i2, item2) { - indexArr.push('tr[lay-data-index="' + item2[LAY_DATA_INDEX] + '"]'); + layui.each(delNodesFlat, function (i2, delNode) { + var delNodeDataIndex = delNode[LAY_DATA_INDEX]; + indexArr.push('tr[lay-data-index="' + delNodeDataIndex + '"]'); + // 删除临时 key + if(delNodeDataIndex.indexOf('-') !== -1){ + delete tableCache[delNodeDataIndex]; + } }) tableView.find(indexArr.join(',')).remove(); // 删除行 + + var deleteCacheKey = function(){ + for (var key in tableCache) { + // 根节点 getNodeDataByIndex 内部已处理 + if(key.indexOf('-') !== -1){ + // L93 updateCache() 中,cacheKey 取自 rowData 中的 LAY_DATA_INDEX, + // 两者不同说明当前 cacheKey 引用的 rowData 已被更新 + if(key !== tableCache[key][LAY_DATA_INDEX]){ + delete tableCache[key] + } + } + } + } + // 重新整理数据 var tableData = that.initData(); + deleteCacheKey(); // index发生变化需要更新页面tr中对应的lay-data-index 新增和删除都要注意数据结构变动之后的index问题 layui.each(that.treeToFlat(tableData), function (i3, item3) { if (item3[LAY_DATA_INDEX_HISTORY] && item3[LAY_DATA_INDEX_HISTORY] !== item3[LAY_DATA_INDEX]) { @@ -1279,7 +1304,7 @@ layui.define(['table'], function (exports) { } }); // 重新更新顶层节点的data-index; - layui.each(table.cache[id], function (i4, item4) { + layui.each(tableCache, function (i4, item4) { tableView.find('tr[data-level="0"][lay-data-index="' + item4[LAY_DATA_INDEX] + '"]').attr('data-index', i4); }) options.hasNumberCol && formatNumber(that);