Populate simple Visualforce table

The following code demonstrates the key concepts in creating a custom controller and linking it to a Visualforce page to return a set of data.

Custom Controller:
public class mySecondController {

/// Create a list
List<TIDev__Project_Requirements__c> rqs;

/// getReq() - Reffer to the list in the
/// visualforce page with "Req" minus the "get" from
/// controller name.
public List<TIDev__Project_Requirements__c> getReq() {

/// Get the data for the list
rqs = [select id, TIDev__Requirement__c, TIDev__Due_Date__c, TIDev__Assigned_to__c from TIDev__Project_Requirements__c Where TIDev__Object__c = 'Account'];
return rqs;

The custom controller is simply a class with the "get" method invoked. In our example it is called "getReq()". This method quries Salesforce.com for a set of records and returns it to the Visualforce page in a List. The Visualforce page will be able to access this list by referencing the the name of the method minus the "get". So in our example it would be "Req".

Visualforce Page:

<apex:page showHeader="false" sidebar="false" controller='mySecondController'>
<apex:pageBlock title="Requirements" id="tblId">
<apex:dataTable value="{!Req}" var="requirements" cellPadding="4" border="1">
<apex:facet name="header">Account Requirement</apex:facet>
<apex:outputField value="{!requirements.TIDev__Requirement__c}"/>
<apex:facet name="header">Assigned to</apex:facet>
<apex:outputField value="{!requirements.TIDev__Assigned_to__c}"/>
<apex:facet name="header">Due Date</apex:facet>
<apex:outputField value="{!requirements.TIDev__Due_Date__c}"/>

The first step in your Visualforce page is to link that page to the custom controller you created. This is done with the "controller" tag in the tag.

Next the List is referenced in the with 'value="{!Req}"'. The data in that list is assigned to the the variable "requirements" and then referenced in the data table using standard Salesforce.com dot notation "{!requirements.TIDev__Requirement__c}".


Angeldust said...

great sample! thanks :D
but i just wondering.. what if i want to display 2 table like account and contact on the same row? is it possible on visualforce?

Ivans said...

Super example and explanation!
Thank you!
If all VisualForce tutorial would be explained this way, it would be much, much easier to learn it.
Thank you again!

Ivans said...

Super example and explanation!
Thank you!
If all VisualForce documentation was written this way, it would be much easier to learn VF.
First simple and clear explanation of this topic.
Thank you!

Abi Dzar said...

You helped solve 1 week problem.

Post a Comment