How to iterate list in Salesforce Visualforce Page?

How to iterate list in Salesforce Visualforce Page?

Page Block Table, Data Table, Data List and Apex Repeat can be used to render list in Visualforce page. Check the sample code for reference.

Sample Code:

Visualforce Page:

<apex:page controller="SamplePageController">  
    <apex:pageBlock title="Page Block Table">
        <apex:pageBlockTable value="{!listAccounts}" var="acc">
            <apex:column value="{!acc.Name}"/>
            <apex:column value="{!acc.Type}"/>
            <apex:column value="{!acc.Industry}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:pageBlock title="Data Table">    
        <apex:dataTable value="{!listAccounts}" var="acc" border="2">
            <apex:column value="{!acc.Name}"/>
            <apex:column value="{!acc.Type}"/>
            <apex:column value="{!acc.Industry}"/>
        </apex:dataTable>
    </apex:pageBlock>
    <apex:pageBlock title="Data List">   
        <apex:dataList value="{!listAccounts}" var="acc">
            {!acc.Name} - {!acc.Type} - {!acc.Industry}
        </apex:dataList>
    </apex:pageBlock>
    <apex:pageBlock title="Apex Repeat">   
        <apex:pageBlockSection columns="3">
            <apex:repeat value="{!listAccounts}" var="acc">
                <apex:pageBlockSectionItem >{!acc.Name}</apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >{!acc.Type}</apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >{!acc.Industry}</apex:pageBlockSectionItem>
            </apex:repeat>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Apex Class:

public class SamplePageController {
    
    public List < Account > listAccounts { get; set; }
    
    public SamplePageController() {
        
        listAccounts = [ SELECT Id, Name, Industry, Type FROM Account LIMIT 5 ] ;
        
    }

}

Output:

Leave a Reply