Angular-tree-component: How to set IsSelected nodes by default?

Created on 5 Feb 2018  路  6Comments  路  Source: CirclonGroup/angular-tree-component

How to set IsSelected nodes by default?

Most helpful comment

@ViewChild('tree') treeComponent: TreeComponent;
onCheckAllButtonClick() {
const treeModel: TreeModel = this.treeComponent.treeModel;
const nodes = treeModel.nodes;
for (const node of nodes) {
for (const child of node.children) {
treeModel.getNodeById(child.id).setIsSelected(true);
}
}
}

All 6 comments

You are supposed to be able to select them by setting the property isSelected to true in the node. But it doesn't seem to be working.

I do it like this and it seems to work:
node.setIsSelected(true);

Yes, with the method setIsSelected it works.

I'm not exactly sure what OP means by selected "by default", but my scenario is that I want to mark nodes as selected at startup, when I initialize the [nodes] directive. I use a checkbox tree. It might be what @DMITRYTISHKIN wants aswell.

Reading from the docs, you are able to set a node as expanded on startup. I would love the same functionality for active/selected.

The problem I ran into was that I have a data structure that specifies the tree structure, and it also specifies if a node should be selected (checkbox ticked) or not.

If I load the nodes via directive, and immediately look for the nodes via "treeModel.getNodeById()" I get an exception. You can get around this by delaying the setIsSelected by a setTimeout.
This behavior makes sense, because of the angular lifecycle. The data in the directive has to go through the OnChanges routine before they can be manipulated. Delaying it a cycle makes it work.

Here is a plunkr that shows the problem. Its a working workaround, but not the cleanest solution.
https://plnkr.co/edit/cMUwDa7lO8OtbDUtksKo?p=preview

The dream scenario would be something like this:
const NODES = [ { id:'1', name:'Root', children: [ { id:'2', name:'First Child', isSelected: true }, { id:'3', name:'Second Child', isSelected: true } ] } ]

I'm thinking that the normal logic of selecting a parent also selects the children should apply. However it should be configurable of this select emits the select action event.

I've been trying see if I can contribute with a solution myself, but sitting in a windows environment, the build scripts aren't cooperating with me yet :)

Setting isSelected in advance will not be supported in future versions. We are keeping tree state apart from tree data.
You have 2 options:
1) As mentioned above and described here: https://angular2-tree.readme.io/docs/expanding-all-on-init
2) By binding to tree state: https://angular2-tree.readme.io/docs/save-restore

@ViewChild('tree') treeComponent: TreeComponent;
onCheckAllButtonClick() {
const treeModel: TreeModel = this.treeComponent.treeModel;
const nodes = treeModel.nodes;
for (const node of nodes) {
for (const child of node.children) {
treeModel.getNodeById(child.id).setIsSelected(true);
}
}
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Roman-Simik picture Roman-Simik  路  5Comments

snstarosciak picture snstarosciak  路  5Comments

Vishnusangam picture Vishnusangam  路  4Comments

salilbajaj picture salilbajaj  路  4Comments

Gillardo picture Gillardo  路  5Comments