,

Standard Set Controller Basics

Standard set controllers join the best of two worlds—the flexibility of Visualforce pages and the built-in power of views and searches on the Force Platform.

You already know almost everything you need to create a Visualforce page that displays multiple records from a standard controller. The one new aspect for your page is an attribute for the page tag itself. The attribute is recordSetVar, and it takes a string value. This string is the variable name for the set of records that the standard set controller makes available to the page, which will be used as the value of an iterative data component, such as a data table.

1.
Create a new Visualforce page with a quick fix to a new page called JobApplications.

2.
Modify the default code for the page to the following:

<apex:page standardController="Job_Application__c"
       recordSetVar="JobApps" tabstyle="Job_Application__c">
</apex:page>

With the recordSetVar attribute, you have supplied a reference to the set of records that the standard controller supplies for the Job Application object. This new page also includes the tabstyle attribute, which associates the page with the tab and styling for a particular object—in this case, the Job Application object.

Your next step is to add a page block and a page block table.

3.
Change the code for your Visualforce page to the following:

<apex:page standardController="Job_Application__c"
       recordSetVar="JobApps" tabstyle="Job_Application__c">
    <apex:pageblock id="thePage">
        <apex:pageblocktable value="{!JobApps}" var="JA"
          id="candidateTable">
            <apex:column value="{!JA.Name}"/>
            <apex:column value="{!JA.Candidate__c}"/>
            <apex:column
              value="{!JA.Candidate__r.First_Name__c}"/>
            <apex:column
              value="{!JA.Candidate__r.Last_Name__c}"/>
        <apex:column value="{!JA.Position__c}"/>
        <apex:column
          value="{!JA.Position__r.Job_Description__c}"/>
        </apex:pageblocktable>
     </apex:pageblock>
</apex:page>

Nothing new here—these tags define a pageBlockTable the same way you did for your earlier Visualforce page, with an id attribute so you can identify (and refresh) the table a little later in this chapter. But, of course, this page is displaying a set of records from your Job Applications object, not related records to a Position, so the value attribute is set to the var defined for the standard set controller.

Figure 167. Using a standard set controller


4.
Save your code to see your new page in action, as shown in the figure above.

Your new page does look like a list view, having adopted the formatting associated with the Job Application object; however, you have combined fields from the related Candidate and Position objects. This would not have been possible in a list view, as well as including links to the related records in those objects by displaying the values for those lookup fields. This list displays more information and acts as a switchboard that can take you directly to related records.

Already you are ahead of the game, but you still do not want to sacrifice any of the functionality delivered by a list view. One favorite feature of a list view is the built-in pagination. Users are only presented with a page of records at a time, along with buttons or links to navigate through the extended list.

A standard Visualforce dataTable does not behave so nicely. These tables simply display all the records for the associated object. This downside has not affected your pages yet since you have merely been showing small numbers of related records. But with something like job applications across your entire company, you can end up with hundreds.

By default, a standard set controller Visualforce page only shows a page full of records, but the standard controllers that power these pages offer some built-in navigation actions.

5.
Modify your Visualforce page to add the following highlighted code:

<apex:page standardController="Job_Application__c"
  recordSetVar="JobApps" tabstyle="JobApps__tab">
    <apex:form >
        <apex:pageblock id="thePageBlock">
            <apex:pageblocktable value="{!JobApps}" var="JA"
              id="candidateTable">
                <apex:column value="{!JA.Name}"/>
                <apex:column value="{!JA.Candidate__c}"/>
                <apex:column
                  value="{!JA.Candidate__r.First_Name__c}"/>
                <apex:column
                  value="{!JA.Candidate__r.Last_Name__c}"/>
                <apex:column value="{!JA.Position__c}"/>
                <apex:column
                value="{!JA.Position__r.Job_Description__c}">
                 </column>
           </apex:pageblocktable>
       </apex:pageblock>
       <apex:panelGrid columns="2">
								<apex:commandLink
								action="{!previous}">
								Previous</apex:commandLink>
								<apex:commandLink
								action="{!next}">
								Next</apex:commandLink>
								</apex:panelGrid>
								</apex:form>
</apex:page>

You have to add <apex:form> tags to your page, since commandLinks can only be used within a form. The two commandLinks call two new functions offered by standard set controllers, previous and next.

Note

Standard set controllers also support first and last actions.

The new version of your page is ready to roll.

6.
Save your Visualforce page to change the page displayed to the one shown below.



Figure 168. Standard set-based Visualforce page with navigation


The set-based Visualforce page is working as you hoped, but you could make the navigation portion of the page a little more friendly. For instance, when you are on the first page of records, there is no need to have the Next link visible.

You can address this situation with another attribute of most Visualforce components—rendered. As the name implies, this attribute takes a Boolean value that will determine if the component is shown on the page. Standard set controllers includes values that you can use for exactly this purpose—the hasNext and hasPrevious values.

Add these attributes to the command links you previously defined. In addition, you give the outputPanel that contains these links an id attribute of "navigation", since you will want to rerender these links when the page changes. The code in the next section accomplishes this.

7.
For now, add the highlighted code to your Visualforce page:

<apex:page standardController="Job_Application__c"
  recordSetVar="JobApps" tabstyle="JobApps__tab">
    <apex:form >
        <apex:pageblock id="thePageBlock">
            <apex:pageblocktable value="{!JobApps}" var="JA"
              id="candidateTable">
                <apex:column value="{!JA.Name}"/>
                <apex:column value="{!JA.Candidate__c}"/>
                <apex:column
                  value="{!JA.Candidate__r.First_Name__c}"/>
                <apex:column
                  value="{!JA.Candidate__r.Last_Name__c}"/>
                <apex:column value="{!JA.Position__c}"/>
                <apex:column
                value="{!JA.Position__r.Job_Description__c}">
                 </column>
           </apex:pageblocktable>
       </apex:pageblock>
       <apex:panelGrid columns="2">
           <apex:commandLink
             action="{!previous}"
             rendered="{!hasPrevious}">
             Previous</apex:commandLink>
           <apex:commandLink
             action="{!next}"
             rendered="{!hasNext}">
             Next</apex:commandLink>
       </apex:panelGrid>
   </apex:form>
</apex:page>

Your new page can display many records, nicely displayed a page at a time. But how many records are displayed on a page? Right now, your Visualforce page is using the default number of records, as defined for your org. But you can change this quite easily.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.79.26