Tips: Iterating a map in Visualforce page

Posted in

The common solution to display a list of values or SObject’s field value in Visualforce page is looping through a List. For example:

 

Controller method

List keyList = new List{'value for first','value for second','value for third'};

public List getKeyList(){
    return keyList;
}

 

Visualforce page

    
    


 

However, sometimes we may need to construct our data into a map instead and have it displayed in Visualforce page. Here is how you can loop through a map and it’s value:

 

Controller method

public Map getDemoMap(){

    Map demoMap = new Map();
    demoMap.put('first', 'value for first');
    demoMap.put('second', 'value for second');
    demoMap.put('third', 'value for third');
    demoMap.put('forth', 'value for forth');

    return demoMap;
}

 

Visualforce page

    
        
        
   

Get in touch today to see how WDCi can help your business.