1

I'm iterating over a list of Stores(Account) to display all the Stores assigned to an individuals. What I want to achieve is, from the list of Stores if one of list items (Store) is clicked/touched, it should display the details of that account. I have an Array of Store(Account) as an attribute in the component I'm iterating in.

<aura:attribute name="stores" type="Account[]"/>  
<aura:iteration items="{!v.stores}" var="store">
    <c:IndirectStoreCard store="{!store}"/>
</aura:iteration>

As you can see, I have created nested component(c:IndirectStoreCard) which encapsulate all the details of a store. My question is, how do I pass, the selected record i.e.Store to the JS controller. I know how to pass one of the fields (say, Id) to controller using anchor tag, but that would required me to query the other fields from the database and I don't want another database trip, simply because I already have all the data.So, I want pass the entire record.

3 Answers 3

4

When the nested component(c:IndirectStoreCard) is clicked, it fires a component event with a parameter(the selected record).

IndirectStoreCard.cmp:

<aura:attribute name="store" type="Account" />
<aura:registerEvent name="onClick" type="c:messageEvt" />
<div style="cursor:pointer;" onclick="{!c.clickStore}">
  <p>{!v.store.Name}</p>
</div>

IndirectStoreCardController.js:

clickStore: function(component, event, helper) {
  var cmpEvt = component.getEvent("onClick");
  cmpEvt.setParams({
    "store": component.get("v.store")
  }).fire();
}

Then the parent component is

<aura:attribute name="stores" type="Account[]"/>  
<aura:iteration items="{!v.stores}" var="store">
  <c:IndirectStoreCard store="{!store}" onClick="{!c.clickStore}" />
</aura:iteration>

JS controller:

clickStore: function(component, event, helper) {
  var store = event.getParam("store");
  console.log(store);
}
5
  • It's not working for me. When I click on list item, nothing happens. I must be doing something wrong. Are you sure, the event you are firing from the IndirectStoreCard, doesn't require a handler in the parent? Any input on how can I debug the code?
    – Aryansh04
    Commented Apr 9, 2016 at 19:51
  • I also set an alert on IndirectStoreCard controller to check if the event is getting fired. But it's not reaching there.
    – Aryansh04
    Commented Apr 9, 2016 at 19:54
  • If you want to write as below: <c:IndirectStoreCard store="{!store}" /> you need to add a handler in the parent component:<br/> <aura:handler name="onClick" event="c:messageEvt" action="{!c.clickStore}"/> When you set an alert at the first line, does a popup window open? And you try to check whether cmpEvt is not null. If cmpEvt is null, then you will make a mistake in the name of aura:registerEvent.
    – H.Rog
    Commented Apr 9, 2016 at 21:38
  • I set an alert on IndirectStoreCardController.js t to check if cmpEvent is null, but no pop up comes up. What I feel is, the event is not fired, because when I click on the list item, nothing happens, nothing. I also verified the name of aura:registerEvent to where I'm accessing it. It's all correct. Please find all the code here: codepaste.net/95uxp5
    – Aryansh04
    Commented Apr 10, 2016 at 14:41
  • You try to change <div class="slds-card" style="cursor:pointer;" onClick="{!c.goToStoreDetail}"> to <div class="slds-card" style="cursor:pointer;" onclick="{!c.goToStoreDetail}">. Not "onClick" but "onclick"
    – H.Rog
    Commented Apr 11, 2016 at 12:21
3

I figured another way to achieve this. There is no need to create a child component:

    <aura:attribute name="stores" type="Account[]"/> 
    <aura:iteration items="{!v.stores}" var="store" indexVar="idx">
    <div class="slds-card"  data-record="{!idx}"  onclick="{!c.goToStoreDetail}">
        <header class="slds-card__header slds-grid grid--flex-spread">    
            <h2 class="slds-text-heading--medium slds-truncate">{!store.Name}</h2>
        </header>            
    </div>                                                
</aura:iteration>

As you can see, I have used indexVar attribute in the iteration. I then use the index values in the data attribute of Div. Now in the JS Controller you can use the following code to retrieve the record based on the index.

     var selectedItem = event.currentTarget; // Get the target object
     var index = selectedItem.dataset.record; // Get its value i.e. the index
     var selectedStore = component.get("v.stores")[index]; // Use it retrieve the store record 

Use console.log to verify the results.

1

Please check below link whether it is useful

How to Navigate to the record/get the record id in <Aura:iteration>

If you still have problem, provide snippet of your code

You must log in to answer this question.

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