tree_v2.vue
1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<script lang="ts" setup name="TreeV2">
import { ref } from 'vue'
import { ElTreeV2 } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import type { TreeNodeData, TreeNode } from 'element-plus/es/components/tree-v2/src/types'
const props = defineProps({
treeInfo: {
type: Object,
default: {}
},
})
const emits = defineEmits(['checkChange']);
const query = ref('')
const treeRef = ref<InstanceType<typeof ElTreeV2>>()
const treeHeight = ref(200)
const treeProps = computed(() => {
return props.treeInfo.props ?? {}
})
const treeData = computed(() => {
return props.treeInfo.data ?? []
})
const defaultCheckedKeys = computed(() => {
return props.treeInfo.checkedKey
})
const defaultExpandedKeys = computed(() => {
return props.treeInfo.expandedKey
})
defineExpose({
treeRef,
})
const onQueryChanged = (query: string) => {
treeRef.value!.filter(query)
}
const filterMethod = (query: string, node: TreeNode) => {
return node[treeProps.value.label]!.includes(query)
}
const checkChange = (data: TreeNodeData, checked: boolean) => {
emits('checkChange', data, checked)
}
onMounted(() => {
setTimeout(() => {
const panel = document.querySelector('.tree_panel')
treeHeight.value = panel.offsetHeight - 40
}, 200)
})
</script>
<template>
<div class="tree_panel">
<el-input v-model.trim="query" placeholder="请输入关键字" :prefix-icon="Search" clearable @input="onQueryChanged" />
<el-tree-v2 ref="treeRef" show-checkbox :data="treeData" :props="treeProps" :height="treeHeight" :item-size="32"
:filter-method="filterMethod" :expand-on-click-node="false" :default-checked-keys="defaultCheckedKeys"
:default-expanded-keys="defaultExpandedKeys" @check-change="checkChange" />
</div>
</template>
<style lang="scss" scoped>
.tree_panel {
width: 100%;
height: 100%;
min-height: 300px;
padding: 8px 0;
overflow: hidden;
.el-input {
margin: 0 8px 8px;
width: calc(100% - 16px);
}
}
</style>