Przeglądaj źródła

fix:项目管理树建立代码,树结构数据没错,但是建立出来有问题

vian 5 lat temu
rodzic
commit
5947a3855c
1 zmienionych plików z 74 dodań i 2 usunięć
  1. 74 2
      web/building_saas/pm/js/pm_tree.js

+ 74 - 2
web/building_saas/pm/js/pm_tree.js

@@ -268,7 +268,79 @@ const pmTree = {
                 }, []);
             };
 
-            Tree.prototype.loadData = function (arrData) {
+          function sortChildren(nodes) {
+            const IDMap = {};
+            const nextMap = {};
+            let firstNode = null;
+            const newList = [];
+            for (const node of nodes) {
+              // 递规排序
+              if (node.children && node.children.length > 0) {
+                node.children = sortChildren(node.children);
+              }
+              IDMap[node.data.ID] = node;
+              if (node.data.NextSiblingID != -1) {
+                nextMap[node.data.NextSiblingID] = node;
+              }
+            }
+            for (const node of nodes) {
+              if (!nextMap[node.data.ID]) { //如果在下一节点映射没找到,则是第一个节点
+                firstNode = node;
+                break;
+              }
+            }
+            if (firstNode) {
+              newList.push(firstNode);
+              delete IDMap[firstNode.data.ID];
+              setNext(firstNode, newList);
+            }
+            // 容错处理,如果链断了的情况,直接添加到后面
+            for (const key in IDMap) {
+              if (IDMap[key]) {
+                newList.push(IDMap[key]);
+              }
+            }
+            return newList;
+
+            function setNext(node, array) {
+              if (node.data.NextSiblingID != -1) {
+                const next = IDMap[node.data.NextSiblingID];
+                node.nextSibling = next || null;
+                if (next) {
+                  array.push(next);
+                  delete IDMap[next.data.ID];
+                  setNext(next, array);
+                }
+              }
+            }
+          }
+
+          Tree.prototype.loadData = function (treeData) {
+            this.sourceData = treeData;
+            // 建立节点及索引
+            const nodes = [];
+            const parentMap = {};
+            const IDMap = {};
+            treeData.forEach(nodeData => {
+              const node = new Node(this, nodeData);
+              this.maxNodeId(node.id());
+              IDMap[node.data.ID] = node;
+              (parentMap[node.data.ParentID] || (parentMap[node.data.ParentID] = [])).push(node);
+              nodes.push(node);
+            });
+            // 过滤出各节点的children、设置节点parent
+            nodes.forEach(node => {
+              node.children = parentMap[node.data.ID] || [];
+              node.parent = IDMap[node.data.ParentID] || this._root;
+            });
+            const roots = parentMap['-1'];
+            // 将各节点的children进行排序,并设置nextSibling
+            this._root.children = sortChildren(roots);
+            // 生成tree.items
+            sortTreeItems(this);
+          };
+
+            /* Tree.prototype.loadData = function (arrData) {
                 this.sourceData = arrData;
                 let i, that = this;
                 let nodesIndex = {};
@@ -332,7 +404,7 @@ const pmTree = {
                 }
                 //set items
                 sortTreeItems(this);
-            };
+            }; */
 
             Tree.prototype.addNodeData = function (data, parent, nextSibling, nodeState = null) {
                 this.sourceData.push(data);