<template>
<a-spin :spinning="loading">
<div class="tree-container">
<a-input-search v-model:value="searchValue" style="margin-bottom: 8px" placeholder="请输入" />
<a-tree v-model:expandedKeys="expandedKeys" v-model:auto-expand-parent="autoExpandParent"
v-model:selectedKeys="selectedKeys" :showIcon="true" :checkable="false" v-if="treeData" :tree-data="treeData"
@click.stop="clickTree" :field-names="{ title: 'name', key: 'id' }">
<template #title="{ children, name, id, level }">
<div class="flex f-between">
<div class="mr-30">
<span v-if="name.indexOf(searchValue) > -1">
{{ name.substring(0, name.indexOf(searchValue)) }}
<span style="color: var(--main-color)">{{ searchValue }}</span>
{{ name.substring(name.indexOf(searchValue) + searchValue.length) }}
</span>
<span v-else>{{ name }}</span>
</div>
<div style="display: inline-block;">
<a-dropdown trigger="click">
<span> ···</span>
<template #overlay>
<a-menu @click="(item, key) => onMenuClick(item)">
<a-menu-item v-for="item in operateList(level)" :key="item.value" :label="item.label"
:string="item.orgString">{{ item.label
}}</a-menu-item>
</a-menu>
</template>
</a-dropdown>
</div>
</div>
</template>
</a-tree>
</div>
</a-spin>
</template>
<script setup lang="ts">
import { ref, reactive, toRefs, onMounted, onBeforeUnmount, watch } from 'vue'
import { useStore } from '@/store/index'
import emitter from '@/utils/eventBus';
import { getdepartmentList } from '@/request/department'
import { message, type TreeProps } from 'ant-design-vue';
const emit = defineEmits(['openDrawer', 'update:open'])
const store = useStore()
const operateList = (level) => {
let add = { label: '新增子部门', value: '11', orgString: 'addEditDepartment', }
let otherOperate = [
{ label: '编辑部门', value: '12', orgString: 'addEditDepartment', },
{ label: '移动部门和所有人', value: '13', orgString: 'moveDepartment', },
{ label: '仅移动人员', value: '14', orgString: 'movePerson', },
{ label: '删除', value: '15', orgString: 'deleteOrganization', },
]
return level < 3 ? [add, ...otherOperate] : otherOperate
}
const searchValue = ref('')
const treeData = ref()
const selectedKeys = ref<string[]>([]);
// const checkedKeys = ref<string[]>(['2','5']);
const onMenuClick = (item) => {
selectTreeInfo.value.operateTitle = item.item.label
selectTreeInfo.value.operateKey = item.key
selectTreeInfo.value.orgComponent = item.item.string
emit('openDrawer', selectTreeInfo.value)
}
const loading = ref(false)
let selectTreeInfo: any = ref({})
const clickTree = (data, info) => {
selectTreeInfo.value = info
emit('openDrawer', selectTreeInfo.value)
}
// 获取列表
const getDepartment = () => {
loading.value = true
getdepartmentList().then((res: any) => {
if (res.code === 200) {
treeData.value = res.data
expandedKeys.value = [res.data[0].id] // 默认展开第一级部门
generateList(res.data)
} else {
message.error(res.message)
}
loading.value = false
}).catch((err) => {
loading.value = false
})
}
getDepartment()
defineExpose({ getDepartment })
const dataList = [];
const getParentKey = (key, tree,) => {
let parentKey;
for (let i = 0; i < tree.length; i++) {
const node = tree[i];
if (node.children) {
if (node.children.some(item => item.id === key)) {
parentKey = node.id;
} else if (getParentKey(key, node.children)) {
parentKey = getParentKey(key, node.children);
}
}
}
return parentKey;
};
const generateList = (data) => {
for (let i = 0; i < data.length; i++) {
const node = data[i];
const key = node.id;
dataList.push({ key, title: node.name });
if (node.children) {
generateList(node.children);
}
}
};
const expandedKeys = ref([]);
const autoExpandParent = ref(true);
watch(() => searchValue.value, value => {
const expanded = dataList
.map((item) => {
if (item.title.indexOf(value) > -1) {
return getParentKey(item.key, treeData.value);
}
return null;
})
.filter((item, i, self) => item && self.indexOf(item) === i);
expandedKeys.value = expanded;
});
</script>
<style scoped lang="less">
.tree-container {
padding: 20px;
height: calc(100vh - 100px);
min-width: 300px;
border: 1px solid #ccc;
overflow: auto;
box-shadow: 1px 1px 10px #ccc;
}
</style>
2024-12-17 树形图带查询功能
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近接到需求把两个关联的表格整改成树形结构图,如上图所示。需要满足的要求:1.勾选父级,子集不勾选。2.子集显示默...
- Tree View; Mind map; Think map; tree map; 树状图;思维导图;组织结构图;...
- 儿童思维训练课系列,受用一生的思维习惯 连载文章目录 (点击蓝字可阅读前两篇微课笔记) 1.圆圈图 2.流程图和气...
- 《八大逻辑图》中介绍,树形图是训练分类能力的工具,也是进行思维训练的重要工具之一。因为分类在我们的生活、学习和工作...