+ Start a Discussion
Vigneshwaran LoganathanVigneshwaran Loganathan 

get Record Id with lightning:Tree

Hi All,  I have a requirement to show object's records in nested-accordian view.. It goes Like Customer(Parent) > Customer Product(Child for Customer) > Suites (Child for Customer Product)

Initially I used Accordian, but I was not able to go third-level.. So, I used lightning:tree for this purpose.. But, with this, I cannot get Id of the record.. Can anyone help with this? 

CMP: 
<aura:component controller="TreeController">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="items" type="Object"/>
    <lightning:tree items="{! v.items }" onselect="{!c.handleSelect}"/>
</aura:component>

JS
({
    doInit: function (cmp, event, helper) {
        var action = cmp.get("c.getTreeData");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set('v.items', JSON.parse(response.getReturnValue()));
            }
            
        });
        $A.enqueueAction(action);
    },
    
    handleSelect: function (cmp, event, helper) {  
        
        var getId = event.getParam('name');
        console.log('>>> getName' + getId);
	},
    
 })




User-added image

Vignesh
Best Answer chosen by Vigneshwaran Loganathan
CharuDuttCharuDutt
Hii Vigneshwaran Loganathan
If You Dont Mind Can You Share Your Controller (Apex Class) Cause in mine it Sets Id 
User-added image
 

All Answers

CharuDuttCharuDutt
Hii Vigneshwaran Loganathan
Try The Below Code Changes By Me Are In Bold
<aura:component controller="TreeController">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <aura:attribute name="items2" type="String" />

    <aura:attribute name="items" type="Object"/>
    <lightning:tree items="{! v.items }" onselect="{!c.handleSelect}"/>

    <b><H6>Selected ID : {!v.items2}</H6></b>

</aura:component>


JS
({
    doInit: function (cmp, event, helper) {
        var action = cmp.get("c.getTreeData");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set('v.items', JSON.parse(response.getReturnValue()));
            }
            
        });
        $A.enqueueAction(action);
    },
    
    handleSelect: function (cmp, event, helper) {  
        
        var getId = event.getParam('name');

         cmp.set('v.items2', getId );
    },
    
 })
Please Let Me Know And Mark It As Best Answer If It Helps
Thank You!
 
Vigneshwaran LoganathanVigneshwaran Loganathan
CharuDutt - Thanks for the input. but this simply takes "Name" of the record from item and set it in item2. But, I want to fetch Id of the record.
CharuDuttCharuDutt
Hii Vigneshwaran Loganathan
If You Dont Mind Can You Share Your Controller (Apex Class) Cause in mine it Sets Id 
User-added image
 
This was selected as the best answer
Vigneshwaran LoganathanVigneshwaran Loganathan
Great - I got my mistake from your screenshot. 

FYI - Below is the snippet from code. 

            cWraper.name = c.Id ; // earlier I had as "c.Name"
            cWraper.label =c.Name ;
            cWraper.expanded =false ;

Thanks for your help!