function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ashish jadhav 9ashish jadhav 9 

How can I display list/set/map values on VF page? & how to iterate those values?

Please guide how to display collection items on vf page with iteration. Also while developing this what things I must remember?
Best Answer chosen by ashish jadhav 9
mritzimritzi
The same way as List and Map has been used.
Apex:
public class SampleClass{
	public Set<Account> variableName{get;set;}
	public SampleClass(){
		variableName = [Select id,Name,Phone From Account LIMIT 10];
	}
	public void someFunction(){
		
	}
}
VF:
<apex:page controller="SampleClass">
<apex:form>
	<apex:repeat value="{!variableName}" var="var">
		<apex:inputField value="{!var.Name}"/>
		<apex:outputText value="{!var.Phone}"/>
		<!-- you can add more fields, to simply display content use outputText, to get input get inputField -->
	</apex:repeat>
</apex:form>
</apex:page>


 

All Answers

Dhanya NDhanya N
Hi Ashish,

You can iterate them using <apex:repeat> tag in vf page.
Refer this link :
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_maps_lists.htm

Thanks,
Dhanya
mritzimritzi
Sample code for List

Apex:
public class SampleClass{
	public List<Account> accountList{get;set;}
	public SampleClass(){
		accountList = [Select Name, Phone From Account LIMIT 10];
	}
	public void someFunction(){
		
	}
}

VF:
<apex:page controller="SampleClass">
<apex:form>
	<apex:repeat value="{!accountList}" var="ac">
		<apex:inputField value="{!ac.Name}"/>
		<apex:outputText value="{!ac.Phone}"/>
		<!-- you can add more fields -->
	</apex:repeat>
</apex:form>
</apex:page>


Sample code for Map

Apex:
public class SampleClass{
	public Map<Id,Account> accountMap{get;set;}
	public SampleClass(){
		accountMap = new Map<Id,Account>([Select id,Name,Phone From Account LIMIT 10]);
	}
	public void someFunction(){
		
	}
}


VF:
<apex:page controller="SampleClass">
<apex:form>
	<apex:repeat value="{!accountMap}" var="key">
		<apex:inputField value="{!accountMap[key].Name}"/>
		<apex:outputText value="{!accountMap[key].Phone}"/>
		<!-- you can add more fields -->
	</apex:repeat>
</apex:form>
</apex:page>

Make changes to suit your needs.

mark it as Best Answer, if this solves your problem.
 
ashish jadhav 9ashish jadhav 9
Thanks for help, but can you please tell me how can I use it for set?
mritzimritzi
The same way as List and Map has been used.
Apex:
public class SampleClass{
	public Set<Account> variableName{get;set;}
	public SampleClass(){
		variableName = [Select id,Name,Phone From Account LIMIT 10];
	}
	public void someFunction(){
		
	}
}
VF:
<apex:page controller="SampleClass">
<apex:form>
	<apex:repeat value="{!variableName}" var="var">
		<apex:inputField value="{!var.Name}"/>
		<apex:outputText value="{!var.Phone}"/>
		<!-- you can add more fields, to simply display content use outputText, to get input get inputField -->
	</apex:repeat>
</apex:form>
</apex:page>


 
This was selected as the best answer
ashish jadhav 9ashish jadhav 9
Thanks for help Mritzi
mritzimritzi
Please note -> That you can iterate over Set using apex:repeat when Set stores basic data types, not in case of sObjects.
Set<String> will work, Set<Account> seems not to be working.

To populate Set from DB, you can use this:
variablename = new Set<Account>([Select id,name,phoen From Account LIMIT 10]);
 
Raja VelmuruganRaja Velmurugan
I'm working with 2 custom objects A and B. A has only one Text Field. B has 4 Text Fields. My controller program is such that every A's Field has been mapped to one of the 4 Text Fields in B. During system.debug I can see all the mapped values. But I couldnt get a proper way to print the same mapped values in a visualforce page. Is there a way to print my map in a vfp?