实现树的搜索
通过关键字模糊搜索,将其他不涉及的分支移除 子节点符合规则,孙节点不符合规则,剔除孙节点,显示子节点以上的节点 搜索节点以上字根节点
图形演示大概逻辑export default {
components: {
treeNode
},
name: 'tree_s',
data () {
return {
treeData: [
{
title: '0',
id: 1,
parentId: 0,
children: [
{
title: '0-0',
id: 2,
parentId: 1,
children: [
{
title: '0-0-0',
id: 3,
parentId: 2,
children: []
},
{
title: '枣1',
id: 4,
parentId: 2,
children: [
{
title: '红',
id: 5,
parentId: 4,
children: [
{
title: '红枣',
id: 7,
parentId: 5,
children: []
}
]
},
{
title: '小红',
id: 6,
parentId: 4,
children: []
}
]
}
]
}
]
},
{
title: '1',
id: 8,
parentId: 0,
children: [
{
title: '778',
id: 9,
parentId: 8,
children: []
}
]
},
{
title: '2',
id: 10,
parentId: 0,
children: [
{
title: '枣枣',
id: 11,
parentId: 10,
children: [
{
title: '456',
id: 12,
parentId: 11,
children: []
}
]
}
]
}
],
filterTree: [],
filterValue: ''
}
},
watch: {
filterValue: function (val) {
if (val === '') {
this.filterTree = this.treeData
} else {
this.handelTree(val)
}
}
},
methods: {
handelTree (val) {
// 将树扁平化 同时查找包含关键字的节点
const delayering = this.delayering(val)
const treeNode = delayering.treeNode
const likeNode = delayering.likeNode
const resultTree = []
const resultTreeObj = {}
const childNodeObj = {}
// 将查找出来的节点逆推
function f (node) {
if (node.parentId === 0) {
if (!resultTreeObj[node.id]) {
resultTree.push(node)
resultTreeObj[node.id] = true
}
} else {
if (!childNodeObj[node.id]) {
treeNode[node.parentId].children.push(node)
childNodeObj[node.id] = true
}
f(treeNode[node.parentId])
}
}
// 把根节点每一项代入
for (let i = 0; i
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?