fix tree
This commit is contained in:
parent
d1de16a753
commit
ad7c4bf1a6
|
|
@ -43,87 +43,157 @@ export const treePrefix = (props: any) => {
|
||||||
return prefix;
|
return prefix;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const sortTree = (list: any, parent_key: string, pk: string) => {
|
export const sortTree = (list: any[], parent_key: string, pk: string) => {
|
||||||
let meta = {} as Record<
|
const nodes: { [id: string]: any } = {};
|
||||||
string,
|
const result: any[] = [];
|
||||||
{ item: any; idx: string; depth: number; id_parent: any }
|
|
||||||
>;
|
|
||||||
|
|
||||||
if (list.length > 0 && !isEditor) {
|
// First pass: Create nodes
|
||||||
const new_list = [];
|
list.forEach((node) => {
|
||||||
const unlisted = {} as Record<string, any>;
|
const id = node[pk];
|
||||||
for (const item of list) {
|
nodes[id] = { ...node, __depth: 0, __children: [] };
|
||||||
if (item[parent_key] === null) {
|
});
|
||||||
if (!meta[item[pk]]) {
|
|
||||||
meta[item[pk]] = {
|
// Second pass: Build the tree structure
|
||||||
item,
|
list.forEach((node) => {
|
||||||
idx: new_list.length + "",
|
const id = node[pk];
|
||||||
depth: 0,
|
const parentId = node[parent_key];
|
||||||
id_parent: null,
|
if (parentId === null || parentId === undefined) {
|
||||||
};
|
result.push(nodes[id]);
|
||||||
item.__depth = 0;
|
} else {
|
||||||
new_list.push(item);
|
if (nodes[parentId]) {
|
||||||
}
|
nodes[parentId].__children.push(nodes[id]);
|
||||||
} else {
|
} else {
|
||||||
unlisted[item[pk]] = item;
|
// Handle the case where a parent is missing
|
||||||
|
result.push(nodes[id]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let cyclic = {} as Record<string, number>;
|
// Function to flatten the tree
|
||||||
while (Object.values(unlisted).length > 0) {
|
function flattenTree(node: any, depth: number = 0): any[] {
|
||||||
for (const item of Object.values(unlisted)) {
|
node.__depth = depth;
|
||||||
const parent = meta[item[parent_key]];
|
const children = node.__children || [];
|
||||||
if (!cyclic[item[pk]]) {
|
delete node.__children;
|
||||||
cyclic[item[pk]] = 1;
|
return [
|
||||||
} else {
|
node,
|
||||||
cyclic[item[pk]]++;
|
...children
|
||||||
}
|
.sort((a: any, b: any) => {
|
||||||
if (cyclic[item[pk]] > 5) {
|
if (
|
||||||
item.__depth = 0;
|
a.__children.length === 0 &&
|
||||||
meta[item[pk]] = {
|
b.__children.length === 0 &&
|
||||||
item,
|
a.name &&
|
||||||
depth: 0,
|
b.name
|
||||||
idx: new_list.length + "",
|
) {
|
||||||
id_parent: null,
|
return a.name.localeCompare(b.name);
|
||||||
};
|
}
|
||||||
new_list.push(item);
|
|
||||||
delete unlisted[item[pk]];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item[parent_key] === item[pk]) {
|
return (b.__children?.length || 0) - (a.__children?.length || 0);
|
||||||
item.__depth = 0;
|
})
|
||||||
|
.flatMap((child: any) => flattenTree(child, depth + 1)),
|
||||||
meta[item[pk]] = {
|
];
|
||||||
item,
|
|
||||||
depth: 0,
|
|
||||||
idx: new_list.length + "",
|
|
||||||
id_parent: null,
|
|
||||||
};
|
|
||||||
new_list.push(item);
|
|
||||||
delete unlisted[item[pk]];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parent) {
|
|
||||||
item.__depth = parent.depth + 1;
|
|
||||||
|
|
||||||
meta[item[pk]] = {
|
|
||||||
item,
|
|
||||||
depth: parent.depth + 1,
|
|
||||||
idx: parent.idx + ".",
|
|
||||||
id_parent: item[parent_key],
|
|
||||||
};
|
|
||||||
delete unlisted[item[pk]];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const sorted = Object.values(meta)
|
|
||||||
.sort((a, b) => a.idx.localeCompare(b.idx))
|
|
||||||
.map((e) => e.item);
|
|
||||||
|
|
||||||
return sorted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
// Flatten and assign indices
|
||||||
|
const flatResult = result.flatMap((node) => flattenTree(node));
|
||||||
|
flatResult.forEach((node, index) => {
|
||||||
|
node.idx = index;
|
||||||
|
});
|
||||||
|
|
||||||
|
return flatResult;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// export const sortTree = (list: any[], parent_key: string, pk: string) => {
|
||||||
|
// let meta = {} as Record<
|
||||||
|
// string,
|
||||||
|
// { item: any; idx: string; depth: number; id_parent: any }
|
||||||
|
// >;
|
||||||
|
|
||||||
|
// let mode = "" as "" | "str" | "num";
|
||||||
|
// let _list = list.sort((a, b) => {
|
||||||
|
// if (!mode) {
|
||||||
|
// mode = typeof a[pk] === "string" ? "str" : "num";
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (mode === "str") return b[pk].toLocaleString(a[pk]);
|
||||||
|
|
||||||
|
// return a[pk] - b[pk];
|
||||||
|
// });
|
||||||
|
|
||||||
|
// if (_list.length > 0 && !isEditor) {
|
||||||
|
// const new_list = [];
|
||||||
|
// const unlisted = {} as Record<string, any>;
|
||||||
|
// for (const item of _list) {
|
||||||
|
// if (item[parent_key] === null) {
|
||||||
|
// if (!meta[item[pk]]) {
|
||||||
|
// meta[item[pk]] = {
|
||||||
|
// item,
|
||||||
|
// idx: new_list.length + "",
|
||||||
|
// depth: 0,
|
||||||
|
// id_parent: null,
|
||||||
|
// };
|
||||||
|
// item.__depth = 0;
|
||||||
|
// new_list.push(item);
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// unlisted[item[pk]] = item;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// let cyclic = {} as Record<string, number>;
|
||||||
|
// while (Object.values(unlisted).length > 0) {
|
||||||
|
// for (const item of Object.values(unlisted)) {
|
||||||
|
// const parent = meta[item[parent_key]];
|
||||||
|
// if (!cyclic[item[pk]]) {
|
||||||
|
// cyclic[item[pk]] = 1;
|
||||||
|
// } else {
|
||||||
|
// cyclic[item[pk]]++;
|
||||||
|
// }
|
||||||
|
// if (cyclic[item[pk]] > 5) {
|
||||||
|
// item.__depth = 0;
|
||||||
|
// meta[item[pk]] = {
|
||||||
|
// item,
|
||||||
|
// depth: 0,
|
||||||
|
// idx: new_list.length + "",
|
||||||
|
// id_parent: null,
|
||||||
|
// };
|
||||||
|
// new_list.push(item);
|
||||||
|
// delete unlisted[item[pk]];
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (item[parent_key] === item[pk]) {
|
||||||
|
// item.__depth = 0;
|
||||||
|
|
||||||
|
// meta[item[pk]] = {
|
||||||
|
// item,
|
||||||
|
// depth: 0,
|
||||||
|
// idx: new_list.length + "",
|
||||||
|
// id_parent: null,
|
||||||
|
// };
|
||||||
|
// new_list.push(item);
|
||||||
|
// delete unlisted[item[pk]];
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (parent) {
|
||||||
|
// item.__depth = parent.depth + 1;
|
||||||
|
|
||||||
|
// meta[item[pk]] = {
|
||||||
|
// item,
|
||||||
|
// depth: parent.depth + 1,
|
||||||
|
// idx: parent.idx + ".",
|
||||||
|
// id_parent: item[parent_key],
|
||||||
|
// };
|
||||||
|
// delete unlisted[item[pk]];
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// const sorted = Object.values(meta)
|
||||||
|
// .sort((a, b) => a.idx.localeCompare(b.idx))
|
||||||
|
// .map((e) => e.item);
|
||||||
|
|
||||||
|
// return sorted;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return _list;
|
||||||
|
// };
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue