Pagination Using Salesforce Standard Set Controller

We can utilize standardSetController provided by salesforce to actualize pagination in visualforce. It is very easy to implement pagination using the standard set controller. We can navigate easily without much of a stretch explore through pages, move straightforwardly to first, the last page or to next or past pages utilizing the standard set controller. We can likewise choose what number of records ought to be shown on each page. We need to initialize the StandardSetController and utilize the standard methods provided by salesforce to use the pagination functionality.

Visualforce Page:

<apex:page controller="OpportunitiesPaginationController" tabStyle="Opportunity">
    <apex:form >
        <apex:actionFunction name="refreshPageSize" action="{!refreshPageSize}" status="checkStatus" reRender="pageblockId"/>
        <apex:pageBlock id="pageblockId">
            <apex:pageBlockSection title="All Opportunities" collapsible="false" columns="1">
                <apex:pageBlockTable value="{!Opportunities}" var="oppRecord">
                    
                    <apex:column headerValue="Opportunity Name">
                        <apex:outputField value="{!oppRecord.Name}"/>
                    </apex:column>
                    
                    <apex:column headerValue="Account Name">
                        <apex:outputField value="{!oppRecord.Account.name}"/>
                    </apex:column>
                    
                    <apex:column headerValue="Amount">
                        <apex:outputField value="{!oppRecord.Amount}"/>
                    </apex:column>
                    
                    <apex:column headerValue="Stage">
                        <apex:outputField value="{!oppRecord.StageName}"/>
                    </apex:column>
                    
                    <apex:column headerValue="Last Activity Date">
                        <apex:outputField value="{!oppRecord.LastModifiedDate}"/>
                    </apex:column>
                    
                    <apex:column headerValue="Close Date">
                        <apex:outputField value="{!oppRecord.CloseDate}"/>
                    </apex:column>
                </apex:pageBlockTable>
                
                <apex:panelGrid columns="8">
                    
                    <apex:selectList value="{!size}" size="1" onchange="refreshPageSize();">
                        <apex:selectOptions value="{!paginationSizeOptions}"/>
                    </apex:selectList>
                    
                    <apex:commandButton status="checkStatus" reRender="pageblockId" value="First" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>
                    
                    <apex:commandButton status="checkStatus" reRender="pageblockId" value="Previous" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
                    
                    <apex:commandButton status="checkStatus" reRender="pageblockId" value="Next" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
                    
                    <apex:commandButton status="checkStatus" reRender="pageblockId" value="Last" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>
                    
                    
                    <apex:outputPanel >
                        <apex:actionStatus id="checkStatus" >
                            <apex:facet name="start" >
                                <img src="/img/loading.gif" />
                            </apex:facet>
                        </apex:actionStatus>
                    </apex:outputPanel>
                    
                </apex:panelGrid>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Code:

public class OpportunitiesPaginationController{
    Public Integer size{get;set;}
    Public Integer noOfRecords{get; set;}
    public List<SelectOption> paginationSizeOptions{get;set;}
    
    public OpportunitiesPaginationController(){
        size=10; // set the default pagination size to 10 
        paginationSizeOptions = new List<SelectOption>();
        paginationSizeOptions.add(new SelectOption('5','5'));
        paginationSizeOptions.add(new SelectOption('10','10'));
        paginationSizeOptions.add(new SelectOption('20','20'));
        paginationSizeOptions.add(new SelectOption('50','50'));
        paginationSizeOptions.add(new SelectOption('100','100'));
    }
    
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [select id,Name,AccountId,Account.name,Amount,StageName,CloseDate,LastModifiedDate from Opportunity]));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
        }
        set;
    }
    
    //Changes the size of pagination
    public PageReference refreshPageSize() {
        setCon.setPageSize(size);
        return null;
    }
    
    // Initialize setCon and return a list of record
    
    public List<Opportunity> getOpportunities() {
        return (List<Opportunity>) setCon.getRecords();
    }
}

Output: