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:-
<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>
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:
public class oppoNe { public oppoNe(ApexPages.StandardSetController controller) { controller.setPageSize(10); } }
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” /]