+ Start a Discussion
SankeshReddy NallamilliSankeshReddy Nallamilli 

Create a Map with Student name as key and Student phoneNo as value and display the map data using pageblockTable in the VisualForce Page.Kindly provide solution.

Best Answer chosen by SankeshReddy Nallamilli
{!pramod_nishane}{!pramod_nishane}
Hi SankeshReddy,

Try this code:-

Page:- 

<apex:page controller="createMap">
<apex:form id="formId">
    <apex:pageBlock id="pgBlockId">
        <apex:pageBlockTable value="{!namePhoneMap}" var="item"  >
                <apex:column value="{!item}"/>  <!-- key --->
                <apex:column value="{!namePhoneMap[item]}"/>  <!-- Value--->
      </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>


Class:-
public class createMap{
    public Map<String,String> namePhoneMap{get;set;} 
    public List<Account> accList{get;set;}
    public createMap(){
        accList = new List<Account>();
        namePhoneMap = new Map<String,String>();
        accList = [select id,name,phone from Account limit 10];
        if(accList != null && accList.size() > 0){
            for(Account acc : accList ){
                namePhoneMap.put(acc.name,acc.phone);
            }
        }
    }
}

Output:-

User-added image

Let me know in case of any concerns.

Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.

Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com

All Answers

Rajat BurmanRajat Burman
Hi SankeshReddy,

You need to create a map like below:

public static Map<String, Integer> studentMap = new Map<String, Integer>{
             'John' =>     9866565656,
             'Sara' => 9866565658,
             'Ron' =>  9861569552,
             'Harry' =>   9861519552,
          };


You have to use apex:repeat tag as below:

Visualforce code goes like:

<apex:repeat value="{!studentMap }" var="key">
     <apex:column headerValue="{!key}"/> <!-- key --->
    <apex:column value="{!studentMap [key]}"/> <!-- Value--->
</apex:repeat>

Thanks.
{!pramod_nishane}{!pramod_nishane}
Hi SankeshReddy,

Try this code:-

Page:- 

<apex:page controller="createMap">
<apex:form id="formId">
    <apex:pageBlock id="pgBlockId">
        <apex:pageBlockTable value="{!namePhoneMap}" var="item"  >
                <apex:column value="{!item}"/>  <!-- key --->
                <apex:column value="{!namePhoneMap[item]}"/>  <!-- Value--->
      </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>


Class:-
public class createMap{
    public Map<String,String> namePhoneMap{get;set;} 
    public List<Account> accList{get;set;}
    public createMap(){
        accList = new List<Account>();
        namePhoneMap = new Map<String,String>();
        accList = [select id,name,phone from Account limit 10];
        if(accList != null && accList.size() > 0){
            for(Account acc : accList ){
                namePhoneMap.put(acc.name,acc.phone);
            }
        }
    }
}

Output:-

User-added image

Let me know in case of any concerns.

Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.

Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com
This was selected as the best answer