1

How do I iterate over a map of list using aura:iteration. I'm returning Map<object,List<string>>from apex, but not sure how do I pass it to the component. To Be more clear I'm trying to get the accounts by created date. If I use

 <aura:iteration items="{!v.accountlist}" var="acc">
       {!acc.createddate}
       <hr/>
       {!acc.Name}
    </aura:iteration>

the created date will be printed once for each record, if there are multiple accounts created on the sameday. Instead I want to display the all the records based on the createddate.

Apex:

return [SELECT createddate,name FROM Account]

I modified the query to return Map<createdDate, List<Account>>

  • Could you share a working example having the issue? – Raul Jan 13 '18 at 18:20
  • I don't have a working example, I'm returning a map of list from apex but not sure how do I manipulate it in the controller. I have updated my question – cartman Jan 13 '18 at 18:27
6

Try the below: This code is with the assumption you have multiple keys for different date with list of accounts for each date

var accountmap = component.get('v.accountList');
var accountList = [];

for ( var key in accountmap ) {
     accountList.push({accountdate:key, value:accountmap[key]});
     }

set accountList to an attribute in your component.

In your markup

 <aura:iteration items="{!v.accountlist}" var="acc">
       {!acc.accountdate}
      <aura:iteration items="{!acc.value}" var="account">
       <p>{!account}</p>
      </aura:iteration>

    </aura:iteration>
  • tried..its working perfectly :) – Arpit Sethi Jan 14 '18 at 19:28
  • I was just trying this, component.get('v.accountList'); is always going to be null and it doesn't enter the for loop, or am I missing something? – cartman Jan 15 '18 at 11:16
  • if accountlist is coming from a init call on the component. inside callback get that value and then run the loop. first time on init the accountList will be null. you will only be setting it inside callback – RedDevil Jan 15 '18 at 11:22
-3

for this you have to send a SET<key> and Map<key,list<string>> from apex controller. in controller js you have set the values into to attribute.

and do nested iteraton.

<aura:iteration item="map" var="obj">
<aura:iteration item="set" var="obj1">
  {!obj.obj1} // here you getting the list 
</aur:iteration>
</aura:iteration>
  • Your answer is not clear nor aligned to what OP is asking. – Raul Jan 13 '18 at 18:37
  • let me know where you getting block – Sarvesh Jan 13 '18 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.