This post explains the pagination in salesforce using field sets which allows easy customization of column display including
a) what to display
b) what order/sequence
Below markup illustrates this:
Visualforce Page
<apex:page standardController="Product2" recordsetvar="products" extensions="PaginationTutorial_4"> <apex:pageMessages id="pgm"/> <apex:form > <apex:pageBlock > <apex:outputPanel layout="block" styleClass="pSearchShowMore" id="otpNav2"> Total Records Found: <apex:outputText rendered="{!IF(Con.resultSize==10000,true,false)}">10000 +</apex:outputText><apex:outputText rendered="{!IF(Con.resultSize < 10000,true,false)}">{!Con.resultSize}</apex:outputText> <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/> <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/> <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" rendered="{!Con.HasPrevious}"/> <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasPrevious)}">Previous Page</apex:outputPanel> ({!IF(Con.PageNumber == 1,1,((Con.PageNumber -1) * Con.PageSize)+1)}-{!IF(Con.resultSize < Con.PageSize,Con.resultSize,Con.PageNumber * Con.pageSize)}) <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasNext)}">Next Page</apex:outputPanel> <apex:commandLink title="Next Page" value="Next Page" rendered="{!Con.HasNext}" action="{!Next}"/> <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/> <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/> </apex:outputPanel> <apex:pageBlockTable value="{!products}" var="v"> <apex:repeat value="{!$ObjectType.Product2.FieldSets.fieldSetForPagination}" var="f"> <apex:column value="{!v[f.FieldPath]}"/> </apex:repeat> </apex:pageBlockTable> <apex:outputPanel layout="block" styleClass="pSearchShowMore" id="otpNav"> Total Records Found: <apex:outputText rendered="{!IF(Con.resultSize==10000,true,false)}">10000 +</apex:outputText><apex:outputText rendered="{!IF(Con.resultSize < 10000,true,false)}">{!Con.resultSize}</apex:outputText> <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/> <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/> <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" rendered="{!Con.HasPrevious}"/> <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasPrevious)}">Previous Page</apex:outputPanel> ({!IF(Con.PageNumber == 1,1,((Con.PageNumber -1) * Con.PageSize)+1)}-{!IF(Con.resultSize < Con.PageSize,Con.resultSize,Con.PageNumber * Con.pageSize)}) <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasNext)}">Next Page</apex:outputPanel> <apex:commandLink title="Next Page" value="Next Page" rendered="{!Con.HasNext}" action="{!Next}"/> <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/> <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/> </apex:outputPanel> </apex:pageBlock> </apex:form> </apex:page>
Apex Class
public class PaginationTutorial_4 { public ApexPages.StandardSetController con{get; set;} public PaginationTutorial_4(ApexPages.StandardSetController controller) { con = controller; con.setPageSize(10); } //Boolean to check if there are more records after the present displaying records public Boolean hasNext { get { return con.getHasNext(); } set; } //Boolean to check if there are more records before the present displaying records public Boolean hasPrevious { get { return con.getHasPrevious(); } set; } //Page number of the current displaying records public Integer pageNumber { get { return con.getPageNumber(); } set; } //Returns the previous page of records public void previous() { con.previous(); } //Returns the next page of records public void next() { con.next(); } }
Field Set
Preview
Advantages
- Table columns can be customized anytime by adjusting the field set and ends as just a configuration level change
- We can make any display level changes by directly editing visual force page
- Both columns and its sequence of display can be controlled
- Use the dbrequired flag on field set if the pagination is for inline editing
Disadvantages
- Pagination is limited only to the direct object fields. That is, relationship fields cannot be included. For example, we cannot display Last Modified By user email in this table. We can include this field in the pagination, but this visual force page will fail with exception: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: LastModifiedBy.Email.
Hence this pagination is limited only to direct fields for the object in context.
Further blog posts will cover this exception. Stay tuned.