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.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) {

View File

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

View File

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

View File

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