Friday, 1 July 2016

Salesforce Dev Utility Post #3 : Pagination in Visualforce Page, Using Visualforce Component

Working on large set of Data or Displaying more than 100 records in a single visualforce page, We need to do the Pagination,

Salesforce has a number of different pagination options that are available to you which include using a Visualforce StandardSetController, Query and QueryMore SOAP API Calls, and Offset clauses within SOQL queries. These options are great but sometimes when you are building a custom site or working with complex data models you are unable to use standard objects and need to use custom wrapper classes for your data.

Below is Generic component to implement pagination. Here you only have to pass the list of recodrs you want to show up using pagination also you have to pass the columns fields you desire to display.

Following is a visualforce component for pagination:
Here you have to pass the list of records(whichever object it may be) you want to display in table and the column fields to display.

Visualforce component

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<apex:component controller="ComponentController">
  <apex:attribute name="listo" description="List of Account" type="Account[]" required="false" assignTo="{!idList}"/>
  <apex:attribute name="listofield" description="Fields to display" type="string[]" required="false" assignTo="{!SobjFieldList}"/>     
     
    <apex:form >
       <apex:pageblock id="pg">
          <apex:pageBlockTable value="{!SObjectRecs}" var="rec">
             <apex:repeat value="{!FieldList}" var="fl">
                <apex:column value="{!rec[fl]}"/>
             </apex:repeat> 
          </apex:pageBlockTable>
       
          <apex:panelGrid columns="7">
             <apex:commandButton status="fetchStatus" reRender="pg" value="First" action="{!setRecords.first}" disabled="{!!setRecords.HasPrevious}" />
             <apex:commandButton status="fetchStatus" reRender="pg" value="Previous" action="{!setRecords.previous}" disabled="{!!setRecords.HasPrevious}" />
             <apex:commandButton status="fetchStatus" reRender="pg" value="Next" action="{!setRecords.next}" disabled="{!!setRecords.HasNext}" />
             <apex:commandButton status="fetchStatus" reRender="pg" value="Last" action="{!setRecords.last}" disabled="{!!setRecords.HasNext}"/>
             <apex:outputPanel style="color:green;">
                  <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
             </apex:outputPanel>
          </apex:panelGrid>
       </apex:pageblock>
    </apex:form>
  
</apex:component>

Controller For Visualforce component

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class ComponentController {

public List<sObject> idList{get;set;}
public List<String> SobjFieldList{get;set;}
public Integer pageSize = 10;  
  Public ApexPages.StandardSetController setRecords{
    get{
     if(setRecords == null){
        setRecords = new ApexPages.StandardSetController(getIdListRecrds());
        setRecords.setPageSize(pageSize);
     }  
      return setRecords;
    }set;
   }
    
   Public List<sObject> getSObjectRecs(){
        List<sObject> sObjList = New List<sObject>();
        for(sObject SObj :(List<sObject>)setRecords.getRecords())
            sObjList.add(SObj); 
        return  sObjList ;   
   }
   
   Public List<String> FieldList{
       get{
       List<String> FieldList = New List<string>();
       FieldList = getSobjtFieldList();
       return FieldList;
       }set;
   }
   
    public List<sObject> getIdListRecrds() {
       List<sObject> IdListRecrds =idList;
       return IdListRecrds;
    }
    
    public List<string> getSobjtFieldList() {
       List<String> FieldList = SobjFieldList;
       return FieldList ;
    }

}

Usage with Visual force Page :

1
2
3
<apex:page controller="PageController">
  <c:PaginationComponent listo="{!accountList}" listofield="{!accountFieldList}"/>
</apex:page>

Controller :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public Class PageController {
Public List<Account> accountList {get;set;}
Public List<String> accountFieldList{get;set;}

    public PageController (){
      accountList = New List<Contact>();
      accountFieldList = New List<string>();
      accountList = [select Name, AccountNumber, AccountSource, Phone, Type from Account];
      accountFieldList.add('Name');
      accountFieldList.add('AccountNumber');
      accountFieldList.add('AccountSource');
      accountFieldList.add('Phone');
      accountFieldList.add('Type');
    }
}

Please connect with me in case of any doubt.

                                         Happy Coding !!      

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete


  2. That mean every time i have to change the 'type' attribute for when i am using this component to new object.
    is it possible to give sobject name from visualforce page not from component.so that it made easy for who are using your component

    ReplyDelete