setTreeRecursion(data) {
data.map((item,index) => {
item.scopedSlots = { title: 'customNode'};
if(item.children.length != 0){
this.setTreeRecursion(item.children);
}
});
return data;
}
setTreeRecursion('树结构数据')
queryNodesTotal(data, num = 0){
let forFn = (nodes) => {
for(let i = 0; i < nodes.length; i++){
num++;
if(nodes[i].children){
forFn(nodes[i].children);
}
}
}
forFn(data);
return num;
}
queryNodesTotal('树结构数据')
getLastChildNode(node, temp = []) {
let forFn = (arr) => {
for (let i = 0; i < arr.length; i++) {
if (arr[i].children.length === 0) {
temp.push(arr[i]);
} else {
forFn(arr[i].children)
}
}
}
forFn(node)
return temp;
}
getLastChildNode('树结构数据')
getLastChildNode(node, temp = []) {
let forFn = (arr) => {
for (let i = 0; i < arr.length; i++) {
if (arr[i].children.length === 0) {
temp.push(arr);
arr = [];
} else {
forFn(arr[i].children)
}
}
}
forFn(node)
return temp;
}
getLastChildNode('树结构数据')
deleteSourceNode(node) {
for (let i = node.length - 1; i >= 0; i--) {
if (!node[i].hasOwnProperty('children')) {
continue;
}
if (node[i].children.length === 0) {
node.splice(i, 1);
continue;
}
this.deleteSourceNode(node[i].children);
}
return node;
}
deleteSourceNode('树组件数据')
queryParentNode(node, pid, temp = undefined){
let forFn = (arr, id) => {
for (let i = 0; i < arr.length; i++) {
if (arr[i].id === id) {
temp = arr[i];
break;
} else {
if (arr[i].children) {
forFn(arr[i].children, id)
}
}
}
}
forFn(node, pid);
return temp;
}
queryParentNode('树组件数据', '节点的pId')
getMaxFloor(treeData) {
let floor = 0;
let max = 0;
let deepEach = (data, floor) => {
data.map((item,index) => {
item.floor = floor;
if (floor > max) {
max = floor;
};
if (item.children.length > 0) {
deepEach(item.children, floor + 1);
};
});
};
deepEach(treeData, 1);
return max;
}
getMaxFloor('树组件数据')
parseTreeToRow(node, data = [], row = []) {
if (node.children.length === 0) {
data.push(row);
} else {
node.children.map((item,index) => {
const obj = {
label:item.label,
id: item.id,
isfile: item.isfile,
pId: item.pId
};
this.parseTreeToRow(item, data, [...row, obj]);
});
}
return data;
}
let obj = {
id: '',
isfile: false,
label: '',
pId: '',
children: '树组件数据'
}
parseTreeToRow(obj)
findParents(treeData,id) {
let allparents = []
if(treeData.length === 0){
return
}
let findele = (data,id) => {
if (!id) return
data.forEach((item,index) => {
if (item.id === id) {
allparents.unshift(item.id)
findele(treeData,item.parentId)
} else {
if (item.children) {
findele(item.children,id)
}
}
})
}
findele(treeData,id)
return allparents
}
function createTree(arr, pid = 0) {
return arr
.filter((v) => v.parent_id === pid)
.map((v) => {
v = JSON.parse(JSON.stringify(v))
const children = createTree(arr, v.id)
if (children.length) {
v.children = createTree(arr, v.id)
}
return v
})
}