Files
admin-sjzx/src/components/tree/index.vue

163 lines
5.7 KiB
Vue
Raw Normal View History

2024-02-19 13:44:32 +08:00
<template>
<div :style="{ width: menuCollapse ? '40px' : props.width }" style="transition: all 0.3s; overflow: hidden">
2024-12-02 13:33:44 +08:00
<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" />
<div class="cn-tree" :style="{ opacity: menuCollapse ? 0 : 1 }">
<div style="display: flex; align-items: center" class="mb10">
2024-12-02 13:33:44 +08:00
<el-input v-model="filterText" placeholder="请输入内容" clearable maxlength="10" show-word-limit @input="change">
2024-02-19 13:44:32 +08:00
<template #prefix>
<Icon name="el-icon-Search" style="font-size: 16px" />
2024-02-19 13:44:32 +08:00
</template>
</el-input>
2024-12-02 13:33:44 +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" />
<el-button icon="el-icon-Plus" v-if="props.addTree" type="primary" class="ml10"
@click="onAddTree">新增</el-button>
2024-02-19 13:44:32 +08:00
</div>
2024-12-02 13:33:44 +08:00
<el-tree style="flex: 1; overflow: auto" ref="treeRef" :props="defaultProps" highlight-current
:filter-node-method="filterNode" node-key="id" v-bind="$attrs">
<template #default="{ node, data }">
<span class="custom-tree-node">
2024-12-02 13:33:44 +08:00
<Icon :name="data.icon" style="font-size: 16px" :style="{ color: data.color }"
v-if="data.icon" />
<span style="margin-left: 4px">{{ node.label }}</span>
2024-02-19 13:44:32 +08:00
</span>
</template>
</el-tree>
</div>
</div>
</template>
<script lang="ts" setup>
2024-02-19 13:44:32 +08:00
import useCurrentInstance from '@/utils/useCurrentInstance'
import { ElTree } from 'element-plus'
import { ref, watch } from 'vue'
2024-12-02 13:33:44 +08:00
import { ElMessage } from 'element-plus'
2024-02-19 13:44:32 +08:00
defineOptions({
name: 'govern/tree'
})
const emit = defineEmits(['onAddTree'])
2024-02-19 13:44:32 +08:00
interface Props {
width?: string
2024-02-21 16:41:40 +08:00
canExpand?: boolean
addTree?: boolean
2024-02-19 13:44:32 +08:00
}
const props = withDefaults(defineProps<Props>(), {
2024-02-21 16:41:40 +08:00
width: '280px',
canExpand: true,
addTree: false
2024-02-19 13:44:32 +08:00
})
const { proxy } = useCurrentInstance()
const menuCollapse = ref(false)
const filterText = ref('')
const defaultProps = {
label: 'name',
value: 'id'
}
2024-12-02 13:33:44 +08:00
const specialCharsPattern = /[`~!@$%^&*\-+=<>?:"{}|,.\/;'\\[\]·~@¥%……&*()——\-+={}|《》?:“”【】、;‘’,。、~]/g;
const change=(val) => {
if (specialCharsPattern.test(val)) {
ElMessage.warning('禁止输入特殊字符!')
filterText.value = val.replace(/[`~!@$%^&*\-+=<>?:"{}|,.\/;'\\[\]·~@¥%……&*()——\-+={}|《》?:“”【】、;‘’,。、~]/g, "")
console.log("🚀 ~ change ~ filterText.value:", filterText.value)
treeRef.value!.filter(filterText.value)
}else{
treeRef.value!.filter(filterText.value)
}
}
// watch(filterText, val => {
// console.log("🚀 ~ val:", specialCharsPattern.test(val))
// if (specialCharsPattern.test(val)) {
// ElMessage.warning('禁止输入特殊字符!')
// filterText.value = val.replace(/[`~!@$%^&*\-+=<>?:"{}|,.\/;'\\[\]·~@¥%……&*()——\-+={}|《》?:“”【】、;‘’,。、~]/g, "")
// console.log("🚀 ~ filterText.value:", filterText.value)
// treeRef.value!.filter(filterText.value)
// }else{
// treeRef.value!.filter(filterText.value)
// }
// })
2024-02-19 13:44:32 +08:00
const onMenuCollapse = () => {
menuCollapse.value = !menuCollapse.value
proxy.eventBus.emit('cnTreeCollapse', menuCollapse)
}
2024-11-21 11:36:36 +08:00
const filterNode = (value: string, data: any, node: any) => {
2024-02-19 13:44:32 +08:00
if (!value) return true
2024-11-21 11:36:36 +08:00
// return data.name.includes(value)
if (data.name) {
2024-12-02 13:33:44 +08:00
2024-11-21 11:36:36 +08:00
return chooseNode(value, data, node)
}
}
2024-12-02 13:33:44 +08:00
2024-11-21 11:36:36 +08:00
// 过滤父节点 / 子节点 (如果输入的参数是父节点且能匹配则返回该节点以及其下的所有子节点如果参数是子节点则返回该节点的父节点。name是中文字符enName是英文字符.
const chooseNode = (value: string, data: any, node: any) => {
if (data.name.indexOf(value) !== -1) {
return true
}
const level = node.level
// 如果传入的节点本身就是一级节点就不用校验了
if (level === 1) {
return false
}
// 先取当前节点的父节点
let parentData = node.parent
// 遍历当前节点的父节点
let index = 0
while (index < level - 1) {
// 如果匹配到直接返回此处name值是中文字符enName是英文字符。判断匹配中英文过滤
if (parentData.data.name.indexOf(value) !== -1) {
return true
}
// 否则的话再往上一层做匹配
parentData = parentData.parent
index++
}
// 没匹配到返回false
return false
2024-02-19 13:44:32 +08:00
}
// 添加树
const onAddTree = () => {
emit('onAddTree')
}
2024-02-19 13:44:32 +08:00
const treeRef = ref<InstanceType<typeof ElTree>>()
defineExpose({ treeRef })
</script>
<style lang="scss" scoped>
2024-02-19 13:44:32 +08:00
.cn-tree {
flex-shrink: 0;
display: flex;
flex-direction: column;
box-sizing: border-box;
padding: 10px;
height: 100%;
width: 100%;
2024-02-22 14:51:05 +08:00
2024-02-19 13:44:32 +08:00
:deep(.el-tree) {
border: 1px solid var(--el-border-color);
}
2024-02-22 14:51:05 +08:00
2024-02-19 13:44:32 +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-02-22 14:51:05 +08:00
2024-02-19 13:44:32 +08:00
.menu-collapse {
color: var(--el-color-primary);
}
}
2024-02-22 14:51:05 +08:00
2024-02-19 13:44:32 +08:00
.custom-tree-node {
display: flex;
align-items: center;
}
</style>