瀏覽代碼

生成树形结构方法修改

zhongxiaoyu 2 年之前
父節點
當前提交
f7ec912687
共有 1 個文件被更改,包括 54 次插入32 次删除
  1. 54 32
      src/utils/validate.js

+ 54 - 32
src/utils/validate.js

@@ -1,7 +1,7 @@
 /*
  * @Author: your name
  * @Date: 2021-12-13 09:43:22
- * @LastEditTime: 2022-04-29 17:03:30
+ * @LastEditTime: 2022-06-01 11:37:15
  * @LastEditors: your name
  * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  */
@@ -36,39 +36,61 @@ export function orgTree (str) {
 }
 
 // 岗位树构造
-export function translateDataToTreeAll (data, parentKey, parentIDKey) {
-  let parent = data.filter((value) => Number(value[parentKey]) <= 0);// 父数据
-  let children = data.filter((value) => Number(value[parentKey]) > 0);// 子数据
-  //console.log('--parent', parent)
-  //console.log('--children', children)
-  let translator = (parent, children) => {
-    parent.forEach((parent) => {
-      parent.children = [];
-      // children.forEach((current, index) => {
-      //   if (current[parentKey] === parent[parentIDKey]) {
-      //     const temp = JSON.parse(JSON.stringify(children));
-      //     temp.splice(index, 1);
-      //     translator([current], temp);
-      //     typeof parent.children !== "undefined"
-      //       ? parent.children.push(current)
-      //       : (parent.children = [current]);
-      //   }
-      // });
-      for (let i = 0; i < children.length;) {
-        if (children[i][parentKey] === parent[parentIDKey]) {
-          const temp = children.splice(i, 1)
-          typeof parent.children !== 'undefined'
-            ? parent.children.push(...temp)
-            : (parent.children = temp)
-          translator(temp, children)
-        } else {
-          i++
+// export function translateDataToTreeAll (data, parentKey, parentIDKey) {
+//   let parent = data.filter((value) => Number(value[parentKey]) <= 0);// 父数据
+//   let children = data.filter((value) => Number(value[parentKey]) > 0);// 子数据
+//   //console.log('--parent', parent)
+//   //console.log('--children', children)
+//   let translator = (parent, children) => {
+//     parent.forEach((parent) => {
+//       parent.children = [];
+//       children.forEach((current, index) => {
+//         if (current[parentKey] === parent[parentIDKey]) {
+//           const temp = JSON.parse(JSON.stringify(children));
+//           temp.splice(index, 1);
+//           translator([current], temp);
+//           typeof parent.children !== "undefined"
+//             ? parent.children.push(current)
+//             : (parent.children = [current]);
+//         }
+//       });
+//     });
+//   };
+//   translator(parent, children);
+//   return parent;
+// }
+
+export function translateDataToTreeAll(arr, parentKey, key) {
+  const map = {}
+  const result = []
+  arr.forEach(element => {
+    const id = element[key]
+    const pid = element[parentKey]
+    if (map[id]) {
+      map[id] = {
+        ...element,
+        children: map[id].children
+      }
+    } else {
+      map[id] = {
+        ...element,
+        children: []
+      }
+    }
+    const item = map[id]
+    if (pid <= 0) {
+      result.push(item)
+    } else {
+      if (map[pid]) {
+        map[pid].children.push(item)
+      } else {
+        map[pid] = {
+          children: [item]
         }
       }
-    });
-  };
-  translator(parent, children);
-  return parent;
+    }
+  })
+  return result
 }
 
 export function deteleObject (obj) {