【组件】前端ElementUi 下拉Tree树形组件 带模糊搜索自动展开高亮功能
【组件】前端ElementUi 下拉Tree树形组件 带模糊搜索自动展开高亮功能
https://live.csdn.net/v/435737
<template>
<div>
<el-popover
style="overflow-y: auto; "
placement="bottom"
trigger="click">
<el-input
style="margin-bottom: 10px"
v-model="搜索内容"
clearable
@clear="searchHandleIptClear"
>
<el-button slot="append" icon="el-icon-search" @click="search" size="small" >搜索</el-button>
</el-input>
<!-- 树结构-->
<el-tree
style="width:auto"
ref="tree"
:props="props"
:data="data"
:check-strictly="false"
@check-change="handleCheckChange"
show-checkbox
node-key="id"
:default-expanded-keys="需要展开的节点keys"
:default-checked-keys="[]"
@node-click="handleNodeClick"
>
<span class="custom-tree-node" slot-scope="{ node, data }">
<!-- 高亮的部分-->
<span v-if="需要高亮节点数组.indexOf(data.id) !== -1" style="background-color: yellow">{{ node.label }}</span>
<!-- 不需要高亮的部分-->
<span v-else>{{ node.label }}</span>
</span>
</el-tree>
<!-- 输入框 用来显示选中的节点内容-->
<el-input slot="reference"
style="width:380px"
v-model="value.label"
placeholder="节点"
clearable
readonly
@clear="handleIptClear">
</el-input>
</el-popover>
</div>
</template>
<script>
export default {
data() {
return {
data: [{
id: 1,
label: '一级 1 A',
children: [{
id: 4,
label: '二级 1-1 张三B',
children: [{
id: 9,
label: '三级 1-1-1 李四C'
}, {
id: 10,
label: '三级 1-1-2 王五a'
}]
}]
}, {
id: 2,
label: '一级 2 赵六c',
children: [{
id: 5,
label: '二级 2-1 张三b'
}, {
id: 6,
label: '二级 2-2 李四a'
}]
}, {
id: 3,
label: '一级 3 王五D',
children: [{
id: 7,
label: '二级 3-1 赵六d'
}, {
id: 8,
label: '二级 3-2 钱七a'
}]
}],
props: {
label: 'label',
children: 'children'
},
count: 1,
value:{id:'', label: ''},
需要高亮节点数组:[],
需要展开的节点keys:[],
搜索内容:''
};
},
methods: {
searchHandleIptClear(){
this.需要高亮节点数组 = []
},
高亮模糊查询的节点递归(arr, 父节点){
if(!arr || arr.length <= 0){
return
}
let that = this
arr.forEach(item=>{
//不区分大小写匹配
if(item.label.toUpperCase().indexOf(that.搜索内容.toUpperCase()) !== -1){
console.log('搜索内容匹配', that.搜索内容, item, 父节点)
that.需要高亮节点数组.push(item.id);
if(父节点){
that.需要展开的节点keys.push(父节点.id)
}
}
if(item.children){
that.高亮模糊查询的节点递归(item.children, item)
}
})
},
/**
* 点击搜索
*/
search(){
let that = this
//1 过滤所有内容
that.需要高亮节点数组 = []
that.需要展开的节点keys = []
if(that.搜索内容){
this.高亮模糊查询的节点递归(this.data, null)
}
//3 展开匹配到的节点内容
},
// 清空输入框内容
handleIptClear(){
console.log('清空输入框内容')
//清空选中内容
this.$refs.tree.setCheckedNodes([])
this.value.id = ''
this.value.label = ''
},
/**
* 更新被选中的值
*/
updateCheck(){
const seltedNodes = this.$refs.tree.getCheckedNodes()
console.log(seltedNodes)
const ids = seltedNodes.map(n => n.id)
const labels = seltedNodes.map(n => n.label)
this.value.id = ids + ''
this.value.label = labels + ''
},
// checkbox被选中或取消选中
handleCheckChange(arg1, arg2, arg3){
console.log(arg1, arg2, arg3);
this.updateCheck()
},
// 节点被点击
handleNodeClick(arg1, arg2, arg3){
console.log('nodes:', arg1, arg2, arg3)
this.updateCheck()
},
}
};
</script>
原文地址:https://blog.csdn.net/G971005287W/article/details/144024897
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!