Wednesday 11 September 2013

Uses Of Apex Repeat


An iteration component that allows you to display the contents of a collection according to a structure that you specify. The collection can include up to 1,000 items.

This component cannot be used as a direct child of the following components:
<apex:dataTable>
<apex:pageBlockTable>
<apex:panelBar>
<apex:selectCheckboxes>
<apex:selectList>
<apex:selectRadio>
<apex:tabPanel>

Suposse there is a requirement, you need to display Five accounts with thier Contacts in a visualforce page.Format should be like this image


How will you do it?
It is very simple, just use repeat in visualforce page.
Here is visualforce page and controller

<apex:page controller="UseofRepeatOnAccountController" sidebar="false" tabStyle="Account">
    <apex:form >
    <apex:pageBlock title="Accounts with assoicated Contacts">
        <apex:repeat value="{!accountList }" var="acc">
            <apex:pageBlockSection title="{!acc.name}">
                <apex:pageBlockTable value="{!acc.contacts}" var="con">
                    <apex:column value="{!con.Firstname}"/>
                    <apex:column value="{!con.Lastname}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:repeat>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Controller is..
public class UseofRepeatOnAccountController {
    public List<Account> accountList{get;set;}
   
    public UseofRepeatOnAccountController() {
        accountList = new List<Account>();
        accountList = [SELECT id, Name,(SELECT LastName ,FirstName From Contacts)From Account limit 5] ;
    }
}

No comments: