From b14f45006c19ff8cb252a21e071e1c7c406d500c Mon Sep 17 00:00:00 2001 From: Alan Wang <948467222@qq.com> Date: Sun, 12 Sep 2021 18:00:22 +0800 Subject: [PATCH] fix: [el-table] default-expand-all doesn't work (#3321) close #2776 --- packages/components/table/src/store/index.ts | 3 +++ packages/components/table/src/store/tree.ts | 21 ++++++++++++++----- .../components/table/src/store/watcher.ts | 2 ++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/components/table/src/store/index.ts b/packages/components/table/src/store/index.ts index 71a7de0eef..34ab83bc98 100644 --- a/packages/components/table/src/store/index.ts +++ b/packages/components/table/src/store/index.ts @@ -48,6 +48,9 @@ function useStore() { // 没有使用 computed,而是手动更新部分数据 https://github.com/vuejs/vue/issues/6660#issuecomment-331417140 instance.store.updateCurrentRowData() instance.store.updateExpandRows() + instance.store.updateTreeData( + instance.store.states.defaultExpandAll.value + ) if (unref(states.reserveSelection)) { instance.store.assertRowKey() instance.store.updateSelectionByRowKey() diff --git a/packages/components/table/src/store/tree.ts b/packages/components/table/src/store/tree.ts index bb08f6d245..076c3e1acb 100644 --- a/packages/components/table/src/store/tree.ts +++ b/packages/components/table/src/store/tree.ts @@ -66,18 +66,19 @@ function useTree(watcherData: WatcherPropsData) { return res } - const updateTreeData = () => { + const updateTreeData = ( + ifExpandAll = instance.store?.states.defaultExpandAll.value + ) => { const nested = normalizedData.value const normalizedLazyNode_ = normalizedLazyNode.value const keys = Object.keys(nested) const newTreeData = {} if (keys.length) { const oldTreeData = unref(treeData) - const defaultExpandAll = instance.store?.states.defaultExpandAll.value const rootLazyRowKeys = [] const getExpanded = (oldValue, key) => { const included = - defaultExpandAll || + ifExpandAll || (expandRowKeys.value && expandRowKeys.value.indexOf(key) !== -1) return !!((oldValue && oldValue.expanded) || included) } @@ -124,8 +125,18 @@ function useTree(watcherData: WatcherPropsData) { instance.store?.updateTableScrollY() } - watch(() => normalizedData.value, updateTreeData) - watch(() => normalizedLazyNode.value, updateTreeData) + watch( + () => normalizedData.value, + () => { + updateTreeData() + } + ) + watch( + () => normalizedLazyNode.value, + () => { + updateTreeData() + } + ) const updateTreeExpandKeys = (value: string[]) => { expandRowKeys.value = value diff --git a/packages/components/table/src/store/watcher.ts b/packages/components/table/src/store/watcher.ts index 70c0811469..e55e2f7768 100644 --- a/packages/components/table/src/store/watcher.ts +++ b/packages/components/table/src/store/watcher.ts @@ -430,6 +430,7 @@ function useWatcher() { const { updateTreeExpandKeys, toggleTreeExpansion, + updateTreeData, loadOrToggle, states: treeStates, } = useTree({ @@ -490,6 +491,7 @@ function useWatcher() { updateExpandRows, updateCurrentRowData, loadOrToggle, + updateTreeData, states: { rowKey, data,