Skip to content

Commit a732abf

Browse files
authored
Add experimental selection of multiple nodes (see test-project) (#20)
1 parent b07ee87 commit a732abf

File tree

6 files changed

+45
-11
lines changed

6 files changed

+45
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ In order to get the attributes of your Node entity in your project, I encourage
121121
![Misc](/assets/tab08-misc.png)
122122

123123
- You can now save the state of your table. This only works for the whole tree, no partial trees!
124-
- Experimentally expose `window.__TreeTable_{guid}_select`. This will only accept 1 guid at the moment.
124+
- Experimentally expose `window.__TreeTable_{guid}_select`. This will accept a guid as string, or array of guids for multi selection
125125

126126
## Test-project
127127

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mxtreetable",
33
"widgetName": "MxTreeTable",
4-
"version": "2.0.5",
4+
"version": "2.0.6",
55
"description": "TreeTable widget",
66
"copyright": "Mendix 2019",
77
"author": "Jelte Lagendijk <jelte.lagendijk@mendix.com>",

src/MxTreeTable.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class MxTreeTable extends Component<MxTreeTableContainerProps> {
143143
getInitialTableState: this.getInitialState,
144144
writeTableState: this.writeTableState,
145145
onSelect: this.onSelectAction.bind(this),
146+
selectionMode: this.props.selectMode,
146147
// TODO make this a check if you can reset state!
147148
resetState: true,
148149
reset: this.reset,

src/MxTreeTable.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ Note: State management (currently) ONLY works when you load a whole tree (see Da
587587
<propertyGroup caption="Experimental">
588588
<property key="experimentalExposeSetSelected" type="boolean" defaultValue="false">
589589
<caption>Expose setSelected</caption>
590-
<description>This is an EXPERIMENTAL feature. It will expose the function to select a certain object in the Tree Table, so you can target it with a Nanoflow + Javascript Action.
590+
<description>This is an EXPERIMENTAL feature. It will expose the function to select a certain object/list of objects in the Tree Table, so you can target it with a Nanoflow + Javascript Action.
591591

592592
Notes:
593593
- This is EXPERIMENTAL. Please debug in the console when having problems. No guarantees that this feature will stay in the widget.

src/package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<package xmlns="http://www.mendix.com/package/1.0/">
3-
<clientModule name="MxTreeTable" version="2.0.5" xmlns="http://www.mendix.com/clientModule/1.0/">
3+
<clientModule name="MxTreeTable" version="2.0.6" xmlns="http://www.mendix.com/clientModule/1.0/">
44
<widgetFiles>
55
<widgetFile path="MxTreeTable.xml"/>
66
</widgetFiles>

src/store/index.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import arrayToTree, { Tree } from "array-to-tree";
44
import { ValidationMessage, getObject } from "@jeltemx/mendix-react-widget-utils";
55
import { TreeColumnProps, getTreeTableColumns, TableRecord } from "../util/columns";
66
import { RowObject, TreeRowObject } from "./objects/row";
7+
import { SelectionMode } from "../../typings/MxTreeTableProps";
78

89
configure({ enforceActions: "observed" });
910

@@ -52,6 +53,7 @@ export interface NodeStoreConstructorOptions {
5253
expandFirstLevel: boolean;
5354
resetState: boolean;
5455
rowObjectMxProperties: RowObjectMxProperties;
56+
selectionMode: SelectionMode;
5557

5658
childLoader: (guids: string[], parentKey: string, loadFromRef: boolean) => Promise<void>;
5759
convertMxObjectToRow: (mxObject: mendix.lib.MxObject, parentKey?: string | null) => Promise<TreeRowObject>;
@@ -89,6 +91,7 @@ export class NodeStore {
8991
private writeTableState: (state: TableState) => void;
9092
private onSelect: (ids: string[]) => void;
9193

94+
private selectionMode: SelectionMode;
9295
private dataResetOnContextChange: boolean;
9396
private needToCalculateInitialParents: boolean;
9497
private needToRestoreStateOnContextChange: boolean;
@@ -111,6 +114,7 @@ export class NodeStore {
111114
rowObjectMxProperties,
112115
validationMessages,
113116
getInitialTableState,
117+
selectionMode,
114118
onSelect,
115119
writeTableState,
116120
resetState,
@@ -130,6 +134,7 @@ export class NodeStore {
130134
this.needToCalculateInitialParents = calculateInitialParents;
131135
this.validationMessages = validationMessages;
132136
this.rowObjectMxProperties = rowObjectMxProperties;
137+
this.selectionMode = selectionMode;
133138
this.getInitialTableState = getInitialTableState;
134139
this.writeTableState = writeTableState;
135140
this.resetState = resetState;
@@ -521,13 +526,41 @@ export class NodeStore {
521526
return this.rowObjects.filter(findRow => findRow._parent && findRow._parent === row.key).length > 0;
522527
}
523528

524-
private _setSelectedFromExternal(guid: string): void {
525-
const object = this.findRowObject(guid);
526-
if (object) {
527-
const parents = this.findParents(object);
528-
this.setSelected([object.key]);
529-
this.setExpanded(parents.map(p => p.key));
530-
this.onSelect([object.key]);
529+
private _setSelectedFromExternal(guids: string | string[]): void {
530+
if (this.selectionMode === "none") {
531+
return;
532+
}
533+
534+
let selected: RowObject[] = [];
535+
if (Array.isArray(guids)) {
536+
selected = guids.map(guid => this.findRowObject(guid)).filter(obj => obj !== null) as RowObject[];
537+
} else {
538+
const object = this.findRowObject(guids);
539+
if (object) {
540+
selected.push(object);
541+
}
542+
}
543+
544+
if (selected.length > 0) {
545+
if (this.selectionMode === "single") {
546+
selected = [selected[0]];
547+
}
548+
549+
const parentGuids: string[] = [];
550+
const selectedGuids = selected.map(obj => obj.key);
551+
552+
selected.forEach(obj => {
553+
const parents = this.findParents(obj);
554+
parents.forEach(parent => {
555+
if (parentGuids.indexOf(parent.key) === -1) {
556+
parentGuids.push(parent.key);
557+
}
558+
});
559+
});
560+
561+
this.setSelected(selectedGuids);
562+
this.setExpanded(parentGuids);
563+
this.onSelect(selectedGuids);
531564
}
532565
}
533566

0 commit comments

Comments
 (0)