Thursday, September 7, 2017

Generic Pagination with Visualforce Component

Apex Class for Component : GenericPaginationComponentContrl


public class GenericPaginationComponentContrl {    
    //Stores the records which are supplied to the 'Records' attribute.
    public Sobject[] sObjLst {get;set;}    
    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();
    }
}


Visualforce Component : GenericPaginationComponent

<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>


Visualforce Page: 

<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>

Apex Class For Page : GenericPaginationUsageContrl

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'};
                    }
    
}



Screen Shot Of the output:


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.