Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
15
How to trigger a function when selecting a node of Igx-Tree (without checkboxes)
posted

Hi all,

I have a question about the tree (https://es.infragistics.com/products/ignite-ui-angular/angular/components/tree), how could I trigger a function when there is a node selected in the tree? For example, the function takes the name of the node, and react accordingly. There are another 2 requirements in my case.

1. The tree should not have checkboxes, so its Selection = None, not BiState or Cascading

2. I don't want to use "activeNodeChanged" (www.infragistics.com/.../IgxTreeComponent.html, because it has side effect, which also triggers the function when I want to expand the tree by clicking the expand symbol next to the node. And it is not wanted.

Could anyone help?  Many thanks

Gotop

Parents
No Data
Reply
  • 1320
    Offline posted

    Hello Gotop,

    After investigating this further, I have determined that your requirement could be achieved by handling the click event of each node in the IgxTree, setting the selected state of the node and executing a custom method. This could be achieved as follows:

    public ngAfterViewInit() {

        this.tree.nodes.forEach((node) => {

          node.nativeElement.addEventListener('click', (evt) =>

            this.onClick(node, evt) );

        });

      }

      public onClick(node, event) {

        if ( event.target.ariaLabel !== 'Expand' && event.target.ariaLabel !== 'Collapse') {

          node.selected = !node.selected;

          this.customFunction();

        }

      }

    Please keep in mind that by design the selectedChanged event is not emitted, when a node is selected through the API. It is emitted only by clicking on the checkbox or by pressing ‘Space’.

    I have prepared a sample, demonstrating the described behavior. Please test it on your side and let me know if you need any further information regarding this matter.

    Regards,
    Monika Kirkova,
    Infragistics

Children