= ({
css`
.rdg {
display: grid !important;
+
+ .rdg-cell,
+ .rdg-header-sort-name {
+ display: flex;
+ flex-direction: row;
+ align-items: stretch;
+
+ &.rdg-header-sort-name {
+ align-items: center;
+ }
+ }
}
`
)}
diff --git a/comps/list/utils/sort-tree.ts b/comps/list/utils/sort-tree.ts
index 27ab46c..5badca8 100755
--- a/comps/list/utils/sort-tree.ts
+++ b/comps/list/utils/sort-tree.ts
@@ -42,158 +42,53 @@ export const treePrefix = (props: any) => {
}
return prefix;
};
-
export const sortTree = (list: any[], parent_key: string, pk: string) => {
const nodes: { [id: string]: any } = {};
- const result: any[] = [];
// First pass: Create nodes
list.forEach((node) => {
const id = node[pk];
- nodes[id] = { ...node, __depth: 0, __children: [] };
+ nodes[id] = { ...node, __depth: 0, __children: [], __parent: null };
});
- // Second pass: Build the tree structure
+ // Second pass: Build relationships
list.forEach((node) => {
const id = node[pk];
const parentId = node[parent_key];
- if (parentId === null || parentId === undefined) {
- result.push(nodes[id]);
- } else {
- if (nodes[parentId]) {
- nodes[parentId].__children.push(nodes[id]);
- } else {
- // Handle the case where a parent is missing
- result.push(nodes[id]);
- }
+
+ if (parentId && parentId !== id && nodes[parentId]) {
+ nodes[id].__parent = nodes[parentId];
+ nodes[parentId].__children.push(nodes[id]);
}
});
- // Function to flatten the tree
- function flattenTree(node: any, depth: number = 0): any[] {
- node.__depth = depth;
- const children = node.__children || [];
- delete node.__children;
- return [
- node,
- ...children
- .sort((a: any, b: any) => {
- if (
- a.__children.length === 0 &&
- b.__children.length === 0 &&
- a.name &&
- b.name
- ) {
- return a.name.localeCompare(b.name);
- }
+ // Function to calculate depth
+ const calculateDepth = (node: any, visited: Set
= new Set()): number => {
+ if (visited.has(node.id)) return 0; // Prevent cycles
+ visited.add(node.id);
+
+ if (!node.__parent) return 0;
+ return 1 + calculateDepth(node.__parent, visited);
+ };
- return (b.__children?.length || 0) - (a.__children?.length || 0);
- })
- .flatMap((child: any) => flattenTree(child, depth + 1)),
- ];
- }
+ // Calculate depths
+ Object.values(nodes).forEach((node: any) => {
+ node.__depth = calculateDepth(node);
+ });
- // Flatten and assign indices
- const flatResult = result.flatMap((node) => flattenTree(node));
- flatResult.forEach((node, index) => {
+ // Sort nodes
+ const sortedNodes = Object.values(nodes).sort((a: any, b: any) => {
+ if (a.__depth !== b.__depth) return a.__depth - b.__depth;
+ if (a.__children.length !== b.__children.length) {
+ return b.__children.length - a.__children.length;
+ }
+ return a.name.localeCompare(b.name);
+ });
+
+ // Assign indices
+ sortedNodes.forEach((node: any, index: number) => {
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;
-// 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;
-// 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;
-// };
+ return sortedNodes;
+};
\ No newline at end of file
diff --git a/comps/md/gen/gen-table-list.ts b/comps/md/gen/gen-table-list.ts
index 16a653f..788ac12 100755
--- a/comps/md/gen/gen-table-list.ts
+++ b/comps/md/gen/gen-table-list.ts
@@ -181,13 +181,6 @@ const genTable = async (opt: GenOpt) => {
return;
}
if (e.is_pk && (arg.mode === "table" || arg.mode === "auto")) return;
- let tree_depth = "";
- let tree_depth_built = "";
- if (first) {
- tree_depth = `tree_depth={col.depth}`;
- tree_depth_built = `tree_depth:col.depth`;
- first = false;
- }
return {
component: {
id: "297023a4-d552-464a-971d-f40dcd940b77",
@@ -211,10 +204,10 @@ const genTable = async (opt: GenOpt) => {
adv: {
js: `\
-
+
`,
jsBuilt: `\
-render(React.createElement("div", Object.assign({}, props, { className: cx(props.className, \`s-\${_item?.edit?.parent?.item?.id}\` , "") }),React.createElement(FormatValue, { value: col.value, name: col.name, gen_fields: gen__fields, ${tree_depth_built} })));
+render(React.createElement("div", Object.assign({}, props, { className: cx(props.className, \`s-\${_item?.edit?.parent?.item?.id}\` , "") }),React.createElement(FormatValue, { value: col.value, name: col.name, gen_fields: gen__fields })));
`,
},
}),
diff --git a/comps/md/gen/tbl-list/on_load.ts b/comps/md/gen/tbl-list/on_load.ts
index f47e814..cf3f780 100755
--- a/comps/md/gen/tbl-list/on_load.ts
+++ b/comps/md/gen/tbl-list/on_load.ts
@@ -55,6 +55,10 @@ async (arg: TableOnLoad) => {
const fields = parseGenField(gen__fields);
const gen = generateSelect(fields);
+ if (opt__feature.includes("tree") && opt__id_parent) {
+ gen.select[opt__id_parent] = true
+ }
+
const result = {items: []}
result.items = await db.${table}.findMany({
select: gen.select,
diff --git a/utils/format-value.tsx b/utils/format-value.tsx
index d5551c4..cda2a67 100755
--- a/utils/format-value.tsx
+++ b/utils/format-value.tsx
@@ -11,10 +11,9 @@ export const FormatValue: FC<{
value: any;
name: string;
gen_fields: string[];
- tree_depth?: number;
mode?: "money" | "datetime" | "timeago" | "date";
}> = (prop) => {
- const { value, gen_fields, name, tree_depth, mode } = prop;
+ const { value, gen_fields, name, mode } = prop;
if (gen_fields) {
const gf = JSON.stringify(gen_fields);
if (!fields_map.has(gf)) {
@@ -122,29 +121,28 @@ export const FormatValue: FC<{
}
}
- let prefix = <>>;
- if (typeof tree_depth === "number" && tree_depth > 0) {
- prefix = (
-
- );
- }
+ // let prefix = <>>;
+ // if (typeof tree_depth === "number" && tree_depth > 0) {
+ // prefix = (
+ //
+ // );
+ // }
return (
);