Tuesday, 15 September 2015

Pagination using List Views with Standard List Controllers - Part # 2

Many Salesforce pages include list views that allow you to filter the records displayed on the page. For example, on the opportunities home page, you can choose to view a list of only the opportunities you own by selecting "My Opportunities" from the list view drop-down.

On a page that is associated with a list controller, you can also use list views.

Standard List Controller Actions:

Action Description
first Displays the first page of records in the set
last Displays the last page of records in the set
next Displays the next page of records in the set
previous Displays the previous page of records in the set


For example, to create a simple list of products (as explained in Tutorial # 1) with a list view, create a page with the following markup:

Visualforce Page

<!--
Code Name   : PaginationTutorial_2
Code Type   : Visualforce
Description : Pagination using List Views with Standard List Controllers
Author      : Bharathimohan Ramamurthy
/-->
<apex:page standardController="Product2" recordSetvar="products">
<apex:form >
<apex:pageBlock title="Pagination using List Views with Standard List Controllers">
    <apex:pageBlockButtons location="top">
        <div style="float:left;font-weight:bold;">
            <apex:outputLabel value="Select View:"/>
            <apex:selectList value="{!filterId}" size="1">
                <apex:actionSupport event="onchange" rerender="myTable"/>
               <apex:selectOptions value="{!listviewoptions}"/>
            </apex:selectList>
        </div>
        <div style="float:center;font-weight:bold;">
            <apex:commandLink action="{!first}">First</apex:commandlink> &nbsp;&nbsp;
            <apex:commandLink action="{!previous}">Previous</apex:commandlink> &nbsp;&nbsp;
            <apex:commandLink action="{!next}">Next</apex:commandlink> &nbsp;&nbsp;
            <apex:commandLink action="{!last}">Last</apex:commandlink> &nbsp;&nbsp;
        </div>
    </apex:pageBlockButtons>
      <apex:pageBlockTable value="{!products}" var="v" id="myTable">
          <apex:column value="{!v.name}"/>
          <apex:column value="{!v.IsActive}"/>
          <apex:column value="{!v.ProductCode}"/>
          <apex:column value="{!v.RecordTypeId}"/>
          <apex:column value="{!v.CreatedbyId}"/>
          <apex:column value="{!v.CreatedBy.ProfileId}"/>      
          <apex:column value="{!v.CreatedDate}"/>
      </apex:pageBlockTable>           
</apex:pageBlock>
</apex:form>
</apex:page>


Preview Screenshot



Points to Note
  • When using a standard list controller, the returned records sort on the first column of data, as defined by the current view, even if that column is not rendered
  • When you use the standardController attribute on the <apex:page> tag, you cannot use the controller attribute at the same time
  • The recordSetVar attribute not only indicates that the page uses a list controller, it indicates the variable name of the record collection. This variable can then be used to access data from the record collection
  • This page does not specify a filter in the request, so the page is displayed is based on the last used filter. To check this behavior, reload your visual force page and go to the Products tab and you can observe that records displayed in first page on both these tables are same
  • Using visualforce expressions, we can access data from the records and embed to the page
  • We can traverse up to five levels of child-to-parent relationships and one level of parent-to-child relationships (Refer to Column "Profile" in screenshot for example)
  • By default, a list controller returns 20 records on the page


Pros
  • No Apex Class required and hence no test class
  • Minimal effort for development
  • No SOQL queries issued to sfdc server
  • Governor limits are not in scope for this page. However the view state limit still applies but will be minimal always as only lesser records are displayed in each page

Cons
  • No more than 10,000 records can be returned by a standard list controller. Custom controllers can work with larger results sets
  • No table sorting on records
  • No Search functionality
  • No customizations to column headers like changing label, css etc.,(Refer to Column "Profile" in screenshot for example, which suppose to be renamed as "Created User Profile" instead for better understanding)
  • You cannot filter records to be displayed inside the table dynamically through code as it was controlled by the list view

No comments:

Post a Comment