10

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?

17

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.

  • 1
    correct. i will get value each time by using mapAllAttributesLabel[M]. and as value is it self a list and i what that list size. i want to get size of a value list associated with a key. Is there not a way to get size of value list? – Pramod Kumar Mar 7 '13 at 8:09
  • aha! well, theoretically i think that should work then, could be that it's not possible with Visualforce. Let me test it out. – pjcarly Mar 7 '13 at 8:30
  • I just tested it out, and I dont think its possible to do it directly from visualforce, I tried to fiddle around trying to get it to work with an <apex:param> but unfortunately that didn't work either, what I ended up doing, and got it to work will be added to my answer. – pjcarly Mar 7 '13 at 8:49

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.