Creating Generic Pagination with Visualforce Component
GenericPaginationComponent
GenericPaginationComponentContrl
User can see this component in Visualforce Component Reference, see the below screenshot -
*************
Using the above Generic Pagination from a Visualforce Page
GenericPaginationUsagePage
GenericPaginationUsageContrl
Result
Live Demo
GenericPaginationComponent
- <!--
- Description
- *===========
- * VF Component for the Generic Pagination.
- * It accepts three parameters -
- 1. Records (List of records belongs to any object)
- 2. Fields (List of fields of a sobject to display in the table)
- 3. Title (Title of the section)
- * Author:
- * =======
- * www.srinusfdc.com
- -->
- <apex:component controller="GenericPaginationComponentContrl">
- <!-- Attributes to accept the parameters -->
- <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"/>
- <apex:attribute name="Fields" Type="String[]" required="true" description="Accepts list of field API names of a sobject in string format"/>
- <apex:attribute name="Title" Type="String" required="true" description="Accepts the title of the section"/>
- <!-- Table which displays records along with pagination -->
- <apex:form >
- <apex:pageBlock >
- <apex:pageBlockSection columns="1" title="{!Title}" id="pbSec">
- <apex:pageBlockTable value="{!SobjRecords}" var="sObj">
- <!-- Dispalys the multiple columns based on the user input -->
- <apex:repeat value="{!Fields}" var="fld">
- <apex:column value="{!sObj[fld]}"/>
- </apex:repeat>
- </apex:pageBlockTable>
- <!-- Dispalys pagination buttons -->
- <apex:panelGrid columns="5">
- <apex:commandButton value="First" action="{!con.first}" disabled="{!!con.hasPrevious}" status="pagStatus" reRender="pbSec"/>
- <apex:commandButton value="Previous" action="{!con.previous}" disabled="{!!con.hasPrevious}" status="pagStatus" reRender="pbSec"/>
- <apex:commandButton value="Next" action="{!con.next}" disabled="{!!con.hasNext}" status="pagStatus" reRender="pbSec"/>
- <apex:commandButton value="Last" action="{!con.last}" disabled="{!!con.hasNext}" status="pagStatus" reRender="pbSec"/>
- <apex:actionStatus startText="Fetching..." id="pagStatus"/>
- </apex:panelGrid>
- </apex:pageBlockSection>
- </apex:pageBlock>
- </apex:form>
- </apex:component>
GenericPaginationComponentContrl
- /* Description:
- * ============
- * Class for the VF Component GenericPaginationComponent *
- * Author:
- * ========
- * www.srinusfdc.com
- */
- public class GenericPaginationComponentContrl {
- //Stores the records which are supplied to the 'Records' attribute.
- public Sobject[] sObjLst {get;set;}
- /*
- 1. Implementing the pagination with ApexPages.StandardSetController.
- 2. We can utilize the built in methods available for the ApexPages.StandardSetController to build the pagination.
- 3. Following are the built in mehods we can utilize -
- a. first()
- b. previous()
- c. next()
- d. last()
- e. getHasPrevious() - returns boolean value.
- f. getHasNext() - returns boolean value.
- g. setPageSize(IntegerValue)
- */
- public ApexPages.StandardSetController con {
- get {
- //initializing con with the records.
- if(con == null)
- con = new ApexPages.StandardSetController(sObjLst);
- //Setting the pagination size
- con.setPageSize(5);
- return con;
- }
- set;
- }
- //Method which returns subset of records from the sObjLst.
- public List<sobject> getSobjRecords() {
- //Type Casing the records and returning to display on the page.
- return (List<sobject>)con.getRecords();
- }
- }
User can see this component in Visualforce Component Reference, see the below screenshot -
*************
Using the above Generic Pagination from a Visualforce Page
GenericPaginationUsagePage
- <!--
- Description
- *===========
- * VF Page for using 'GenericPaginationComponent' component.
- * Displaying three different object lists by reusing the logic of 'GenericPaginationComponent' component.
- * Author:
- * =======
- * www.srinusfdc.com
- -->
- <apex:page controller="GenericPaginationUsageContrl" tabStyle="Account">
- <!-- Default name space is : C ; Referring the Visualforce Component -->
- <c:GenericPaginationComponent records="{!accLst}" fields="{!accFieldLst}" title="Accounts"/>
- <c:GenericPaginationComponent records="{!conLst}" fields="{!conFieldLst}" title="Contacts"/>
- <c:GenericPaginationComponent records="{!oppLst}" fields="{!oppFieldLst}" title="Opportunities"/>
- </apex:page>
GenericPaginationUsageContrl
- /* Description:
- * ============
- * Class for the VF Page GenericPaginationUsagePage *
- * Author:
- * ========
- * www.srinusfdc.com
- */
- public class GenericPaginationUsageContrl {
- //Declaring variables to store list of records.
- public List<Account> accLst {get;set;}
- public List<Contact> conLst {get;set;}
- public List<Opportunity> oppLst {get;set;}
- //Declaring variables to store list of Field API names in string format.
- public List<String> accFieldLst {get;set;}
- public List<String> conFieldLst {get;set;}
- public List<String> oppFieldLst {get;set;}
- //Default Constructor
- public GenericPaginationUsageContrl() {
- //Querying the records from the database.
- accLst = [select Name, AccountNumber, Fax, Phone, Industry from Account limit 100];
- conLst = [select Name, AccountId, Email, Phone from Contact limit 100];
- oppLst = [select Name, AccountId, Amount from Opportunity limit 100];
- //Preparing the list of fields to display in the table.
- accFieldLst = new List<String>{'Name', 'AccountNumber', 'Fax', 'Phone', 'Industry'};
- conFieldLst = new List<String>{'Name', 'AccountId', 'Email', 'Phone'};
- oppFieldLst = new List<String>{'Name', 'AccountId', 'Amount'};
- }
- }
Result
Live Demo
Having an issue rerendering this component when the sobject list gets changed. Do you have any suggestions.
ReplyDeleteIs 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