You need to sign in to do that
Don't have an account?
How to access value from a MAP in lightning component?
Hi I have a following lightning component code.
============My Component =============
<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" controller="MyClass">
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="myMap" type="Map" />
</aura:component>
======================================
The map 'myMap' is loaded with some data through the controller.
Im not getting how will I access the value from the map "myMap" inside the component.
I dont want to iterate over the map
Just need to access a value from It something like myMap.get('Key1') inside the component;
=========MyComponentController==========
({
doInit: function(component, event, helper) {
var action = component.get('c.getMap');
action.setCallback(this,function(result){
component.set('v.myMap',result.getReturnValue());
});
$A.enqueueAction(action);
}
})
============================
==========MyClass=============
public with sharing class MyClass
{
public static MAP<String,String> getMap()
{
Map<String,String> map = new Map<String,String>();
map.put('key1','value1');
map.put('key2','value2');
return map;
}
}
=================================
============My Component =============
<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" controller="MyClass">
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="myMap" type="Map" />
</aura:component>
======================================
The map 'myMap' is loaded with some data through the controller.
Im not getting how will I access the value from the map "myMap" inside the component.
I dont want to iterate over the map
Just need to access a value from It something like myMap.get('Key1') inside the component;
=========MyComponentController==========
({
doInit: function(component, event, helper) {
var action = component.get('c.getMap');
action.setCallback(this,function(result){
component.set('v.myMap',result.getReturnValue());
});
$A.enqueueAction(action);
}
})
============================
==========MyClass=============
public with sharing class MyClass
{
public static MAP<String,String> getMap()
{
Map<String,String> map = new Map<String,String>();
map.put('key1','value1');
map.put('key2','value2');
return map;
}
}
=================================
here is a simple example to use map in lightning component and use @AuraEnabled in you apex class method
component jscontroller
apex class these code show
Apple
Mango
in your component screen
I hop it helps you if it helps you Mark it best answer so it make proper solution for other
Thanks :)
Thank you for your solution.
It works when I am using {!v.myMap.key1}.
I want further to use the Key part dynamically from the map.
======================================================
<aura:component controller="MyMapClass">
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="myMap" type="Map" />
<aura:attribute name="myList" type="List" />
<aura:iteration items="{!v.myList}" var="myVar">
<li >
{!v.myMap.myVar.Name} // ----- Can I use Something like this???
</li>
</aura:iteration>
</aura:component>
===============================================================================
Where myList is a List of 'Topic' Object having the field 'Name'.
Whatever present in the Name field, I want to use it as a Key in the Map.
Thanks :)
but you can iterate a map values on <aura:iteration> go to below link
http://www.sfdcmonkey.com/2017/01/13/iterate-map-values-lightning-component/
i hop it helps you let me inform if it helps you
Thanks
How to iterate map in lightning component. And how display values by iterating map in lightning component?
First as mentioned by above and second by iterating over map using aura iteration.
This will help on this,
https://www.sfdc-lightning.com/2018/09/how-to-use-map-in-lightning-component.html
So, we either have to iterate over it.... Or hardcode the values?! Those are the only solutions?
What is the point of even supporting maps if we cant use the keys dynamically.
That is such a fundamental design flaw.