function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rohit SharmaGRohit SharmaG 

Pagination with Standard controller and Extension

Hello Dears,

Please let me know how to implement Pagination with Standard Controller and Extension used in Apex:Page.
Kindly assist on this.


Thanks,
Rohit Sharma
AshwaniAshwani
Standard Controller provide pagination by default look at following example:

<apex:page standardController="Account" recordSetVar="Accounts" sidebar="false" >

<apex:sectionHeader title="My Accounts" subtitle="Account Management"/>
    <apex:form >
        <apex:pageBlock >
          <apex:pageMessages id="error" />
            <apex:panelGrid columns="7" id="buttons">
                <apex:commandButton reRender="error,blocktable,buttons" action="{!Save}" value="Save"/>
                <apex:commandButton reRender="error,blocktable,buttons" action="{!Cancel}" value="Cancel"/>
                <apex:inputHidden />
                <apex:commandButton reRender="error,blocktable,buttons" disabled="{!!hasprevious}" action="{!First}" value="First"/>
                <apex:commandButton reRender="error,blocktable,buttons" disabled="{!!hasprevious}" action="{!Previous}" value="Previous"/>
                <apex:commandButton reRender="error,blocktable,buttons" disabled="{!!hasnext}" action="{!Next}" value="Next"/>
                <apex:commandButton reRender="error,blocktable,buttons" disabled="{!!hasnext}" action="{!Last}" value="Last"/>
            </apex:panelGrid>
            <apex:pageBlockSection id="blocktable" >
                <apex:pageBlockTable value="{!Accounts}" var="t">
                    <apex:column headerValue="Account" value="{!t.Name}" />
                   
                    <apex:column headerValue="Priority">
                        <apex:outputField value="{!t.Priority__c}" />
                    </apex:column>
                   

                   
                    <apex:inlineEditSupport event="onClick"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>   
           
           
        </apex:pageBlock>
    </apex:form>
</apex:page>


Grazitti TeamGrazitti Team
Hi Rohit,

There are several tutorials that expalins how can we do pagination by standard controller.

Please find below the code that paginate using extension.

Apex Controller:-
public with sharing class PaginationExtension {
    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;
    }
}

VF page:

