张延森 4 years ago
parent
commit
056f863ea3
1 changed files with 22 additions and 25 deletions
  1. 22
    25
      src/pages/property/building/index.jsx

+ 22
- 25
src/pages/property/building/index.jsx View File

@@ -24,54 +24,51 @@ export default props => {
24 24
   const [treeData, setTreeData] = useState()
25 25
 
26 26
   const updateTreeData = (origin, parent, nodes) => {
27
+    const pid = parent ? `${[parent.pid, parent.id].filter(Boolean).join('-')}` : '0'
28
+    const newDatas = Array.isArray(nodes) ? (nodes || []).map(x => ({...x, pid})) : [{...nodes, pid}]
29
+
30
+    console.log(pid, newDatas)
27 31
     if (!parent) {
28
-      const newData = [
32
+      return [
29 33
         ...(origin || []),
30
-        ...(Array.isArray(nodes) ? nodes : [nodes])
34
+        ...newDatas
31 35
       ]
32
-      return newData
33 36
     }
34 37
 
35
-    const newData = (origin || []).map(x => {
36
-      if (x.id === parent) {
37
-        if (Array.isArray(nodes)) {
38
-          x.children = [
39
-            ...(x.children || []),
40
-            ...nodes
41
-          ]
42
-        } else {
38
+    return (origin || []).map(x => {
39
+      if (x.id === parent.id && x.pid === parent.pid) {
43 40
           x.children = [
44 41
             ...(x.children || []),
45
-            nodes
42
+            ...newDatas
46 43
           ]
47
-        }
48 44
       } else {
49 45
         if (x.children && x.children.length) {
50
-          x.children = updateTreeData(x.children, parent, nodes)
46
+          x.children = updateTreeData(x.children, parent, newDatas)
51 47
         }
52 48
       }
53 49
 
54 50
       return x
55 51
     })
56
-
57
-    return newData
58 52
   }
59 53
 
60 54
   const getTreeData = (nodeData, refresh) => {
61 55
 
62
-    const query = nodeData ? {
63
-      id: nodeData.props.dataRef.id,
64
-      name: nodeData.props.dataRef.name,
65
-      treeType: nodeData.props.dataRef.type,
66
-      nodeNumber: nodeData.props.dataRef.nodeNumber,
67
-    } : { treeType: 'phase' }
56
+    const parent = nodeData ? nodeData.props.dataRef : { type: 'phase', pid: 0 }
57
+
58
+    const query = {
59
+      id: parent.id,
60
+      name: parent.name,
61
+      treeType: parent.type,
62
+      nodeNumber: parent.nodeNumber,
63
+    }
68 64
 
69 65
     return new Promise((resolve, reject) => {
70 66
       getTrees({ params: query }).then(res => {
71 67
         if (refresh) {
72
-          setTreeData(res)
68
+          setTreeData((res || []).map(x => ({...x, pid: 0})))
73 69
         } else {
74
-          const data = updateTreeData(treeData, query.id, res)
70
+          const data = updateTreeData(treeData, parent, res)
71
+          console.log(data)
75 72
           setTreeData(data)
76 73
         }
77 74
         resolve()
@@ -126,7 +123,7 @@ export default props => {
126 123
     return (      
127 124
       <Tree.TreeNode
128 125
         {...node}
129
-        key={node.id}
126
+        key={`${node.pid}-${node.id}`}
130 127
         dataRef={node}
131 128
         isLeaf={node.type === 'end'}
132 129
         title={(<NodeLabel data={node} onAdd={showPrompt} onDelete={handleNodeDelete} />)}