Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular-tree-component how to capture selected nodes through checkbox selection

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.enter image description here

like image 784
SatAj Avatar asked Jan 29 '23 04:01

SatAj


2 Answers

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]);
like image 170
nitin Avatar answered Jan 31 '23 21:01

nitin


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;
  });
like image 38
Sachin Kale Avatar answered Jan 31 '23 21:01

Sachin Kale