1、根据子级标识获取其所在的对象
this.getTreeObjByUrl(嵌套数组data, url);
getTreeObjByUrl(treeData, url) {
for (let item of treeData) {
if (item.children && item.url != url) {
this.getTreeObjByUrl(item.children, url);
} else if (item.url == url) {
this.preUrlObj = item;
return item;
}
}
},
2、根据子级找到父级
let urlArr = this.bindFindKeys(
treeData,
id
);
bindFindKeys(list, id) {
if (!list || !id) {
return '';
}
let arr = [];
let findParent = (data, nodeId, parentId) => {
for (var i = 0, length = data.length; i < length; i++) {
let node = data[i];
if (node.permissionId === nodeId) {
arr.unshift(node);
if (nodeId === list[0].permissionId) {
break;
}
findParent(list, parentId);
break;
} else {
if (node.children) {
findParent(node.children, nodeId, node.permissionId);
}
continue;
}
}
return arr;
};
return findParent(list, id);
},
3、获取指定层级
export const getAdTreeByCount = (tree, level) => {
var newArr = [];
for (var i = 0; i < tree.length; i++) {
var item = tree[i];
if (level === 1) {
newArr.push({
label: item.label,
lat: item.lat,
level: item.level,
lon: item.lon,
parentCode: item.parentCode,
value: item.value,
leaf: item.leaf,
children: []
});
break;
}
if (item.level > level) {
tree.splice(i--, 1);
} else {
if (item.children) {
item.children = getAdTreeByCount(item.children, level);
}
if (item.level == level) {
item.leaf = true;
item.children = null;
}
newArr.push(item);
}
}
return newArr;
};