1

I've got an aura iteration that looks like this:

<aura:component description="ResourceTiles" implements="forceCommunity:searchInterface" controller="MainBrowser">

<aura:attribute name="resources" type="Resource__c[]" access="public" />

<aura:attribute name="resource" type="Resource__c" access="public" default="{'sobjectType': 'Resource__c', 'Name': 'Volvo XC90', 'Comment__c': ''}"/>
<aura:attribute name="selectedResourceId" type="Id" access="public" />
<aura:registerEvent name="ResourceSelected" type="c:ResourceSelected" />


<div class="slds-scrollable--x">
    <table class="slds-table slds-table--bordered slds-table--cell-buffer slds-table--striped">
    <thead>
        <tr class="slds-text-title_caps">
            <th scope="col"><div class="slds-truncate" title="Car Selection"></div></th>
            <th scope="col"><div class="slds-truncate" title="Model">Car Model</div></th>
            <th scope="col"><div class="slds-truncate" title="Engine Information">Engine</div></th>

        </tr>
        </thead>
        <tbody>
        <aura:iteration items="{!v.resources}" var="resource">
            <tr>
                <td>
                    <lightning:button label="{!resource.Id}" aura:id="allButtons" onclick="{!c.changeColor}" value="{resource.Id}">
                    </lightning:button>
                </td>
                <td>
                    <div class="slds-truncate" title="{!resource.Name}">
                        {!resource.Name}
                    </div>
                </td>
                <td>
                    <div class="slds-truncate" title="{!resource.Comment__c}">
                        {!resource.Comment__c}
                    </div>
                </td>
            </tr>
        </aura:iteration>
        </tbody>
    </table>
</div>

and a Controller that looks like this:

changeColor: function(component, event, helper) { 
    var resource = event.getSource().get("v.value");
    var event = component.getEvent("ResourceSelected");
    event.setParams({
        resourceId: resource.Id
    });
    event.fire();

Why am I receiving undefined for the resource Id? The label on the button itself is successfully displaying the record ID. I'm just having issues passing it to my controller - what am I missing here?

3
  • 1
    You have same variable name resource in your aura:attribute and aura:iteration. Try changing one of them?
    – Raul
    Commented Apr 29, 2018 at 14:52
  • 1
    Looks like you're assigning the value of the button to the resource's Id, but you're then taking v.value.Id within your event handler. Type mismatch?
    – David Reed
    Commented Apr 29, 2018 at 14:53
  • try this option in change color method ,,,var Val = event.target.value; or try event.currentTarget.value
    – Annappa PH
    Commented Apr 29, 2018 at 14:58

2 Answers 2

1

You have same variable name resource in your aura:attribute and aura:iteration. Try changing one of them?

0

Thanks for the help guys. The issue was that I had an old attribute with the same name as the iteration variable. I just removed the part.

1
  • 1
    You might accept Raul's answer if that was the solution to the issue.
    – David Reed
    Commented Apr 29, 2018 at 15:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .