vue elementUI tree 搜索显示节点和所有子节点(
2023-03-08 本文已影响0人
八妹sss
<el-input
placeholder="输入关键字进行过滤"
v-model="filterText">
</el-input>
<el-tree
class="filter-tree"
:data="data"
:props="defaultProps"
default-expand-all
:filter-node-method="filterNode"
ref="tree">
</el-tree>
<script>
export default {
watch: {
filterText(val) {
this.$refs.tree.filter(val);
}
},
methods: {
filterNode (value, data, node) {
return this.filterNodeAndParent(value, data, node)
},
// 递归检测父元素或自己是否符合条件
filterNodeAndParent (value, data, node) {
if (node.level === 1) {
return node.data.label.indexOf(value) !== -1
} else if (data.level !== 1) {
// 判断自己是否符合条件
if (node.data.label.indexOf(value) !== -1) {
return true
} else { // 否则查找父级
return this.filterNodeAndParent(value, node.data, node.parent)
}
}
}
},
data() {
return {
filterText: '',
data: [{
id: 1,
label: '一级 1',
children: [{
id: 4,
label: '二级 1-1',
children: [{
id: 9,
label: '三级 1-1-1'
}, {
id: 10,
label: '三级 1-1-2'
}]
}]
}, {
id: 2,
label: '一级 2',
children: [{
id: 5,
label: '二级 2-1'
}, {
id: 6,
label: '二级 2-2'
}]
}, {
id: 3,
label: '一级 3',
children: [{
id: 7,
label: '二级 3-1'
}, {
id: 8,
label: '二级 3-2'
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
};
}
};
</script>