<apex:page Controller="PaginationExtension">
    <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="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>
                <apex:outputText >{!(setCon.pageNumber * size)+1-size}-{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.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>


Please find here some url where you can explore yourself and customize accordingly.
1. http://hisrinu.wordpress.com/2012/01/09/pagination-using-standardsetcontroller/
2. http://blog.ryansieve.com/2012/06/06/pagination-made-easy-with-standard-set-controllers/
3. https://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_sosc_pagination.htm
4. https://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm

Please feel free to contact in case of any other issue.

Thanks,
Regards,
Grazitti Team
Web: www.grazitti.com
raj kiranraj kiran
Hi Grazitti Team,
Is there any way to give the page size dynamically from vf ? like passing the select list values from vf to apex  variable size = 10 mentioned above?

Regards,
Raj
 
Krrish GopalKrrish Gopal
Hi Grazitti Team,

I want to use pagination for search result at my visualforce page, I want to use your PaginationExtension apex controller code in my existing apex controller displaycase, pl suggest required change in my apex controller code, my apex controller and visualforce page code is below
Apex controller displaycase

public class displaycase {

public string getcasestatus{get;set;}
public string getcasenumber{get;set;}
public string getcasetype{get;set;}
public string getcasereason{get;set;}
public string getcasepriority{get;set;}

public  void displaycase ()
{
    getcasestatus='';
    getcasetype= '';
    getcasereason= '';
    getcasepriority= '';
    
}
public list<case> caselst{get;set;}


public list<selectoption>getcstatus()
{
    list<selectoption>selectopts=new list<selectoption>();
    selectopts.add(new selectoption('',''));
    selectopts.add(new selectoption('Open','Open'));
    selectopts.add(new selectoption('Closed','Closed'));
    selectopts.add(new selectoption('Rejected','Rejected'));
    return selectopts;
}

public list<selectoption>getctype()
{
    list<selectoption>selectoptt=new list<selectoption>();
    selectoptt.add(new selectoption('',''));    
    selectoptt.add(new selectoption('Out of Warranty','Out of Warranty'));
    selectoptt.add(new selectoption('Under AMC','Under AMC'));
    selectoptt.add(new selectoption('Under Warranty','Under Warranty'));
    return selectoptt;
}

public list<selectoption>getcreason()
{
    list<selectoption>selectoptr=new list<selectoption>();
    selectoptr.add(new selectoption('',''));    
    selectoptr.add(new selectoption('Acceptance Testing','Acceptance Testing'));
    selectoptr.add(new selectoption('Breakdown','Breakdown'));
    selectoptr.add(new selectoption('Equipment Complexity','Equipment Complexity'));
    selectoptr.add(new selectoption('Equipment Design','Equipment Design'));
    selectoptr.add(new selectoption('Exceptional','Exceptional'));
    selectoptr.add(new selectoption('Feedback','Feedback'));
    selectoptr.add(new selectoption('Installation','Installation'));
    selectoptr.add(new selectoption('Job Warranty','Job Warranty'));
    selectoptr.add(new selectoption('Other','Other'));
    selectoptr.add(new selectoption('Paid Repair','Paid Repair'));
    selectoptr.add(new selectoption('Performance','Performance'));
    selectoptr.add(new selectoption('Preventive Maintenance','Preventive Maintenance'));    
    return selectoptr;
}

public list<selectoption>getcpriority()
{
    list<selectoption>selectoptp=new list<selectoption>();
    selectoptp.add(new selectoption('',''));
    selectoptp.add(new selectoption('Major','Major'));
    selectoptp.add(new selectoption('Minor','Minor'));
    selectoptp.add(new selectoption('Critical','Critical'));
    return selectoptp;
}

public void displaycaselist()
{
    caselst=new list<case>();

    String query = 'select CaseNumber,CreatedDate,ClosedDate,IsClosed,IsEscalated,Priority,Reason,Status,Subject,Type FROM Case where id !=NULL ';

    if(getcasestatus!=null){
       query+=' and Status=\''+getcasestatus+'\'';
    }
    if(getcasetype!=null){
       query+=' and Type=\''+getcasetype+'\'';
    }
    if(getcasereason!=null){
       query+=' and Reason=\''+getcasereason+'\'';
    }
    if(getcasepriority!=null){
       query+=' and priority=\''+getcasepriority+'\'';
    }

    caselst=Database.query(query+=' ORDER BY CreatedDate DESC');
    
}

public void searchcaselist()
{   
    caselst=new list<case>();
    
    if(getcasenumber!=null){
       caselst=Database.query('select CaseNumber,id,ClosedDate,IsClosed,IsEscalated,Priority,Reason,Status,Subject,Type FROM Case where CaseNumber like \'%'+getcasenumber+'%\' ORDER BY CreatedDate DESC');
    }    
}
}
​VF Page
<apex:page controller="displaycase" title="Exicom Engineer Portal">
 <apex:form >
 <apex:pageBlock title="Search for cases based on criteria">
 <table width="100%">
  <tr>
     <td width="25%"><strong>Status  </strong>
        <apex:selectList size="1" value="{!getcasestatus}">
        <apex:selectOptions value="{!cstatus}"> </apex:selectOptions>
        </apex:selectList>
     </td>
     <td width="25%"><strong>Type  </strong>
        <apex:selectList size="1" value="{!getcasetype}">
        <apex:selectOptions value="{!ctype}"> </apex:selectOptions>
        </apex:selectList>
     </td>
     <td width="25%"><strong>Reason  </strong>
        <apex:selectList size="1" value="{!getcasereason}">
        <apex:selectOptions value="{!creason}"> </apex:selectOptions>
        </apex:selectList>       
     </td>
     <td width="15%"><strong>Priority  </strong>
        <apex:selectList size="1" value="{!getcasepriority}">
        <apex:selectOptions value="{!cpriority}"> </apex:selectOptions>
        </apex:selectList>      
     </td>
     <td width="10%">
        <apex:commandButton value="Search" action="{!displaycaselist}"/>       
     </td>
  </tr>
 </table>
 </apex:pageBlock>
 <apex:pageBlock title="Search a particular case">
 <apex:pageBlockSection columns="2">
 <apex:inputText value="{!getcasenumber}" label="Case Number"/>
 <apex:commandButton value="Search" action="{!searchcaselist}"/>
 </apex:pageBlockSection>
 </apex:pageBlock>
 <apex:pageBlock title="Search Result">
 <apex:pageBlockTable value="{!caselst}" var="cse">
 <apex:column value="{!cse.CaseNumber}"/>
 <apex:column value="{!cse.Subject}"/>
 <apex:column value="{!cse.Type}"/>
 <apex:column value="{!cse.Priority}"/>
 <apex:column value="{!cse.Reason}"/>
 <apex:column value="{!cse.Status}"/>
 <apex:column value="{!cse.IsEscalated}"/>
 <apex:column value="{!cse.IsClosed}"/>
 <apex:column value="{!cse.ClosedDate}"/>
 </apex:pageBlockTable>
 </apex:pageBlock>
 </apex:form>
</apex:page>

Snap shoot of VF Page
Saket Ranjan 3Saket Ranjan 3
Thanks @Grazitti