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
RockersRockers 

How to populate collection of data on visualforce pages..

Hi,

 

   I have List Collection type on one object. Now, i want to print those values of object one by one on a visual force page.

 

   how, could i place record by record (say.,

 

     1st record

 

     2nd record

 

     3rd record..........)

 

     Thanks in advance........

 

David WrightDavid Wright

There are a few ways you can do this--to answer your questions more specifically I'd need some more details, but here's a simple way to do it:

 

1. Identify the list of data you want to iterate through. As an example, lets pretend we have an account, and we'll grab the list of contacts off of it.

 

Account acc = [SELECT Id, (SELECT Id, FirstName, LastName, Email FROM Contacts) FROM Account LIMIT 1];

Now the list you want to iterate through is the collection of contacts on this account.

 

2. Iterate over your data in a pageblock table

In visual force, you can reference this list in several elements--one of my favorites is <apex: pageBlockTable>

<apex:pageBlock>
    <apex:pageBlockTable value="{!acc.Contacts}" var="contact">
        <apex:column value="{!contact.FirstName}" />
        <apex:column value="{!contact.LastName}" />
        <apex:column value="{!contact.Email}" />
    </apex:pageBlockTable>
</apex:pageBlock>

 

Visualforce will do the wotk for you here. If you have 5 items in the list, you'll get 5 rows each with 3 columns (first name, last name, email). Note that the "value" attribute refers to the list we want to display. You can replace that value with whatever refers to the list of data you want to iterate through.

 

If your needs are different, you can also take a look at <apex: dataTable> or <apex: dataList>

 

Hope that helps!