It is very easy to implement pagination using standard set controller provided by salesforce in visualforce page. You can decide how many records should be displayed in every page also you can easily navigate through pages, move directly to first, last page or to next or previous pages using standard set controller.
Pagination is very useful when you want to display 1000+ records on visual force page. Without pagination if you try to display 1000+ records on visualforce page then you face visualforce error "Collection size xxxx exceeds maximum size of 1000"
Below example displaying all opportunities in visualforce page with pagination. Default page size 10.
Step 1: Create apex class
Step 2: Create Visualforce Page
Step 3: Click on Preview button of visualforce page
Or
You can Go to the URL and type (Change the yoursalesforceinstance with your salesforce org URL)
https://yoursalesforceinstance.com/apex/OpportunitiesPagination.
When you use pagination, an exception is thrown when there are modified rows in the collection. This includes any new rows added to the collection through an extension action. The handling of error messages in this case follows the standard behavior and can either be displayed upon the page. For example, you can use the <apex:pageMessages> or <apex:messages> component to display an error message to the user.
Pros and Cons of Choosing This Pagination Tool
Pros:
There is a good article about pagination in Apex here: https://developer.salesforce.com/page/Paginating_Data_for_Force.com_Applications that lists various options.
Enjoy! If you have any questions, comments, please feel free to let me know.
As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.
Next post: How To Learn Get Field Values From Visualforce Page To Apex Class Controller Without Losing Your Mind
Pagination is very useful when you want to display 1000+ records on visual force page. Without pagination if you try to display 1000+ records on visualforce page then you face visualforce error "Collection size xxxx exceeds maximum size of 1000"
Below example displaying all opportunities in visualforce page with pagination. Default page size 10.
Step 1: Create apex class
public class OpportunitiesPaginationController{
Public Integer size{get;set;}
Public Integer noOfRecords{get; set;}
public List<SelectOption> paginationSizeOptions{get;set;}
public OpportunitiesPaginationController(){
size=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 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 variable setCon from below method and return a list of opportunity record
public List<Opportunity> getOpportunities() {
return (List<Opportunity>) setCon.getRecords();
}
}
Step 2: Create Visualforce Page
<apex:page controller="OpportunitiesPaginationController" tabStyle="Opportunity" sidebar="false">
<apex:form >
<apex:actionFunction name="refreshPageSize" action="{!refreshPageSize}" status="fetchStatus" reRender="pbId"/>
<apex:pageBlock id="pbId">
<apex:pageBlockSection title="All Opportunities" collapsible="false" columns="1">
<apex:pageBlockTable value="{!Opportunities}" var="oppObj">
<apex:column headerValue="Opportunity Name">
<a href="/{!oppObj.id}" target="_blank" ><apex:outputField value="{!oppObj.Name}"/></a>
</apex:column>
<apex:column headerValue="Account Name">
<a href="/{!oppObj.AccountId}" target="_blank" ><apex:outputField value="{!oppObj.Account.name}"/></a>
</apex:column>
<apex:column headerValue="Amount">
<apex:outputField value="{!oppObj.Amount}"/>
</apex:column>
<apex:column headerValue="Stage">
<apex:outputField value="{!oppObj.StageName}"/>
</apex:column>
<apex:column headerValue="Close Date">
<apex:outputField value="{!oppObj.CloseDate}"/>
</apex:column>
</apex:pageBlockTable>
<apex:panelGrid columns="8">
<apex:selectList value="{!size}" multiselect="false" size="1" onchange="refreshPageSize();">
<apex:selectOptions value="{!paginationSizeOptions}"/>
</apex:selectList>
<apex:commandButton status="fetchStatus" reRender="pbId" value="First" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>
<apex:commandButton status="fetchStatus" reRender="pbId" value="Previous" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
<apex:commandButton status="fetchStatus" reRender="pbId" value="Next" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
<apex:commandButton status="fetchStatus" reRender="pbId" value="Last" 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:outputPanel >
<apex:actionStatus id="fetchStatus" >
<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>
Step 3: Click on Preview button of visualforce page
Or
You can Go to the URL and type (Change the yoursalesforceinstance with your salesforce org URL)
When you use pagination, an exception is thrown when there are modified rows in the collection. This includes any new rows added to the collection through an extension action. The handling of error messages in this case follows the standard behavior and can either be displayed upon the page. For example, you can use the <apex:pageMessages> or <apex:messages> component to display an error message to the user.
Pros and Cons of Choosing This Pagination Tool
Pros:
- Manages data sets on the server, which reduces page state and increases performance
- Allows you to paginate over large data sets that have up to 10,000 records
- Includes built-in functionality such as next, previous, first, last, getResultSize, and other methods that can simplify your page
- Allows you to paginate forward and backward or to any page within the result set
- Uses a server-side cursor to cache the entire result set, so results will not change if data changes in the database while a user is paginating from one page to the next.
- Has built-in functionality that results in a very small increase in view state size
- Can be used only in Apex.
Enjoy! If you have any questions, comments, please feel free to let me know.
As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.
Next post: How To Learn Get Field Values From Visualforce Page To Apex Class Controller Without Losing Your Mind
Nice information thank you,if you want more information please visit our link salesforce certification
ReplyDelete