|
@@ -267,7 +267,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 = {};
|
|
@@ -331,7 +403,7 @@ const pmTree = {
|
|
|
}
|
|
|
//set items
|
|
|
sortTreeItems(this);
|
|
|
- };
|
|
|
+ }; */
|
|
|
|
|
|
Tree.prototype.addNodeData = function (data, parent, nextSibling, nodeState = null) {
|
|
|
this.sourceData.push(data);
|
|
@@ -409,7 +481,7 @@ const pmTree = {
|
|
|
function isValid(nodes) {
|
|
|
for (let node of nodes) {
|
|
|
if (node.data.ParentID !== -1 &&
|
|
|
- (!node.parent || node.parent.data.ID !== node.data.ParentID)) {
|
|
|
+ (!node.parent || !node.parent.data || node.parent.data.ID !== node.data.ParentID)) {
|
|
|
console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`);
|
|
|
return false;
|
|
|
}
|