[SalesForce] How to get an event onselect when using “lightning-tree” in LWC

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.

Best Answer

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.

Related Topic