适用于ELment-UI级联多选框el-cascader,数据回填,根据子节点的值查找完整路径
已知子元素id,怎么获取它所有的父元素?用递归实现
/**
* 查找匹配的完整路径
* id: 匹配的值
* data: 匹配的数组数据
* prop: 匹配的字段名
*/
searchPath (id, data, prop) {
var arrRes = []
const rev = (data, nodeId) => {
for (var i = 0, length = data.length; i < length; i++) {
const node = data[i]
if (node[prop] === nodeId) {
arrRes.unshift(node[prop])
return true
} else {
if (node.children && node.children.length) {
if (rev(node.children, nodeId)) {
arrRes.unshift(node[prop])
return true
}
}
}
}
return false
}
rev(data, id)
return arrRes
},
使用方法:
allCates = this.getFathersById(item.categoryId, this.categoryOptions, 'id')