Skip to content

Commit

Permalink
* dtable: support for setting parent with array with nested plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
catouse committed Mar 28, 2024
1 parent 1a7abf8 commit 3ae08ce
Showing 1 changed file with 51 additions and 27 deletions.
78 changes: 51 additions & 27 deletions lib/dtable/src/plugins/nested/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {DTableStoreTypes} from '../store';
import type {ColInfo} from '../../types/col';
import type {CustomRenderResult} from '../../types/common';
import type {DTableWithPlugin, DTablePlugin} from '../../types/plugin';
import type {RowData, RowID} from '../../types/row';
import type {RowData, RowID, RowInfo} from '../../types/row';

export const enum NestedRowState {
unknown = '',
Expand Down Expand Up @@ -48,6 +48,7 @@ export type DTableNestedTypes = {
}>,
data: {
nestedMap: Map<RowID, NestedRowInfo>,
nestedRowMap: Map<RowID, RowInfo>,
},
methods: {
getNestedInfo: typeof getNestedInfo;
Expand Down Expand Up @@ -232,7 +233,7 @@ const nestedPlugin: DTablePlugin<DTableNestedTypes, DTableNestedDependencies> =
},
when: options => !!options.nested,
data() {
return {nestedMap: new Map()};
return {nestedMap: new Map(), nestedRowMap: new Map()};
},
state() {
return {nestedState: {}};
Expand All @@ -255,36 +256,59 @@ const nestedPlugin: DTablePlugin<DTableNestedTypes, DTableNestedDependencies> =
},
beforeLayout() {
this.data.nestedMap.clear();
this.data.nestedRowMap.clear();
},
onAddRow(row) {
const {nestedMap} = this.data;
const parent = String(row.data?.[this.options.nestedParentKey ?? 'parent']);
const info: NestedRowInfo = nestedMap.get(row.id) ?? {
state: NestedRowState.unknown,
level: 0,
};
info.parent = parent === '0' ? undefined : parent;
if (row.data?.[this.options.asParentKey ?? 'asParent']) {
info.children = [];
}
nestedMap.set(row.id, info);
this.data.nestedRowMap.set(row.id, row);
},
onAddRows(rows) {
const {nestedMap, nestedRowMap} = this.data;
rows.forEach(row => {
const info: NestedRowInfo = nestedMap.get(row.id) ?? {
state: NestedRowState.unknown,
level: 0,
};
let parentPath = (row.data?.[this.options.nestedParentKey ?? 'parent'] as undefined | (string | number) | (string | number)[]) ?? [];
if (!Array.isArray(parentPath)) {
parentPath = [parentPath];
}
let parentID: string | undefined;
while (parentPath.length) {
let lastParentID = parentPath.pop();
if (lastParentID === undefined) {
continue;
}
lastParentID = String(lastParentID);
const parent = nestedRowMap.get(lastParentID);
if (parent) {
parentID = lastParentID;
break;
}
}

if (parent) {
let parentInfo = nestedMap.get(parent);
if (!parentInfo) {
parentInfo = {
state: NestedRowState.unknown,
level: 0,
};
nestedMap.set(parent, parentInfo);
info.parent = parentID === '0' ? undefined : parentID;
if (row.data?.[this.options.asParentKey ?? 'asParent']) {
info.children = [];
}
if (!parentInfo.children) {
parentInfo.children = [];
nestedMap.set(row.id, info);

if (parentID) {
let parentInfo = nestedMap.get(parentID);
if (!parentInfo) {
parentInfo = {
state: NestedRowState.unknown,
level: 0,
};
nestedMap.set(parentID, parentInfo);
}
if (!parentInfo.children) {
parentInfo.children = [];
}
parentInfo.children.push(row.id);
}
parentInfo.children.push(row.id);
}
},
onAddRows(rows) {
});
nestedRowMap.clear();

rows = rows.filter(row => this.getNestedRowInfo(row.id).state !== NestedRowState.hidden);
updateNestedMapOrders(this.data.nestedMap);
rows.sort((rowA, rowB) => {
Expand Down

0 comments on commit 3ae08ce

Please sign in to comment.