[SalesForce] Get record Id in aura:iteration

I am looping through a list of records in a lightning component. I want to get the record Id for the selected checkbox record in the js controller.

<aura:iteration var="sw" items="{!v.swList}">
    <tr>
        <td >
            <div class="slds-truncate" >{!sw.contact.Name}</div>
        </td>
        <td >
            <div class="slds-truncate">{!sw.contact.Account__c}</div>
        </td>
        <td >
            <div class="slds-p-left_large">
                <lightning:input type="checkbox" checked="{!sw.contact.Optin__c}" onclick = "{!c.optIn}"/>
            </div>
        </td>
    <tr>
</aura:iteration>

Best Answer

in your iteration, you might want to set the id to some attribute and use an onchange event handler, for example:

<lightning:input type="checkbox" checked="{!sw.contact.Optin__c}" value="{!sw.contact.Id}" onclick = "{!c.optIn}"/>

and use event.getSource().get('v.value') in the client-side controller to get the button components value that was triggered the onchange event

Related Topic