1、树形表格,通过指定行的唯一值id。找到其所有父级节点对象(不包含id当前节点)
getRowAndParentsById(tree, targetId) {
let result = []
let parentFound = false
function search(treeData, parentId) {
for (const node of treeData) {
if (node.value === targetId) {
if (parentId !== null) {
parentFound = true
}
break
} else if (node.children) {
search(node.children, node.value)
if (result.length > 0 || parentFound) {
result.push(node)
break
}
}
}
}
search(tree, null)
return result
},
如果还想获取到id所在的当前节点。 只需要将黄色删除、红色行修改为:
result.push(node)
在上面基础上展开到指定行:
const rowKeyToExpand = this.getRowAndParentsById(this.treeData, targetValue.value)
rowKeyToExpand.forEach((element) => {
this.$nextTick(() => {
this.$refs['treeData'].toggleRowExpansion(element, true)
})
})