How to implement pagination in Visualforce with Example?

This is one of the most common interview question which employers love to ask. In this blog post, we will see how to implement pagination in Visualforce. I hope you will enjoy this blog post. Please share your feedback in the comment section.

What is Pagination in Visualforce?
Pagination is the process of displaying large number of records and displaying the records on multiple pages within in Salesforce.

In order to control the number of records displayed on each page, we use pagination. By default, a list controller returns 20 records on the page.

To customize it, we can use a controller extension to set the pageSize. Take a look at the sample code below:-

[sourcecode lang=”Java”]

<apex:page standardController="Opportunity" extensions="oppoNe" recordSetVar="opportunities">
<apex:pageBlock title="Viewing Opportunities">
<apex:form id="theForm">
<apex:pageBlockSection >
<apex:dataList var="opp" value="{!opportunities}">
{!opp.Name}
</apex:dataList>
</apex:pageBlockSection>
<apex:panelGrid columns="4">
<apex:commandLink action="{!first}">FIRST</apex:commandLink>
<apex:commandLink action="{!next}">NEXT</apex:commandLink>
<apex:commandLink action="{!previous}">PREVIOUS</apex:commandLink>
<apex:commandLink action="{!last}">LAST</apex:commandLink>
</apex:panelGrid>

</apex:form>
</apex:pageBlock>
</apex:page>
[/sourcecode]

By default it will show 20 records on a page, if you want to change the number of records to display on each page, you can use pageSize method as show below:

[sourcecode lang=”Java”]
public class oppoNe {

public oppoNe(ApexPages.StandardSetController controller) {
controller.setPageSize(10);
}

}
[/sourcecode]

In the above example,I have used the setPageSize() to set the number of records to be displayed on every page.

Here is also the link for the video related to Pagination:
[s3video s3url=”/SalesforceInterviewCourse/tutorial35PaginationWithExample.mp4″ responsive=”on” /]

This Post Has 6 Comments

  1. Frederick

    HI deepika.
    No link visible for pagination video??

    1. MyTutorialRack

      There is a video on the page here.Were you able to see the video?

  2. Ehsna

    Hi deepika,

    when i clicked on links like first,last etc it is throwing an error that..formula expression is needed..can you look into it..
    thanks..

  3. Ehsan

    Hi deepika,

    I got it..sorry I didn’t add formula expression in the links..

  4. C

    Good one

Leave a Reply