2024-01-09 13:49:21 +08:00
|
|
|
<template>
|
2024-12-13 14:36:23 +08:00
|
|
|
<div :style="{ width: menuCollapse ? '40px' : props.width }" style='transition: all 0.3s; overflow: hidden;'>
|
|
|
|
|
<Icon v-show='menuCollapse' @click='onMenuCollapse' :name="menuCollapse ? 'el-icon-Expand' : 'el-icon-Fold'"
|
|
|
|
|
:class="menuCollapse ? 'unfold' : ''" size='18' class='fold ml10 mt20 menu-collapse'
|
|
|
|
|
style='cursor: pointer' />
|
2024-06-14 16:00:23 +08:00
|
|
|
<div class='cn-tree' :style='{ opacity: menuCollapse ? 0 : 1 }'>
|
|
|
|
|
<div style='display: flex; align-items: center' class='mb10'>
|
2024-12-13 14:36:23 +08:00
|
|
|
<el-input maxlength="32" show-word-limit v-model='filterText' placeholder='请输入内容' clearable>
|
2024-01-29 13:57:52 +08:00
|
|
|
<template #prefix>
|
2024-06-14 16:00:23 +08:00
|
|
|
<Icon name='el-icon-Search' style='font-size: 16px' />
|
2024-01-29 13:57:52 +08:00
|
|
|
</template>
|
|
|
|
|
</el-input>
|
2024-12-13 14:36:23 +08:00
|
|
|
<Icon @click='onMenuCollapse' :name="menuCollapse ? 'el-icon-Expand' : 'el-icon-Fold'"
|
|
|
|
|
:class="menuCollapse ? 'unfold' : ''" size='18' class='fold ml10 menu-collapse'
|
|
|
|
|
style='cursor: pointer' v-if='props.canExpand' />
|
2024-01-29 13:57:52 +08:00
|
|
|
</div>
|
2024-12-13 14:36:23 +08:00
|
|
|
<el-tree style='flex: 1; overflow: auto;' ref='treeRef' :props='defaultProps' highlight-current
|
|
|
|
|
@check-change="checkTreeNodeChange" :filter-node-method='filterNode' node-key='id' v-bind='$attrs'>
|
2024-06-14 16:00:23 +08:00
|
|
|
<template #default='{ node, data }'>
|
|
|
|
|
<span class='custom-tree-node'>
|
2024-12-13 14:36:23 +08:00
|
|
|
<Icon :name='data.icon' style='font-size: 16px' :style='{ color: data.color }'
|
|
|
|
|
v-if='data.icon' />
|
2024-06-14 16:00:23 +08:00
|
|
|
<span style='margin-left: 4px'>{{ node.label }}</span>
|
2024-01-29 13:57:52 +08:00
|
|
|
</span>
|
|
|
|
|
</template>
|
|
|
|
|
</el-tree>
|
|
|
|
|
</div>
|
2024-01-09 13:49:21 +08:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
2024-06-14 16:00:23 +08:00
|
|
|
<script lang='ts' setup>
|
2024-01-29 13:57:52 +08:00
|
|
|
import useCurrentInstance from '@/utils/useCurrentInstance'
|
|
|
|
|
import { ElTree } from 'element-plus'
|
2024-07-03 19:31:43 +08:00
|
|
|
import { emit } from 'process';
|
2024-01-09 13:49:21 +08:00
|
|
|
import { ref, watch } from 'vue'
|
|
|
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
|
name: 'govern/tree'
|
|
|
|
|
})
|
2024-06-14 16:00:23 +08:00
|
|
|
|
2024-01-29 15:42:45 +08:00
|
|
|
interface Props {
|
|
|
|
|
width?: string
|
2024-06-14 16:00:23 +08:00
|
|
|
canExpand?: boolean
|
2024-01-29 15:42:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
2024-06-14 16:00:23 +08:00
|
|
|
width: '280px',
|
|
|
|
|
canExpand: true
|
2024-01-29 15:42:45 +08:00
|
|
|
})
|
2024-01-29 13:57:52 +08:00
|
|
|
const { proxy } = useCurrentInstance()
|
|
|
|
|
const menuCollapse = ref(false)
|
2024-01-09 13:49:21 +08:00
|
|
|
const filterText = ref('')
|
|
|
|
|
const defaultProps = {
|
|
|
|
|
label: 'name',
|
|
|
|
|
value: 'id'
|
|
|
|
|
}
|
2024-07-03 19:31:43 +08:00
|
|
|
const emit = defineEmits(['checkTreeNodeChange'])
|
2024-01-09 13:49:21 +08:00
|
|
|
watch(filterText, val => {
|
|
|
|
|
treeRef.value!.filter(val)
|
|
|
|
|
})
|
2024-01-29 13:57:52 +08:00
|
|
|
const onMenuCollapse = () => {
|
|
|
|
|
menuCollapse.value = !menuCollapse.value
|
|
|
|
|
proxy.eventBus.emit('cnTreeCollapse', menuCollapse)
|
|
|
|
|
}
|
2024-01-09 13:49:21 +08:00
|
|
|
const filterNode = (value: string, data: any) => {
|
|
|
|
|
if (!value) return true
|
|
|
|
|
return data.name.includes(value)
|
|
|
|
|
}
|
2024-12-13 14:36:23 +08:00
|
|
|
const checkTreeNodeChange = () => {
|
|
|
|
|
console.log(treeRef.value?.getCheckedNodes(), "ikkkkkiisiiisis");
|
|
|
|
|
emit('checkTreeNodeChange', treeRef.value?.getCheckedNodes())
|
2024-07-03 19:31:43 +08:00
|
|
|
}
|
2024-01-19 14:08:07 +08:00
|
|
|
const treeRef = ref<InstanceType<typeof ElTree>>()
|
2024-01-11 08:54:09 +08:00
|
|
|
defineExpose({ treeRef })
|
2024-01-09 13:49:21 +08:00
|
|
|
</script>
|
2024-01-29 13:57:52 +08:00
|
|
|
|
2024-06-14 16:00:23 +08:00
|
|
|
<style lang='scss' scoped>
|
2024-01-09 13:49:21 +08:00
|
|
|
.cn-tree {
|
|
|
|
|
flex-shrink: 0;
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
padding: 10px;
|
|
|
|
|
height: 100%;
|
2024-01-29 15:42:45 +08:00
|
|
|
width: 100%;
|
2024-06-14 16:00:23 +08:00
|
|
|
|
2024-01-09 13:49:21 +08:00
|
|
|
:deep(.el-tree) {
|
2024-01-29 13:57:52 +08:00
|
|
|
border: 1px solid var(--el-border-color);
|
2024-01-09 13:49:21 +08:00
|
|
|
}
|
2024-06-14 16:00:23 +08:00
|
|
|
|
2024-01-09 13:49:21 +08:00
|
|
|
:deep(.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content) {
|
|
|
|
|
background-color: var(--el-color-primary-light-7);
|
|
|
|
|
}
|
2024-06-14 16:00:23 +08:00
|
|
|
|
2024-01-29 13:57:52 +08:00
|
|
|
.menu-collapse {
|
|
|
|
|
color: var(--el-color-primary);
|
|
|
|
|
}
|
2024-01-09 13:49:21 +08:00
|
|
|
}
|
2024-06-14 16:00:23 +08:00
|
|
|
|
2024-01-09 13:49:21 +08:00
|
|
|
.custom-tree-node {
|
2024-10-25 14:08:42 +08:00
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
2024-01-09 13:49:21 +08:00
|
|
|
}
|
|
|
|
|
</style>
|