Просмотр исходного кода

生成树形结构的函数修改

zhongxiaoyu 3 лет назад
Родитель
Сommit
f84328e225
2 измененных файлов с 79 добавлено и 48 удалено
  1. 60 29
      src/utils/validate.js
  2. 19 19
      src/views/accountManagement/components/accountEdit.vue

+ 60 - 29
src/utils/validate.js

@@ -19,37 +19,68 @@ export function validUsername(str) {
   return valid_map.indexOf(str.trim()) >= 0
 }
 // 账号组树构造
-export function translateDataToTreeAll(data, parentKey, parentIDKey) {
-  const parent = data.filter(value => Number(value[parentKey]) <= 0) // 父数据
-  const children = data.filter(value => Number(value[parentKey]) > 0) // 子数据
-  // console.log('--parent', parent)
-  // console.log('--children', children)
-  const 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) {
+//   const parent = data.filter(value => Number(value[parentKey]) <= 0) // 父数据
+//   const children = data.filter(value => Number(value[parentKey]) > 0) // 子数据
+//   // console.log('--parent', parent)
+//   // console.log('--children', children)
+//   const 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++
+//         }
+//       }
+//     })
+//   }
+//   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
 }
 
 // 模糊查询

+ 19 - 19
src/views/accountManagement/components/accountEdit.vue

@@ -1,7 +1,7 @@
 <!--
  * @Author: Badguy
  * @Date: 2022-02-15 11:37:42
- * @LastEditTime: 2022-04-25 12:02:43
+ * @LastEditTime: 2022-04-25 16:32:27
  * @LastEditors: your name
  * @Description: 编辑账号
  * have a nice day!
@@ -521,24 +521,24 @@ export default {
       })
     },
     handleSaveEdit() {
-      const params = {
-        AuthList: this.permissionTreeChckedTemp,
-        GroupList: this.accountGroupTreeCheckedTemp.map(groupId => ({ GroupId: groupId })),
-        RoleList: this.roleListCheckedTemp,
-        TacList: this.loginPolicyCheckedTemp,
-        UserDesc: this.accountForm.desc,
-        UserName: this.accountForm.name,
-        UserPwd: this.accountForm.pwd
-      }
-      if (this.doesAccountExist) {
-        this.saveEditAccount({
-          ...params,
-          UserId: this.userId,
-          Status: this.accountForm.status
-        })
-      } else {
-        this.saveAddAccount(params)
-      }
+      // const params = {
+      //   AuthList: this.permissionTreeChckedTemp,
+      //   GroupList: this.accountGroupTreeCheckedTemp.map(groupId => ({ GroupId: groupId })),
+      //   RoleList: this.roleListCheckedTemp,
+      //   TacList: this.loginPolicyCheckedTemp,
+      //   UserDesc: this.accountForm.desc,
+      //   UserName: this.accountForm.name,
+      //   UserPwd: this.accountForm.pwd
+      // }
+      // if (this.doesAccountExist) {
+      //   this.saveEditAccount({
+      //     ...params,
+      //     UserId: this.userId,
+      //     Status: this.accountForm.status
+      //   })
+      // } else {
+      //   this.saveAddAccount(params)
+      // }
     },
     // 编辑账号
     async saveEditAccount(params) {