Thursday, June 29, 2017

How to use apex:repeat in Visualforce Page?

How to use <apex:repeat>  in Visualforce Page?

<apex:repeat>:

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

Note that if used within an < apex:pageBlockSection > or < apex:panelGrid > component, all content generated by a child < apex:repeat > component is placed in a single < apex:pageBlockSection > or < apex:panelGrid > cell.

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 >

This tag supports following attributes:

Attribute
Description
first
The first element in the collection visibly rendered, where 0 is the index of the first element in the set of data specified by the value attribute. For example, if you did not want to display the first two elements in the set of records specified by the value attribute, set first="2".
id
An identifier that allows the repeat component to be referenced by other components in the page.
rendered
A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true.
rows
The maximum number of items in the collection that are rendered. If this value is less than the number of items in the collection, the items at the end of the collection are not repeated.
value
The collection of data that is iterated over.
var
The name of the variable that represents the current item in the iteration.

Visualforce Example:

<apex:page controller="repeatCon" id="thePage">
     <apex:repeat value="{!strings}" var="string" id="theRepeat">
         <apex:outputText value="{!string}" id="theValue"/> 
         <br/>
     </apex:repeat>
 </apex:page>

Controller class:

public class repeatCon {
       public String[] getStrings() {
         return new String[]{'ONE','TWO','THREE'};
     }
 }

Repeater Usage in Visualforce Page.


Hi,
In this post i am giving an example of how to use <apex:repeat> tag in Visualforce page.


Controller Class: 

Public class TableController{
 public List<Account> accList{get;set;}

 Public TableController(){
  accList= new List<Account>();
  accList= [select id,name,type,industry from Account];
 }
}

Visualforce Page Class:

<apex:page controller="TableController" showheader="true" sidebar="false">
 <apex:form >
   <table border="1" width="650Px">
    <tr> 
      <th> Account Name</th>
      <th> Type  </th>
      <th> Industry</th>
    </tr>
    
    <apex:repeat value="{!accList}" var="a">
      <tr>
        <td> {!a.Name} </td>
        <td> {!a.type} </td>
        <td> {!a.industry} </td>
      </tr>
     </apex:repeat>
   </table>

 </apex:form>
 </apex:page>


Test Class:

@isTest
Public class PagblockTable_Test {
 static testmethod void testPageblockTable(){
  //Test converage for the myPage visualforce page
  PageReference pageref = Page.RepeatorExample;
  Test.setCurrentPageReference(pageref);
  
  // create an instance of the controller
  TableController tc= new TableController();
  
 }
}


out put :

save image



If we observe the output of VF page while using <apex:repeat> tag there is no default salesforce styles applied. suppose if you use pageBlock table then salesforce default styles will apply. if you want to apply your custom styles for table and columns you can apply with <apex:repeat> tag.



3 comments:

  1. Hello Everyone,
    Could you please suggest on how to hide the Input Text within the Apex Repeat (based on selected option within another Input field of repeat).

    I have 2 columns inside Repeat.
    First column displays Picklist values using InputField.
    Second column is an InputText (this should be visible, only when the user selects "Other" value within the first column dropdown).

    Please suggest me.

    Thank you..
    Raghava

    ReplyDelete
  2. User ReRender tag with boolean as value.

    ReplyDelete