update 优化 优化前端树结构拼接性能
This commit is contained in:
parent
eab4345198
commit
7be0723c31
@ -167,39 +167,21 @@ export const handleTree = <T>(data: any[], id?: string, parentId?: string, child
|
|||||||
};
|
};
|
||||||
|
|
||||||
const childrenListMap: any = {};
|
const childrenListMap: any = {};
|
||||||
const nodeIds: any = {};
|
|
||||||
const tree: T[] = [];
|
const tree: T[] = [];
|
||||||
|
|
||||||
for (const d of data) {
|
for (const d of data) {
|
||||||
const parentId = d[config.parentId];
|
const id = d[config.id];
|
||||||
if (childrenListMap[parentId] == null) {
|
childrenListMap[id] = d;
|
||||||
childrenListMap[parentId] = [];
|
|
||||||
}
|
|
||||||
nodeIds[d[config.id]] = d;
|
|
||||||
childrenListMap[parentId].push(d);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const d of data) {
|
for (const d of data) {
|
||||||
const parentId = d[config.parentId];
|
const parentId = d[config.parentId];
|
||||||
if (nodeIds[parentId] == null) {
|
const parentObj = childrenListMap[parentId]
|
||||||
|
if (!parentObj) {
|
||||||
tree.push(d);
|
tree.push(d);
|
||||||
|
} else {
|
||||||
|
parentObj[config.childrenList].push(d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const adaptToChildrenList = (o: any) => {
|
|
||||||
if (childrenListMap[o[config.id]] !== null) {
|
|
||||||
o[config.childrenList] = childrenListMap[o[config.id]];
|
|
||||||
}
|
|
||||||
if (o[config.childrenList]) {
|
|
||||||
for (const c of o[config.childrenList]) {
|
|
||||||
adaptToChildrenList(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const t of tree) {
|
|
||||||
adaptToChildrenList(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
return tree;
|
return tree;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user