|
@@ -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
|
|
|
}
|
|
|
|
|
|
// 模糊查询
|