Tree 树组件
基于 Ant Design Vue Tree 封装的树形组件,支持增删改查、拖拽等功能。
基本用法
<template>
<BasicTree
:tree-data="treeData"
@select="handleSelect"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BasicTree } from '@/components/Tree';
const treeData = ref([
{
title: '父节点 1',
key: '1',
children: [
{ title: '子节点 1-1', key: '1-1' },
{ title: '子节点 1-2', key: '1-2' },
],
},
{
title: '父节点 2',
key: '2',
},
]);
function handleSelect(keys) {
console.log('选中的节点:', keys);
}
</script>Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| treeData | TreeDataItem[] | - | 树数据 |
| value | KeyType[] | CheckKeys | - | 勾选的节点值(v-model) |
| selectedKeys | KeyType[] | [] | 选中的节点 |
| expandedKeys | KeyType[] | [] | 展开的节点 |
| checkedKeys | CheckKeys | [] | 勾选的节点 |
| checkable | boolean | false | 是否显示复选框 |
| checkStrictly | boolean | false | 父子节点选中状态不再关联(受控模式) |
| toolbar | boolean | false | 是否显示工具栏 |
| search | boolean | false | 是否显示搜索框 |
| searchValue | string | '' | 搜索值 |
| title | string | '' | 树标题 |
| helpMessage | string | string[] | '' | 帮助信息 |
| defaultExpandLevel | number | string | '' | 默认展开层级 |
| defaultExpandAll | boolean | false | 是否默认展开所有 |
| clickRowToExpand | boolean | false | 点击行是否展开 |
| renderIcon | (params: Recordable) => string | - | 自定义渲染图标 |
| fieldNames | FieldNames | - | 指定节点 key、title、children 的字段 |
| actionList | TreeActionItem[] | [] | 操作项列表 |
| rightMenuList | ContextMenuItem[] | - | 右键菜单列表 |
| beforeRightClick | (...arg: any) => ContextMenuItem[] | ContextMenuOptions | - | 右键点击前回调 |
| filterFn | (searchValue: any, node: TreeItem, fieldNames: FieldNames) => boolean | - | 自定义过滤方法 |
| highlight | boolean | string | false | 高亮搜索值(true 用默认色值,#xxx 用指定值) |
| expandOnSearch | boolean | false | 搜索完成时自动展开结果 |
| checkOnSearch | boolean | false | 搜索完成时自动勾选所有结果 |
| selectedOnSearch | boolean | false | 搜索完成时自动选中所有结果 |
| loading | boolean | false | 是否加载中 |
Emits 事件
| 事件 | 参数 | 说明 |
|---|---|---|
| update:expandedKeys | (keys: KeyType[]) | 展开的节点变化时触发 |
| update:selectedKeys | (keys: KeyType[]) | 选中的节点变化时触发 |
| update:value | (keys: CheckKeys) | 勾选的节点值变化时触发 |
| update:searchValue | (value: string) | 搜索值变化时触发 |
| change | (keys: CheckKeys) | 勾选状态变化时触发 |
| check | (keys: CheckKeys, e) | 勾选/取消勾选时触发 |
| search | (value: string) | 点击搜索时触发 |
树数据格式
interface TreeNode {
key: string | number;
title: string;
icon?: any;
children?: TreeNode[];
disabled?: boolean;
selectable?: boolean;
// ... 其他自定义字段
}选中节点
<template>
<BasicTree
:tree-data="treeData"
:selected-keys="selectedKeys"
@select="handleSelect"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BasicTree } from '@/components/Tree';
const treeData = ref([
{ title: '节点 1', key: '1' },
{ title: '节点 2', key: '2' },
]);
const selectedKeys = ref(['1']);
function handleSelect(keys, { node }) {
selectedKeys.value = keys;
console.log('选中节点:', node);
}
</script>复选框
<template>
<BasicTree
:tree-data="treeData"
:checked-keys="checkedKeys"
:check-strictly="false"
checkable
@check="handleCheck"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BasicTree } from '@/components/Tree';
const treeData = ref([
{
title: '父节点',
key: '1',
children: [
{ title: '子节点 1', key: '1-1' },
{ title: '子节点 2', key: '1-2' },
],
},
]);
const checkedKeys = ref([]);
function handleCheck(keys, { checkedNodes }) {
checkedKeys.value = keys;
console.log('勾选的节点:', checkedNodes);
}
</script>搜索过滤
<template>
<BasicTree
:tree-data="treeData"
search
@search="handleSearch"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BasicTree } from '@/components/Tree';
const treeData = ref([
{ title: '部门 A', key: '1' },
{ title: '部门 B', key: '2' },
{ title: '部门 C', key: '3' },
]);
function handleSearch(value) {
console.log('搜索值:', value);
// 组件会自动过滤树节点
}
</script>工具栏
<template>
<BasicTree
:tree-data="treeData"
:toolbar="true"
@click="handleClick"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BasicTree, TreeActionType } from '@/components/Tree';
const treeData = ref([
{ title: '节点 1', key: '1' },
{ title: '节点 2', key: '2' },
]);
function handleClick(key) {
console.log('点击:', key);
}
</script>获取树实例
<template>
<BasicTree
ref="treeRef"
:tree-data="treeData"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BasicTree, TreeActionType } from '@/components/Tree';
const treeData = ref([
{ title: '节点 1', key: '1' },
{ title: '节点 2', key: '2' },
]);
const treeRef = ref<InstanceType<typeof BasicTree>>();
// 勾选/取消勾选所有
function checkAll(checked: boolean) {
treeRef.value?.checkAll(checked);
}
// 展开/折叠所有
function expandAll(expand: boolean) {
treeRef.value?.expandAll(expand);
}
// 设置展开的节点
function setExpandedKeys(keys: string[]) {
treeRef.value?.setExpandedKeys(keys);
}
// 获取展开的节点
function getExpandedKeys() {
return treeRef.value?.getExpandedKeys();
}
// 设置选中的节点
function setSelectedKeys(keys: string[]) {
treeRef.value?.setSelectedKeys(keys);
}
// 获取选中的节点
function getSelectedKeys() {
return treeRef.value?.getSelectedKeys();
}
// 设置勾选的节点
function setCheckedKeys(keys: CheckKeys) {
treeRef.value?.setCheckedKeys(keys);
}
// 获取勾选的节点
function getCheckedKeys() {
return treeRef.value?.getCheckedKeys();
}
// 按层级过滤
function filterByLevel(level: number) {
treeRef.value?.filterByLevel(level);
}
// 插入节点
function insertNodeByKey(options: InsertNodeParams) {
treeRef.value?.insertNodeByKey(options);
}
// 批量插入节点
function insertNodesByKey(options: InsertNodeParams) {
treeRef.value?.insertNodesByKey(options);
}
// 删除节点
function deleteNodeByKey(key: string) {
treeRef.value?.deleteNodeByKey(key);
}
// 更新节点
function updateNodeByKey(key: string, node: Omit<TreeDataItem, 'key'>) {
treeRef.value?.updateNodeByKey(key, node);
}
// 设置搜索值
function setSearchValue(value: string) {
treeRef.value?.setSearchValue(value);
}
// 获取搜索值
function getSearchValue() {
return treeRef.value?.getSearchValue();
}
// 获取选中的节点对象
function getSelectedNode(key: KeyType) {
return treeRef.value?.getSelectedNode(key);
}
</script>TreeActionType 方法定义
interface TreeActionType {
checkAll: (checkAll: boolean) => void;
expandAll: (expandAll: boolean) => void;
setExpandedKeys: (keys: KeyType[]) => void;
getExpandedKeys: () => KeyType[];
setSelectedKeys: (keys: KeyType[]) => void;
getSelectedKeys: () => KeyType[];
setCheckedKeys: (keys: CheckKeys) => void;
getCheckedKeys: () => CheckKeys;
filterByLevel: (level: number) => void;
insertNodeByKey: (opt: InsertNodeParams) => void;
insertNodesByKey: (opt: InsertNodeParams) => void;
deleteNodeByKey: (key: string) => void;
updateNodeByKey: (key: string, node: Omit<TreeDataItem, 'key'>) => void;
setSearchValue: (value: string) => void;
getSearchValue: () => string;
getSelectedNode: (key: KeyType, treeList?: TreeItem[], selectNode?: TreeItem | null) => TreeItem | null;
}异步加载
<template>
<BasicTree
:tree-data="treeData"
:load-data="loadData"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BasicTree } from '@/components/Tree';
import { defHttp } from '@/utils/http/axios';
const treeData = ref([
{ title: '根节点', key: '0', isLeaf: false },
]);
async function loadData(treeNode) {
console.log('加载子节点:', treeNode);
// 模拟异步请求
const result = await defHttp.get({
url: '/api/tree/children',
params: { parentId: treeNode.key },
});
return result.map((item) => ({
title: item.name,
key: item.id,
isLeaf: !item.hasChildren,
}));
}
</script>右键菜单
<template>
<BasicTree
:tree-data="treeData"
@right-click="handleRightClick"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BasicTree } from '@/components/Tree';
const treeData = ref([
{ title: '节点 1', key: '1' },
{ title: '节点 2', key: '2' },
]);
function handleRightClick({ event, node }) {
console.log('右键点击:', node);
// 可以结合 ContextMenu 组件显示右键菜单
}
</script>完整示例:部门树
<template>
<div class="dept-tree">
<BasicTree
ref="treeRef"
:tree-data="deptTree"
:selected-keys="selectedKeys"
:toolbar="true"
search
title="部门列表"
@select="handleSelect"
/>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { BasicTree, TreeActionType } from '@/components/Tree';
import { defHttp } from '@/utils/http/axios';
const treeRef = ref<InstanceType<typeof BasicTree>>();
const deptTree = ref([]);
const selectedKeys = ref([]);
onMounted(async () => {
await loadDeptTree();
});
async function loadDeptTree() {
const result = await defHttp.get({ url: '/system/depart/tree' });
deptTree.value = result || [];
}
function handleSelect(keys, { node }) {
selectedKeys.value = keys;
console.log('选中部门:', node);
// 可以根据选中的部门加载右侧数据
}
// 暴露方法给父组件
defineExpose({
getSelectedKeys: () => treeRef.value?.getSelectedKeys(),
expandAll: () => treeRef.value?.expandAll(true),
setSearchValue: (value: string) => treeRef.value?.setSearchValue(value),
});
</script>
<style scoped>
.dept-tree {
width: 300px;
height: 100%;
overflow: auto;
}
</style>相关组件
- [[09. BasicForm 表单]] - 表单中的树选择
- [[文档/04.开发文档/01. 前端开发/02. 组件开发/15. JSelectDept 部门选择]] - 部门树选择器
- [[文档/04.开发文档/01. 前端开发/02. 组件开发/14. ApiSelect API选择器]] - API 树选择 (ApiTreeSelect)
页面信息›
已优化是