Finding a component using Global Id : salesforce lightning

I am new to this. Based on our project req. I have to create unique aura:id to support dynamic appending of elements. I researched on this aura:id does not support expression. Two question

  1. Can I use global id like <a id = "{! GlobalId + '_tab' + index}"> where anchor tag will be in an iteration; so that i have a unique id for each anchor tag.

  2. How can i get this unique id; the way i am using right now is

var globalId = component.getGlobalId(); globalId = globalId + “_tab0”;

var activeTab = component.find(globalId);

$A.util.addClass(activeTab,’slds-active’);

I am getting the global id but how to find an element with global id?

Is this approach right?

Answer

On your lightning component markup – lets say you use iteration so you have the index variable from it, and you define your GlobalId as an attribute:

<aura:attribute name="GlobalId" type="String" access="GLOBAL" description="my id" />
<aura:attribute name="contacts" type="Contact[]" access="GLOBAL" description="list of my standard contacts" />
<aura:iteration items="{!v.contacts}" var="contact" indexVar="index">              
    <a aura:id="{! v.GlobalId + '_tab' + index}"> </a>
</aura:iteration>

Now your controller should get the id correctly

Attribution
Source : Link , Question Author : Akash , Answer Author : Itai Shmida

Leave a Comment