Activity › Forums › Salesforce® Discussions › How to iterate map in Salesforce Lightning Component?
Tagged: A EnqueueAction, Aura Component, Aura Handlers, Aura Iteration, AuraEnabled, List, Map, Mapping, Salesforce Error, Salesforce Instance, Salesforce Lightning, Salesforce Lightning Components
-
How to iterate map in Salesforce Lightning Component?
Posted by Tanu on September 21, 2016 at 2:13 PMHi All,
Is it possible to iterate over a Map in an aura:iteration. It doesn’t give me a compile or runtime error but it doesn’t appear to work.
Parul replied 7 years, 9 months ago 3 Members · 2 Replies -
2 Replies
-
Hi Tanu,
You can only defined to iterate over a List, According to the Aura docs.
If you have not seen the Aura docs app before, it is available with any Salesforce instance by modifying the URL:
[your instance stuff].salesforce.com/auradocs/reference.app
Or to navigate to the page for the iteration component:
[your instance stuff].salesforce.com/auradocs/reference.app#reference?descriptor=aura:iteration&defType=component
Hope this helps you.
Thanks
- [adinserter block='9']
-
<aura:component controller=”MyMapClass”>
<aura:handler name=”init” value=”{!this}” action=”{!c.doInit}”/>
<aura:attribute name=”myMap” type=”Map” />
{!v.myMap.key1} <br/>
{!v.myMap.key2}
</aura:component>
({
doInit: function(component, event, helper) {var action = component.get(‘c.getMyMap’);
action.setCallback(this,function(response){
alert(JSON.stringify(response.getReturnValue()));
component.set(‘v.myMap’,response.getReturnValue());
});
$A.enqueueAction(action);
}
})
public class MyMapClass {
@AuraEnabled
public static map<String,String> getMyMap(){
Map<String,String> Mymap = new Map<String,String>();
Mymap.put(‘key1′,’apple’);
Mymap.put(‘key2′,’mango’);
return Mymap;
}
}
here is a simple example to use map in lightning component and use @AuraEnabled in you apex class method
component
Log In to reply.