fix(treeTable): 删除子节点时,内部缓存未同步的问题;节点移动后,点击事件中行索引获取错误 (#1760)

* fix(treeTable): 删除子节点时,内部缓存未同步的问题

* perf(treeTable): 减少不必要的缓存更新

* fix(treeTable): 节点移动后,点击事件中行索引获取错误

* refactor: 改进 724c011
pull/1784/head^2
morning-star 8 months ago committed by GitHub
parent 394c7e7ee7
commit 1585fd2d1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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] || [];

@ -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,6 +505,7 @@ layui.define(['table'], function (exports) {
var isParentKey = customName.isParent;
var childrenKey = customName.children;
var update = function(data, parentIndex){
layui.each(data, function (i1, item1) {
if (!(isParentKey in item1)) {
item1[isParentKey] = !!(item1[childrenKey] && item1[childrenKey].length);
@ -512,8 +513,11 @@ layui.define(['table'], function (exports) {
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);
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);

Loading…
Cancel
Save