Pārlūkot izejas kodu

update 优化 优化前端树结构拼接性能

疯狂的狮子Li 1 mēnesi atpakaļ
vecāks
revīzija
7be0723c31
1 mainītis faili ar 6 papildinājumiem un 24 dzēšanām
  1. 6 24
      src/utils/ruoyi.ts

+ 6 - 24
src/utils/ruoyi.ts

@@ -167,39 +167,21 @@ export const handleTree = <T>(data: any[], id?: string, parentId?: string, child
   };
 
   const childrenListMap: any = {};
-  const nodeIds: any = {};
   const tree: T[] = [];
-
   for (const d of data) {
-    const parentId = d[config.parentId];
-    if (childrenListMap[parentId] == null) {
-      childrenListMap[parentId] = [];
-    }
-    nodeIds[d[config.id]] = d;
-    childrenListMap[parentId].push(d);
+    const id = d[config.id];
+    childrenListMap[id] = d;
   }
 
   for (const d of data) {
     const parentId = d[config.parentId];
-    if (nodeIds[parentId] == null) {
+    const parentObj = childrenListMap[parentId]
+    if (!parentObj) {
       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;
 };