You need to sign in to do that
Don't have an account?
Determine size of a list in a map in Visualforce
I have a map of this structure...
private Map<String,List<MyObject__c>> mymap = new Map<String,List<MyObject__c>>();
In order to keep things generic, it is not clear at all, how many string->list pairs there are going to be in the map. Now, if I repeat over one of these lists in a VisualForce page like this...
<apex:repeat value="{!mymap['someindex']}" var="el"> </apex:repeat>
...how am I going to determine the actual size of mymap['someindex']? Eventually I want to do something as simple as this...
<li class="{!IF(count=mymap['someindex'].size,'active','')}">
However, the size method of a list does not seem to work in VisualForce. Anyways, this procedure already seems to require for me to wrap this stuff with a quite redundant count variable like so...
<apex:variable var="count" value="{!0}"/> <apex:repeat value="{!mymap['someindex']}" var="el"> <apex:variable var="count" value="{!count + 1}"/> <li class="{!IF(count=mymap['someindex'].size,'active','')}"> </apex:repeat>
So, how would you do this, because my current solution looks like this and is far from generic at all and - as for my tastes - does not look good at all...
<apex:repeat value="{!mymap['myindex']}" var="el"> <li class="{!IF(el.Property__c=='knownPropertyOfLastElementInList','last','')}"> ... </li> </apex:repeat>
...or is there a way to declare a getter in my controller that can receive parameters, somewhat like this...
<li class="{!IF(count=listlength('mymap','someindex'),'active','')}">
Cheers, Tobias.
You need a wrapper class for this:
At this point, you should be able to do something like this:
This should work with the advent of Dynamic Visualforce, assuming I have my syntax correct. A wrapper class is almost certainly necessary.
The other alternative would be:
This one leaves your code alone, and you don't even have use the funky code anymore, just:
There's definitely ways to get where you want ot be. Just think encapsulation, and you'll be fine.
All Answers
You need a wrapper class for this:
At this point, you should be able to do something like this:
This should work with the advent of Dynamic Visualforce, assuming I have my syntax correct. A wrapper class is almost certainly necessary.
The other alternative would be:
This one leaves your code alone, and you don't even have use the funky code anymore, just:
There's definitely ways to get where you want ot be. Just think encapsulation, and you'll be fine.
sfdcfox,
I have a similar issue.
How can I use {!myMap.values} in VF page to iterate over the list returned by values() ? I can use values() in APEX, but not in VF page.
Same goes for {!myMap.size}.
regards
ISVForce Partner
AFAIK, you can't access Map.size() nor Map.values() directly. You can, however, do the following:
This helped in getting the map size and iterating it in VF page.
thank you.
Hey sfdcfox, I have another problem with wrapper class in map described here . Can you take a look on this? Thank you.
Karel.
I know, your problem is solved six years ago, but I have something to add to your topic.
I faced with the same problem, but my colleague said that creating a wrapper class only for field is wrong solution. She offered another solution I found best for this case.
My controller: The solution is to create a variable in VF page and call size property on it