Reputation: 1979
I'm using angular-tree-component to generate a tree with checkbox options. HTML
<tree-root [nodes]="nodes" [options]="options">
</tree-root>
Typescript:
import { ITreeOptions } from 'angular-tree-component';
import { Component } from '@angular/core';
export class myComponent {
nodes = [
{
name: 'root1',
children: [
{ name: 'root1_child1' },
{
name: 'root1_child2', children: [
{ name: 'grand_child1' },
{ name: 'grand_child2' }
]
}
]
},
{
name: 'root2',
children: [
{ name: 'root2_child1' },
{
name: 'root2_child2', children: [
{ name: 'grand_child1' },
{ name: 'grand_child2' }
]
}
]
}
];
options: ITreeOptions = {
useCheckbox: true
};
optionsDisabled: ITreeOptions = {
useCheckbox: true,
useTriState: false
};
So I'm able to select tree nodes (including children) but not able to find any way where I can capture all the selected (checked) nodes and display on another box.
Upvotes: 4
Views: 14141
Reputation: 1154
You can do this by using (select) and (deselect) events. here it is a little example.
onSelect(event) {
try {
let pushdata: any = [];
pushdata.push(event.node.data);
console.log(this.TreeViewData);
} catch (e) {
console.log(e.message)
}
}
and same as in deselect even you can capture deselected nodes
ondeSelect(event) {
try {
let pushdata: any = [];
pushdata.push(event.node.data);
console.log(this.TreeViewData);
} catch (e) {
console.log(e.message)
}
}
Event.node.data will return a list of the array off all the properties you binded.
Upvotes: 0
Reputation: 89
With reference to above answer to get objects you can use
Object.entries(this.treeModel.selectedLeafNodeIds)
.filter(([key, value]) => {
return (value === true);
})
.map((id) => {
let node = this.treeModel.getNodeById(id[0]);
return node;
});
Upvotes: 3
Reputation: 156
You can use "event.treeModel.selectedLeafNodeIds" to get the selected node in tree,
Example:
<tree-root [nodes]="treeNode" (select)="onSelect($event)"
(deselect)="onDeselect($event)" [options]="options"></tree-root>
this.selectedTreeList = Object.entries(event.treeModel.selectedLeafNodeIds)
.filter(([key, value]) => {
return (value === true);
}).map((node) => node[0]);
Upvotes: 5