Can anyone please help me to understand the below code. I am not able to understand how we are extracting value from map on visualforce page.
Actually i am trying to get the size of value list which is associated with respective key in map.
<apex:outputPanel > <apex:repeat value="{!attributeWrapper.mapAllAttributesLabel}" var="M"> <div> <div>{!attributeWrapper.mapLabels[M].Name} </div> <div> <ul> <apex:repeat value="{!attributeWrapper.mapAllAttributesLabel[M]}" var="attribute"> <li> <p> <apex:outputLink onclick="selectAttribute('{!attribute.attributeLabel.Label__c}','{!attribute.attributeLabel.Attribute__c}');return false;"> <apex:outputLabel value="{!attribute.attributeLabel.Attribute__r.Name}"/> </apex:outputLink> </p> </li> </apex:repeat> </ul> </div> </div> </apex:repeat> </apex:outputPanel> public with sharing class AttributeWrapper { public Map<String, List<AttributesLabelWrapper>> mapAllAttributesLabel{ // some code to populate this map } }
If i do:
{!attributeWrapper.mapAllAttributesLabel[M].size}
I get an error so how do i get the size of value list on VF page?
Answer
I tried to replicate your problem, and ended up trying different things, no luck doing it your way (confirmed that, got a visualforce error), no luck with <apex:param>
but what I ended up doing was I created a second map, with the same keys, which basically just holds the size of the list in an Integer, and get those values.
Here is my code:
VisualForce Page
<apex:page controller="TestController">
<apex:repeat value="{!testMap}" var="key">
<apex:outputText value="{!listSizeMap[key]}" />
</apex:repeat>
</apex:page>
Apex Controller
public with sharing class TestController
{
public Map<String, List<String>> testMap {get;set;}
public Map<String, Integer> listSizeMap {get;set;}
public TestController()
{
listSizeMap = new Map<String, Integer>();
testMap = new Map<String, List<String>>();
testMap.put('test1', new List<String>{'1', '2'});
testMap.put('test2', new List<String>());
for(String key : testMap.keySet())
{
listSizeMap.put(key, testMap.get(key).size());
}
}
}
Map Explanation (old answer, but perhaps useful for other people)
You can’t do a .size
on a value of the map.
You try to get a single value from the map by adding the square brackets with the variable M {!attributeWrapper.mapAllAttributesLabel[M].size}
which is basically equal to mapAllAttributesLabel.get(M);
in apex.
if you want to get the size of your map just do {!attributeWrapper.mapAllAttributesLabel.size}
which is equal to mapAllAttributesLabel.size();
in apex.
with the apex repeat, you loop over the keys in your map, and the var set by var=”M” defines the current key in your loop. Each time you do mapAllAttributesLabel[M] you get the value of that key in your map.
Attribution
Source : Link , Question Author : Pramod Kumar , Answer Author : pjcarly