Utils 代码整理 重用
2018-04-18 本文已影响0人
cabber
// 部门树形排列
result.data.forEach(item => {
this.creatTree(item, arr);
});
console.log(arr);
// 生成部门树
creatTree (meta, list) {
let index = list.findIndex(ret => ret.treeId === meta.treeParentId);
if (index > -1) {
list[index].children.push(meta);
} else if (meta.treeParentId === 'department_0') {
list.push(meta);
} else {
list.forEach(item => {
item.children && item.children.length && this.creatTree(meta, item.children)
});
}
},
// 树转平 this.treeToList(list, 'items');
treeToList (tree, field) {
let arr = [];
tree && tree.forEach(item => {
if (item[field].length) arr.push(...this.treeToList(item[field], field));
item[field] = [];
arr.push(item);
});
return arr;
}