|
1
|
<template>
|
vben
authored
|
2
|
<PageWrapper title="Tree基础示例">
|
|
3
4
|
<Row :gutter="[16, 16]">
<Col :span="8">
|
vben
authored
|
5
6
7
|
<BasicTree title="基础示例,默认展开第一层" :treeData="treeData" defaultExpandLevel="1">
<template #title> 123123 </template>
</BasicTree>
|
|
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
</Col>
<Col :span="8">
<BasicTree
title="可勾选,默认全部展开"
:treeData="treeData"
:checkable="true"
defaultExpandAll
@check="handleCheck"
/>
</Col>
<Col :span="8">
<BasicTree
title="指定默认展开/勾选示例"
:treeData="treeData"
:checkable="true"
:expandedKeys="['0-0']"
:checkedKeys="['0-0']"
/>
</Col>
<Col :span="8">
<BasicTree
title="懒加载异步树"
ref="asyncTreeRef"
:treeData="tree"
:load-data="onLoadData"
/>
</Col>
|
|
35
|
<Col :span="8">
|
|
36
37
38
39
40
41
42
43
44
|
<Card title="异步数据,默认展开">
<template #extra>
<a-button @click="loadTreeData" :loading="treeLoading">加载数据</a-button>
</template>
<Spin :spinning="treeLoading">
<BasicTree ref="asyncExpandTreeRef" :treeData="tree2" />
</Spin>
</Card>
</Col>
|
|
45
46
47
48
49
50
51
52
|
<Col :span="8">
<Card title="BasicTree内置加载">
<template #extra>
<a-button @click="loadTreeData2" :loading="treeLoading">请求数据</a-button>
</template>
<BasicTree ref="loadTreeRef" :treeData="tree2" :loading="treeLoading" />
</Card>
</Col>
|
|
53
|
</Row>
|
vben
authored
|
54
|
</PageWrapper>
|
|
55
56
|
</template>
<script lang="ts">
|
|
57
58
|
import { defineComponent, nextTick, ref, unref } from 'vue';
import { BasicTree, TreeActionType, TreeItem } from '/@/components/Tree/index';
|
|
59
|
import { treeData } from './data';
|
vben
authored
|
60
|
import { PageWrapper } from '/@/components/Page';
|
|
61
|
import { Card, Row, Col, Spin } from 'ant-design-vue';
|
Arvin
authored
|
62
63
|
import { cloneDeep, uniq } from 'lodash-es';
import { isArray } from '/@/utils/is';
|
|
64
65
|
export default defineComponent({
|
|
66
|
components: { BasicTree, PageWrapper, Card, Row, Col, Spin },
|
|
67
|
setup() {
|
|
68
|
const asyncTreeRef = ref<Nullable<TreeActionType>>(null);
|
|
69
|
const asyncExpandTreeRef = ref<Nullable<TreeActionType>>(null);
|
|
70
|
const loadTreeRef = ref<Nullable<TreeActionType>>(null);
|
|
71
72
73
|
const tree2 = ref<TreeItem[]>([]);
const treeLoading = ref(false);
|
|
74
75
76
|
function handleCheck(checkedKeys, e) {
console.log('onChecked', checkedKeys, e);
}
|
|
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
function loadTreeData() {
treeLoading.value = true;
// 以下是模拟异步获取数据
setTimeout(() => {
// 设置数据源
tree2.value = cloneDeep(treeData);
treeLoading.value = false;
// 展开全部
nextTick(() => {
console.log(unref(asyncExpandTreeRef));
unref(asyncExpandTreeRef)?.expandAll(true);
});
}, 2000);
}
|
|
92
93
94
95
96
97
98
99
100
|
function loadTreeData2() {
treeLoading.value = true;
// 以下是模拟异步获取数据
setTimeout(() => {
// 设置数据源
tree2.value = cloneDeep(treeData);
treeLoading.value = false;
}, 2000);
}
|
|
101
|
|
|
102
103
104
105
106
107
108
109
110
|
const tree = ref([
{
title: 'parent ',
key: '0-0',
},
]);
function onLoadData(treeNode) {
return new Promise((resolve: (value?: unknown) => void) => {
|
Arvin
authored
|
111
|
if (isArray(treeNode.children) && treeNode.children.length > 0) {
|
|
112
113
114
115
|
resolve();
return;
}
setTimeout(() => {
|
|
116
117
118
119
120
121
122
|
const asyncTreeAction: TreeActionType | null = unref(asyncTreeRef);
if (asyncTreeAction) {
const nodeChildren = [
{ title: `Child Node ${treeNode.eventKey}-0`, key: `${treeNode.eventKey}-0` },
{ title: `Child Node ${treeNode.eventKey}-1`, key: `${treeNode.eventKey}-1` },
];
asyncTreeAction.updateNodeByKey(treeNode.eventKey, { children: nodeChildren });
|
Arvin
authored
|
123
124
125
|
asyncTreeAction.setExpandedKeys(
uniq([treeNode.eventKey, ...asyncTreeAction.getExpandedKeys()]),
);
|
|
126
127
|
}
|
|
128
129
|
resolve();
return;
|
Arvin
authored
|
130
|
}, 300);
|
|
131
132
|
});
}
|
|
133
134
135
136
137
138
139
|
return {
treeData,
handleCheck,
tree,
onLoadData,
asyncTreeRef,
asyncExpandTreeRef,
|
|
140
|
loadTreeRef,
|
|
141
142
143
|
tree2,
loadTreeData,
treeLoading,
|
|
144
|
loadTreeData2,
|
|
145
|
};
|
|
146
147
148
|
},
});
</script>
|