树查询:通过子id获取所有父节点
//根据 单元id 获取选择数组
getUnitArray(list, id) {
let res = { arr: [], arrName: [], isOk: false }
list.forEach(item => {
if (item.id == id) {
res.arr.push(id)
res.isOk = true
res.arrName.push(item.riskUnitName)
return
}
if (item.children != undefined && item.children.length > 0) {
let tmp = this.getUnitArray(item.children, id)
if (tmp.isOk == true) {
res.arr = [item.id, …tmp.arr]
res.arrName = [item.riskUnitName, …tmp.arrName]
res.isOk = tmp.isOk
return
}
}
})
return res
},
methods: {
// 树查询:通过子id获取所有父节点
getTreeParent (treeData, searchId, parentAttr = ‘parentid’, childAttr = ‘sid’) {
var arrRes = [];
if (treeData.length == 0) {
if (!!searchId) {
arrRes.unshift(treeData)
}
return arrRes;
}
let rev = (data, nodeId) => {
for (var i = 0, length = data.length; i < length; i++) {
let node = data[i];
if (node[childAttr] == nodeId) {
arrRes.unshift(node)
rev(treeData, node[parentAttr])
break;
}
else {
if (!!node.children) {
rev(node.children, nodeId);
}
}
}
return arrRes;
};
arrRes = rev(treeData, searchId)
arrRes = arrRes.map(v => v[childAttr])
return arrRes;
},
原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/277084.html