+ Start a Discussion
Abdul PatelAbdul Patel 

Fetch Record Id from the aura iteration in lightning component

I am trying to show an edit pop up when someone clicks on the icon shown with aura iteration. Below is my code -

<aura:iteration items="{!v.campaign.Campaign_Members__r}" var="cm">
<tr>
   <td class="slds-text-align_center">
   <div id="{!cm.Id}" class="{!'btn-editStatus cmId-'+cm.Id}" onclick="{!c.handleEdit}">
          <lightning:icon size="x-small" iconName="utility:edit"/>  
   </div>
   </td>
</tr>
</aura:iteration>

How should I get the Id of the record (cm.Id) in the handleEdit function in the lightning controller?
Best Answer chosen by Abdul Patel
lnallurilnalluri
aah typo, my bad

try this.
<aura:iteration items="{!v.campaign.Campaign_Members__r}" var="cm">
<tr>
   <td class="slds-text-align_center">
   <div data-id="{!cm.Id}" class="{!'btn-editStatus cmId-'+cm.Id}" onclick="{!c.handleEdit}">
          <lightning:icon size="x-small" iconName="utility:edit"/>  
   </div>
   </td>
</tr>
</aura:iteration>
var recId = event.currentTarget.dataset.id;




 

All Answers

Rounak SharmaRounak Sharma
hello abdul,
Please use 
<aura:component implements="force:lightningQuickAction,force:hasRecordId">
and 
<aura:attribute name="recordId" type="String" />

by this you can get the id of the record.
Please let me know if you still need help
thanks
Abdul PatelAbdul Patel
Hi Rounak, The component is trying to show all the campaign members of one campaign. I want the id of individual member for showing edit popup. Your suggestion will give me just campaign id. Thanks for your help. Abdul
lnallurilnalluri
@abdul 

try this
 
<aura:iteration items="{!v.campaign.Campaign_Members__r}" var="cm">
<tr>
   <td class="slds-text-align_center">
   <div data-id="{!cm.Id}" class="{!'btn-editStatus cmId-'+cm.Id}" onclick="{!c.handleEdit}">
          <lightning:icon size="x-small" iconName="utility:edit"/>  
   </div>
   </td>
</tr>
</aura:iteration>

In the handleEdit Js
 
var selectedItem = event.currentTarget; // or event.target
 var recId = selectedItem.dataset.record;

 
Abdul PatelAbdul Patel
@Inalluri: Thank you for your help. I am still not able to get the ID in my lightning controller. In your code, recId is shown undefined in the console log. Did we miss something here?
lnallurilnalluri
aah typo, my bad

try this.
<aura:iteration items="{!v.campaign.Campaign_Members__r}" var="cm">
<tr>
   <td class="slds-text-align_center">
   <div data-id="{!cm.Id}" class="{!'btn-editStatus cmId-'+cm.Id}" onclick="{!c.handleEdit}">
          <lightning:icon size="x-small" iconName="utility:edit"/>  
   </div>
   </td>
</tr>
</aura:iteration>
var recId = event.currentTarget.dataset.id;




 
This was selected as the best answer
Abdul PatelAbdul Patel
@lnalluri: No success yet :( It shows undefined again. You need other details?
lnallurilnalluri
you sure you using data-id="{!cm.Id}" in the markup? It is working in mine.
Abdul PatelAbdul Patel
Thank you @Inalluri. It worked. Can you share some documentation related to this. I want to learn this in detail so that I should not trouble you again :)