Table: fix ExpandRowKeys change view is error, add ExpandRowKeys .sync modifier support

修复issue #18913
pull/21491/head
chenjg 2021-11-22 16:53:54 +08:00
parent 7f54540b6b
commit 89342f540a
4 changed files with 43 additions and 7 deletions

View File

@ -25,6 +25,14 @@ Watcher.prototype.mutations = {
this.updateAllSelected(); this.updateAllSelected();
this.updateTableScrollY(); this.updateTableScrollY();
this.$nextTick().then(() => {
states.lastDataKeysMap = {};
if (Array.isArray(data)) {
for (let el of data) {
states.lastDataKeysMap[el[states.rowKey]] = true;
}
}
});
}, },
insertColumn(states, column, index, parent) { insertColumn(states, column, index, parent) {

View File

@ -53,7 +53,13 @@ export default {
watch: { watch: {
normalizedData: 'updateTreeData', normalizedData: 'updateTreeData',
normalizedLazyNode: 'updateTreeData' normalizedLazyNode: 'updateTreeData',
'states.expandRowKeys': {
immediate: true,
handler: function(n) {
this.table && this.table.$emit('update:expandRowKeys', n);
}
}
}, },
methods: { methods: {
@ -99,14 +105,20 @@ export default {
treeData: oldTreeData, treeData: oldTreeData,
defaultExpandAll, defaultExpandAll,
expandRowKeys, expandRowKeys,
lazy lazy,
lastDataKeysMap
} = this.states; } = this.states;
const rootLazyRowKeys = []; const rootLazyRowKeys = [];
const getExpanded = (oldValue, key) => { const getExpanded = (oldValue, key) => {
const included = if (!lastDataKeysMap[key]) {
defaultExpandAll || if (defaultExpandAll) {
(expandRowKeys && expandRowKeys.indexOf(key) !== -1); if (expandRowKeys.indexOf(key) === -1) {
return !!((oldValue && oldValue.expanded) || included); expandRowKeys.push(key);
}
return true;
}
}
return expandRowKeys.indexOf(key) !== -1;
}; };
// 合并 expanded 与 display确保数据刷新后状态不变 // 合并 expanded 与 display确保数据刷新后状态不变
keys.forEach(key => { keys.forEach(key => {
@ -167,6 +179,18 @@ export default {
expanded = typeof expanded === 'undefined' ? !data.expanded : expanded; expanded = typeof expanded === 'undefined' ? !data.expanded : expanded;
treeData[id].expanded = expanded; treeData[id].expanded = expanded;
if (oldExpanded !== expanded) { if (oldExpanded !== expanded) {
if (expanded) {
if (this.states.expandRowKeys.indexOf(`${id}`) === -1) {
this.states.expandRowKeys.push(`${id}`);
}
} else {
for (let i = this.states.expandRowKeys.length - 1;i >= 0;i--) {
if (`${id}` === this.states.expandRowKeys[i]) {
this.states.expandRowKeys.splice(i, 1);
break;
}
}
}
this.table.$emit('expand-change', row, expanded); this.table.$emit('expand-change', row, expanded);
} }
this.updateTableScrollY(); this.updateTableScrollY();

View File

@ -34,6 +34,8 @@ export default Vue.extend({
// 渲染的数据来源,是对 table 中的 data 过滤排序后的结果 // 渲染的数据来源,是对 table 中的 data 过滤排序后的结果
data: [], data: [],
// 上一次数据, 比对展开状态用
lastDataKeysMap: {},
// 是否包含固定列 // 是否包含固定列
isComplex: false, isComplex: false,

View File

@ -619,7 +619,9 @@
immediate: true, immediate: true,
handler(newVal) { handler(newVal) {
if (newVal) { if (newVal) {
this.$nextTick().then(()=>{
this.store.setExpandRowKeysAdapter(newVal); this.store.setExpandRowKeysAdapter(newVal);
});
} }
} }
} }