Commit 5637588fce880b01137191cc82c73e0fce621e8c
1 parent
5a20df45
fix(demo): fix async tree demo, fixed: #823
Showing
1 changed file
with
24 additions
and
12 deletions
src/views/demo/tree/index.vue
@@ -27,19 +27,26 @@ | @@ -27,19 +27,26 @@ | ||
27 | /> | 27 | /> |
28 | </div> | 28 | </div> |
29 | <div class="flex"> | 29 | <div class="flex"> |
30 | - <BasicTree title="异步树" :treeData="tree" class="w-1/3" :load-data="onLoadData" /> | 30 | + <BasicTree |
31 | + title="异步树" | ||
32 | + ref="asyncTreeRef" | ||
33 | + :treeData="tree" | ||
34 | + class="w-1/3" | ||
35 | + :load-data="onLoadData" | ||
36 | + /> | ||
31 | </div> | 37 | </div> |
32 | </PageWrapper> | 38 | </PageWrapper> |
33 | </template> | 39 | </template> |
34 | <script lang="ts"> | 40 | <script lang="ts"> |
35 | - import { defineComponent, ref } from 'vue'; | ||
36 | - import { BasicTree } from '/@/components/Tree/index'; | 41 | + import { defineComponent, ref, unref } from 'vue'; |
42 | + import { BasicTree, TreeActionType } from '/@/components/Tree/index'; | ||
37 | import { treeData } from './data'; | 43 | import { treeData } from './data'; |
38 | import { PageWrapper } from '/@/components/Page'; | 44 | import { PageWrapper } from '/@/components/Page'; |
39 | 45 | ||
40 | export default defineComponent({ | 46 | export default defineComponent({ |
41 | components: { BasicTree, PageWrapper }, | 47 | components: { BasicTree, PageWrapper }, |
42 | setup() { | 48 | setup() { |
49 | + const asyncTreeRef = ref<Nullable<TreeActionType>>(null); | ||
43 | function handleCheck(checkedKeys, e) { | 50 | function handleCheck(checkedKeys, e) { |
44 | console.log('onChecked', checkedKeys, e); | 51 | console.log('onChecked', checkedKeys, e); |
45 | } | 52 | } |
@@ -57,20 +64,25 @@ | @@ -57,20 +64,25 @@ | ||
57 | return; | 64 | return; |
58 | } | 65 | } |
59 | setTimeout(() => { | 66 | setTimeout(() => { |
60 | - tree.value.forEach((v) => { | ||
61 | - if (v.key === treeNode.eventKey) { | ||
62 | - v.children = [ | ||
63 | - { title: 'Child Node', key: `${treeNode.eventKey}-0` }, | ||
64 | - { title: 'Child Node', key: `${treeNode.eventKey}-1` }, | ||
65 | - ]; | ||
66 | - } | ||
67 | - }); | 67 | + const asyncTreeAction: TreeActionType | null = unref(asyncTreeRef); |
68 | + if (asyncTreeAction) { | ||
69 | + const nodeChildren = [ | ||
70 | + { title: `Child Node ${treeNode.eventKey}-0`, key: `${treeNode.eventKey}-0` }, | ||
71 | + { title: `Child Node ${treeNode.eventKey}-1`, key: `${treeNode.eventKey}-1` }, | ||
72 | + ]; | ||
73 | + asyncTreeAction.updateNodeByKey(treeNode.eventKey, { children: nodeChildren }); | ||
74 | + asyncTreeAction.setExpandedKeys([ | ||
75 | + treeNode.eventKey, | ||
76 | + ...asyncTreeAction.getExpandedKeys(), | ||
77 | + ]); | ||
78 | + } | ||
79 | + | ||
68 | resolve(); | 80 | resolve(); |
69 | return; | 81 | return; |
70 | }, 1000); | 82 | }, 1000); |
71 | }); | 83 | }); |
72 | } | 84 | } |
73 | - return { treeData, handleCheck, tree, onLoadData }; | 85 | + return { treeData, handleCheck, tree, onLoadData, asyncTreeRef }; |
74 | }, | 86 | }, |
75 | }); | 87 | }); |
76 | </script> | 88 | </script> |