1

Is there way how we can catch an event from "lightning-tree" structure in LWC ? For ex.: I want to know which element was selected/clicked.

The same works in Aura, but I have a problem with LWC:

HTML:

<template>
    <lightning-tree items={treeItems} onselect={handleSelect}>
    </lightning-tree>
<template>

JS:

@api recordId;
@track treeItems;
     @track error;
     @wire(getTreeData)
     wireTreeData({
         error,
         data
     }) {
         if (data) {
             this.treeItems = data;
             console.log(JSON.stringify(data, null, '\t'));
         } else {
             this.error = error;
         }
     }

handleSelect(evt){
    var myName = evt.getParam('name');
}

I checked the documentation of this structure and onselect attribute is marked as aura action:

onselect Aura Action The action triggered when a tree item is selected.

1 Answer 1

2

In LWC, you use the detail property, not the event.getParam function (this is for Aura).

handleSelect(evt){
    var myName = evt.detail.name;

Make sure you're looking at the Lightning Web Component version of the documentation, and not the Aura Component version.

1
  • That's correct, just was trying it and it wasn't working in my case, just had 'undefined', probably forgot to refersh the page twice to make new changes work, but now tried again and it works as expected :) Commented Apr 20, 2020 at 20:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .