How to access a map with a list in VF?

In my VF page, I’m iterating a over a list of items, and I want to add some additional functionality. I created a Map <ID, List <ID>> in Apex. How do I access this data structure in the VF page? Each list will have 0 or many elements, and I only want to render those elements if there are any

Existing VF:

 <apex:pageBlockTable value="{!theSearchResults}" var="item" id="allData" >
     <apex:column  width="50px">
         <apex:facet name="header">Action</apex:facet>
         <apex:commandButton value="Detail" id="btn" onclick="myActFunc2('{!item.id}')" reRender="pagination"/>
     </apex:column>                      
 </apex:pageBlockTable>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can easily iterate through the elements (values) inside your map as long as the key matches to your item.Id from your current list used in the page block table.

<!-- loop through the keys in the map -->
<apex:repeat value="{!yourMap}" var="key">
    <!-- get the value from the map using this key -->
    <apex:repeat value="{!yourMap[key]}" var="mapList" rendered="{!yourMap[key].size != 0}">
        <!-- mapList is the value from your map - a list of IDs, loop through the list -->
        <apex:repeat value="{!mapList}" var="listElement">
            <apex:outputText value="{!mapElementId}" />
        </apex:repeat>
    </apex:repeat>
</apex:repeat>

It’s not very clear on what exactly you’re trying to do here. If you want to dynamically render rows in your table based on the list size in the map, then you’ll need to use nested <apex:repeat> rather than <apex:pageBlockTable> to generate the table.

Keep in mind that for every element in your theSearchResults list, you must have an entry in your map, otherwise you’ll get a null de-reference error on your page.

Method 2

You can try something like below

Map<Id, List<Id>> mapSearchResults;

List<id> theSearchResults=new List<Id>();

List<List<Id>> allSearchResults=mapSearchResults.values();

for(List<Id> li:allSearchResults)
{
    theSearchResults.addAll(li);
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x