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);