Friday, 30 January 2015

Generic Pagination with Visualforce Component

Creating Generic Pagination with Visualforce Component

GenericPaginationComponent
  1. <!--
  2. Description
  3. *===========
  4. * VF Component for the Generic Pagination.
  5. * It accepts three parameters -
  6. 1. Records (List of records belongs to any object)
  7. 2. Fields (List of fields of a sobject to display in the table)
  8. 3. Title (Title of the section)
  9. * Author:
  10. * =======
  11. * www.srinusfdc.com
  12. -->
  13. <apex:component controller="GenericPaginationComponentContrl">
  14. <!-- Attributes to accept the parameters -->
  15. <apex:attribute name="Records" Type="Sobject[]" assignTo="{!sObjLst}" required="true" description="Accepts list of records of any object and assign to a variable in controller class"/>
  16. <apex:attribute name="Fields" Type="String[]" required="true" description="Accepts list of field API names of a sobject in string format"/>
  17. <apex:attribute name="Title" Type="String" required="true" description="Accepts the title of the section"/>
  18. <!-- Table which displays records along with pagination -->
  19. <apex:form >
  20. <apex:pageBlock >
  21. <apex:pageBlockSection columns="1" title="{!Title}" id="pbSec">
  22. <apex:pageBlockTable value="{!SobjRecords}" var="sObj">
  23. <!-- Dispalys the multiple columns based on the user input -->
  24. <apex:repeat value="{!Fields}" var="fld">
  25. <apex:column value="{!sObj[fld]}"/>
  26. </apex:repeat>
  27. </apex:pageBlockTable>
  28. <!-- Dispalys pagination buttons -->
  29. <apex:panelGrid columns="5">
  30. <apex:commandButton value="First" action="{!con.first}" disabled="{!!con.hasPrevious}" status="pagStatus" reRender="pbSec"/>
  31. <apex:commandButton value="Previous" action="{!con.previous}" disabled="{!!con.hasPrevious}" status="pagStatus" reRender="pbSec"/>
  32. <apex:commandButton value="Next" action="{!con.next}" disabled="{!!con.hasNext}" status="pagStatus" reRender="pbSec"/>
  33. <apex:commandButton value="Last" action="{!con.last}" disabled="{!!con.hasNext}" status="pagStatus" reRender="pbSec"/>
  34. <apex:actionStatus startText="Fetching..." id="pagStatus"/>
  35. </apex:panelGrid>
  36. </apex:pageBlockSection>
  37. </apex:pageBlock>
  38. </apex:form>
  39. </apex:component>

GenericPaginationComponentContrl
  1. /* Description:
  2. * ============
  3. * Class for the VF Component GenericPaginationComponent *
  4. * Author:
  5. * ========
  6. * www.srinusfdc.com
  7. */
  8. public class GenericPaginationComponentContrl {
  9. //Stores the records which are supplied to the 'Records' attribute.
  10. public Sobject[] sObjLst {get;set;}
  11. /*
  12. 1. Implementing the pagination with ApexPages.StandardSetController.
  13. 2. We can utilize the built in methods available for the ApexPages.StandardSetController to build the pagination.
  14. 3. Following are the built in mehods we can utilize -
  15. a. first()
  16. b. previous()
  17. c. next()
  18. d. last()
  19. e. getHasPrevious() - returns boolean value.
  20. f. getHasNext() - returns boolean value.
  21. g. setPageSize(IntegerValue)
  22. */
  23. public ApexPages.StandardSetController con {
  24. get {
  25. //initializing con with the records.
  26. if(con == null)
  27. con = new ApexPages.StandardSetController(sObjLst);
  28. //Setting the pagination size
  29. con.setPageSize(5);
  30. return con;
  31. }
  32. set;
  33. }
  34. //Method which returns subset of records from the sObjLst.
  35. public List<sobject> getSobjRecords() {
  36. //Type Casing the records and returning to display on the page.
  37. return (List<sobject>)con.getRecords();
  38. }
  39. }

User can see this component in Visualforce Component Reference, see the below screenshot -






*************
Using the above Generic Pagination from a Visualforce Page

GenericPaginationUsagePage
  1. <!--
  2. Description
  3. *===========
  4. * VF Page for using 'GenericPaginationComponent' component.
  5. * Displaying three different object lists by reusing the logic of 'GenericPaginationComponent' component.
  6. * Author:
  7. * =======
  8. * www.srinusfdc.com
  9. -->
  10. <apex:page controller="GenericPaginationUsageContrl" tabStyle="Account">
  11. <!-- Default name space is : C ; Referring the Visualforce Component -->
  12. <c:GenericPaginationComponent records="{!accLst}" fields="{!accFieldLst}" title="Accounts"/>
  13. <c:GenericPaginationComponent records="{!conLst}" fields="{!conFieldLst}" title="Contacts"/>
  14. <c:GenericPaginationComponent records="{!oppLst}" fields="{!oppFieldLst}" title="Opportunities"/>
  15. </apex:page>


GenericPaginationUsageContrl
  1. /* Description:
  2. * ============
  3. * Class for the VF Page GenericPaginationUsagePage *
  4. * Author:
  5. * ========
  6. * www.srinusfdc.com
  7. */
  8. public class GenericPaginationUsageContrl {
  9. //Declaring variables to store list of records.
  10. public List<Account> accLst {get;set;}
  11. public List<Contact> conLst {get;set;}
  12. public List<Opportunity> oppLst {get;set;}
  13. //Declaring variables to store list of Field API names in string format.
  14. public List<String> accFieldLst {get;set;}
  15. public List<String> conFieldLst {get;set;}
  16. public List<String> oppFieldLst {get;set;}
  17. //Default Constructor
  18. public GenericPaginationUsageContrl() {
  19. //Querying the records from the database.
  20. accLst = [select Name, AccountNumber, Fax, Phone, Industry from Account limit 100];
  21. conLst = [select Name, AccountId, Email, Phone from Contact limit 100];
  22. oppLst = [select Name, AccountId, Amount from Opportunity limit 100];
  23. //Preparing the list of fields to display in the table.
  24. accFieldLst = new List<String>{'Name', 'AccountNumber', 'Fax', 'Phone', 'Industry'};
  25. conFieldLst = new List<String>{'Name', 'AccountId', 'Email', 'Phone'};
  26. oppFieldLst = new List<String>{'Name', 'AccountId', 'Amount'};
  27. }
  28. }

Result

Live Demo

3 comments:

  1. Having an issue rerendering this component when the sobject list gets changed. Do you have any suggestions.

    ReplyDelete
  2. Is there a way to customize the column headers so that they are not the labels of the fields? For instance, I want a column to be 'Customer Name' not 'Account Name' when field I retrieved was Account.Name

    ReplyDelete


  3. Hai Author, Very Good informative blog post,
    Thanks for Sharing

    ReplyDelete