Pagination is the process of taking a complete document or a large number of records in this example and breaking that document/large number of records into separate pages for viewing within the Salesforce. For example, let’s say we want to display a list of records that only shows 10 records within each page rather than a huge set within Salesforce. The Salesforce user can click the next tab and go through each new page display the next 10 records.

Here is the controller which makes use of standard set controller for pagination.

public with sharing class Pagination

{

Public Integer noOfRecords{get; set;}

Public Integer size{get;set;}

public ApexPages.StandardSetController setCon {

get{

if(setCon == null){

size = 10;

string queryString = ‘Select Name, Type, BillingCity, BillingState, BillingCountry from Account order by Name’;

setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));

setCon.setPageSize(size);

noOfRecords = setCon.getResultSize();

}

return setCon;

}set;

}

 

Public List<Account> getAccounts(){

List<Account> accList = new List<Account>();

for(Account a : (List<Account>)setCon.getRecords())

accList.add(a);

return accList;

}

 

public pageReference refresh() {

setCon = null;

getAccounts();

setCon.setPageNumber(1);

return null;

}

 

public Boolean hasNext {

get {

return setCon.getHasNext();

}

set;

}

public Boolean hasPrevious {

get {

return setCon.getHasPrevious();

}

set;

}

 

public Integer pageNumber {

get {

return setCon.getPageNumber();

}

set;

}

 

public void first() {

setCon.first();

}

 

public void last() {

setCon.last();

}

 

public void previous() {

setCon.previous();

}

 

public void next() {

setCon.next();

}

}

 

Using the above controller methods we can define the pagination.

apex:page controller=”Pagination”>

<apex:form >

<apex:pageBlock id=”pb”>

<apex:pageBlockTable value=”{!Accounts}” var=”a”>

<apex:column value=”{!a.Name}”/>

<apex:column value=”{!a.Type}”/>

<apex:column value=”{!a.BillingCity}”/>

<apex:column value=”{!a.BillingState}”/>

<apex:column value=”{!a.BillingCountry}”/>

</apex:pageBlockTable>

<apex:panelGrid columns=”7″>

<apex:commandButton status=”fetchStatus” reRender=”pb” value=”|<” action=”{!first}” disabled=”{!!hasPrevious}” title=”First Page”/>

<apex:commandButton status=”fetchStatus” reRender=”pb” value=”<” action=”{!previous}” disabled=”{!!hasPrevious}” title=”Previous Page”/>

<apex:commandButton status=”fetchStatus” reRender=”pb” value=”>” action=”{!next}” disabled=”{!!hasNext}” title=”Next Page”/>

<apex:commandButton status=”fetchStatus” reRender=”pb” value=”>|” action=”{!last}” disabled=”{!!hasNext}” title=”Last Page”/>

<apex:outputText >{!(pageNumber * size)+1-size}-{!IF((pageNumber * size)>noOfRecords, noOfRecords,(pageNumber * size))} of {!noOfRecords}</apex:outputText>

<apex:commandButton status=”fetchStatus” reRender=”pb” value=”Refresh” action=”{!refresh}” title=”Refresh Page”/>

<apex:outputPanel style=”color:#4AA02C;font-weight:bold”>

<apex:actionStatus id=”fetchStatus” startText=”Fetching…” stopText=””/>

</apex:outputPanel>

</apex:panelGrid>

</apex:pageBlock>

</apex:form>

</apex:page>

 

Enjoy! If you have any questions, Talk to our team at Tecnovators, a one-of-a-kind Salesforce consulting partner delivering pay-as-you-use On demand services for Salesforce.

Also, you can give us a call at 313-209-7137  or email us at info@tecnovators.com