Pagination using list in custom controller in salesforce

Use case :

Add pagination to display more than 2000 records in a VisualForce page.

Solution :

Query all records using SOQL and store it in a list. Parse the list and store the specified number of records in a new list and dispaly the new list in the page.

Code :

controller

public class ContactPagination {
    
    //List to store all the contacts according to requirement
    public List AllContacts = new list();
    
    //List to show the limited records on the page
    public List ContactsToShow{get;set;}
    
    //Navigation variables
    Integer counter = 0;//TO track the number of records parsed
    Integer limitSize = 10;//Number of records to be displayed
    Integer totalSize =0; //To Store the total number of records available
    
    //Constructor
    public ContactPagination(){
        
         ContactsToShow = new list();
        
        //Get all the contacts which are from Public realations lead source
        AllContacts = [select id , name, LeadSource from contact where LeadSource='Public Relations'];
        //Store the total size
        totalSize = AllContacts.size();
        
        //Intial adding of contacts to ContactsToShow
        //check the total records are more than limitSize and assign the records
        if((counter+limitSize) <= totalSize){
            for(Integer i=0;i<limitSize;i++){
                ContactsToShow.add(AllContacts.get(i));
            }
        }else{
            for(Integer i=0;i<totalSize;i++){
                ContactsToShow.add(AllContacts.get(i));
            }
        }
        
    }
    
    //Navigation methods
    
    
	public void beginning(){
   
        ContactsToShow.clear();
        counter=0;
        if((counter + limitSize) <= totalSize){
       
            for(Integer i=0;i<limitSize;i++){
                ContactsToShow.add(AllContacts.get(i));
            }   
           
        } else{
       
            for(Integer i=0;i<totalSize;i++){
                ContactsToShow.add(AllContacts.get(i));
            }       
           
        }
       
    }
   
    public void next(){
   
        ContactsToShow.clear();
        counter=counter+limitSize;
       
        if((counter+limitSize) <= totalSize){
            for(Integer i=counter-1;i<(counter+limitSize);i++){
                ContactsToShow.add(AllContacts.get(i));
            }
        } else{
            for(Integer i=counter;i<totalSize;i++){
                ContactsToShow.add(AllContacts.get(i));
            }
        }
    }
   
    public void previous(){
   
        ContactsToShow.clear();

        counter=counter-limitSize;       
       
        for(Integer i=counter;i<(counter+limitSize); i++){
            ContactsToShow.add(AllContacts.get(i));
        }
    }

    public void last (){
   
        ContactsToShow.clear();
       
        if(math.mod(totalSize , limitSize) == 0){
            counter = limitSize * ((totalSize/limitSize)-1);
        } else if (math.mod(totalSize , limitSize) != 0){
            counter = limitSize * ((totalSize/limitSize));
        }
       
        for(Integer i=counter-1;i<totalSize-1;i++){
                ContactsToShow.add(AllContacts.get(i));
        }
       
    }
   
    public Boolean getDisableNext(){
   
        if((counter + limitSize) >= totalSize )
            return true ;
        else
            return false ;
    }
   
    public Boolean getDisablePrevious(){
   
        if(counter == 0)
            return true ;
        else
            return false ;
    } 
}

 

page

<apex:page controller="ContactPagination" tabStyle="Contact">
    <apex:form >        <apex:pageBlock title="Contact Pagination " >                 <apex:pageBlockButtons >              <apex:commandButton value="<<" action="{!beginning}" disabled="{!DisablePrevious}"/>
              <apex:commandButton value="<" action="{!previous}" disabled="{!DisablePrevious}"/>
              <apex:commandButton value=">" action="{!next}" disabled="{!DisableNext}"/>
              <apex:commandButton value=">>" action="{!last}" disabled="{!DisableNext}"/>            </apex:pageBlockButtons>             <apex:pageBlockTable value="{!ContactsToShow}" var="con">
                <apex:column value="{!con.name}"/>                  
                <apex:column headerValue="LeadSource">
                    <apex:outputField value="{!con.LeadSource}"/>
                </apex:column>                              </apex:pageBlockTable>                     </apex:pageBlock>
    </apex:form>
</apex:page>

 

Output: 

If you copy paste this code in your org your output will be like the follwing image.

image

Comparison:

Standard pagination: You can display only the records that is strictly belongs to the object. No custom list cannot be shown. For example : you can display only the account objects no custom objects related to the account cannot be show in pagination.

Pagination using offset: You can display only records upto 2000. OFFSET keywords does not support more than 2000 records to skip in SOQL.

Pagination using list: You can parse more than 2000 records but you can query only the maximum number of records a singe SOQL can retrieve and  put it in a list.

Rating: 
Average: 5 (1 vote)