|
1
|
<template>
|
vben
authored
|
2
|
<PageWrapper title="Tree函数操作示例" contentBackground contentClass="p-4">
|
|
3
|
<div class="mb-4">
|
Vben
authored
|
4
5
6
7
|
<a-button @click="expandAll(true)" class="mr-2"> 展开全部 </a-button>
<a-button @click="expandAll(false)" class="mr-2"> 折叠全部 </a-button>
<a-button @click="checkAll(true)" class="mr-2"> 全选 </a-button>
<a-button @click="checkAll(false)" class="mr-2"> 全不选 </a-button>
|
vben
authored
|
8
9
|
<a-button @click="handleLevel(2)" class="mr-2"> 显示到第2级 </a-button>
<a-button @click="handleLevel(1)" class="mr-2"> 显示到第1级 </a-button>
|
Vben
authored
|
10
11
|
</div>
<div class="mb-4">
|
vben
authored
|
12
13
14
15
|
<a-button @click="handleSetCheckData" class="mr-2"> 设置勾选数据 </a-button>
<a-button @click="handleGetCheckData" class="mr-2"> 获取勾选数据 </a-button>
<a-button @click="handleSetSelectData" class="mr-2"> 设置选中数据 </a-button>
<a-button @click="handleGetSelectData" class="mr-2"> 获取选中数据 </a-button>
|
|
16
|
<a-button @click="handleGetSelectNode" class="mr-2"> 获取选中节点 </a-button>
|
|
17
|
|
vben
authored
|
18
19
|
<a-button @click="handleSetExpandData" class="mr-2"> 设置展开数据 </a-button>
<a-button @click="handleGetExpandData" class="mr-2"> 获取展开数据 </a-button>
|
|
20
21
|
</div>
<div class="mb-4">
|
vben
authored
|
22
23
24
25
|
<a-button @click="appendNodeByKey(null)" class="mr-2"> 添加根节点 </a-button>
<a-button @click="appendNodeByKey('2-2')" class="mr-2"> 添加在parent3内添加节点 </a-button>
<a-button @click="deleteNodeByKey('2-2')" class="mr-2"> 删除parent3节点 </a-button>
<a-button @click="updateNodeByKey('1-1')" class="mr-2"> 更新parent2节点 </a-button>
|
|
26
|
</div>
|
Vben
authored
|
27
|
<BasicTree :treeData="treeData" title="函数操作" ref="treeRef" :checkable="true" />
|
vben
authored
|
28
|
</PageWrapper>
|
|
29
30
31
32
33
34
|
</template>
<script lang="ts">
import { defineComponent, ref, unref } from 'vue';
import { BasicTree, TreeActionType } from '/@/components/Tree/index';
import { treeData } from './data';
import { useMessage } from '/@/hooks/web/useMessage';
|
vben
authored
|
35
|
import { PageWrapper } from '/@/components/Page';
|
|
36
|
import { type Nullable } from '@vben/types';
|
|
37
38
|
export default defineComponent({
|
Vben
authored
|
39
|
components: { BasicTree, PageWrapper },
|
|
40
|
setup() {
|
vben
authored
|
41
|
const treeRef = ref<Nullable<TreeActionType>>(null);
|
|
42
|
const { createMessage } = useMessage();
|
Vben
authored
|
43
|
|
|
44
45
46
47
48
|
function getTree() {
const tree = unref(treeRef);
if (!tree) {
throw new Error('tree is null!');
}
|
vben
authored
|
49
|
return tree;
|
|
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
}
function handleLevel(level: number) {
getTree().filterByLevel(level);
}
function handleSetCheckData() {
getTree().setCheckedKeys(['0-0']);
}
function handleGetCheckData() {
const keys = getTree().getCheckedKeys();
createMessage.success(JSON.stringify(keys));
}
function handleSetSelectData() {
getTree().setSelectedKeys(['0-0']);
}
function handleGetSelectData() {
const keys = getTree().getSelectedKeys();
createMessage.success(JSON.stringify(keys));
}
|
|
74
75
76
77
78
79
|
function handleGetSelectNode() {
const keys = getTree().getSelectedKeys();
const node = getTree().getSelectedNode(keys[0]);
createMessage.success(node !== null ? JSON.stringify(node) : null);
}
|
|
80
81
82
83
84
85
86
87
88
|
function handleSetExpandData() {
getTree().setExpandedKeys(['0-0']);
}
function handleGetExpandData() {
const keys = getTree().getExpandedKeys();
createMessage.success(JSON.stringify(keys));
}
|
Vben
authored
|
89
90
91
92
93
94
95
96
|
function checkAll(checkAll: boolean) {
getTree().checkAll(checkAll);
}
function expandAll(checkAll: boolean) {
getTree().expandAll(checkAll);
}
|
|
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
function appendNodeByKey(parentKey: string | null = null) {
getTree().insertNodeByKey({
parentKey: parentKey,
node: {
title: '新增节点',
key: '2-2-2',
},
// 往后插入
push: 'push',
// 往前插入
// push:'unshift'
});
}
function deleteNodeByKey(key: string) {
getTree().deleteNodeByKey(key);
}
function updateNodeByKey(key: string) {
getTree().updateNodeByKey(key, {
title: 'parent2-new',
});
}
return {
treeData,
treeRef,
handleLevel,
handleSetCheckData,
handleGetCheckData,
handleSetSelectData,
handleGetSelectData,
handleSetExpandData,
handleGetExpandData,
|
|
131
|
handleGetSelectNode,
|
|
132
133
134
|
appendNodeByKey,
deleteNodeByKey,
updateNodeByKey,
|
Vben
authored
|
135
136
|
checkAll,
expandAll,
|
|
137
138
139
140
|
};
},
});
</script>
|