StandardSetController with Pagination in VisualForce With Example.
I had always struggled to understand how to implement Pagination with StandardSetController. I decided to try hands on with below Simple Example.
So, basically our use case would be to have a custom Page with selective records to be displayed with pagination. What i mean by pagination is, you should be able to use Next & Previous Links to navigate and view previous or next set of records on a single page. We will also override the standard Oppty Page when you click on Opportunities Tab in Salesforce.
So, we are doing below things:
- Using Std Set Controller in custom Controller Apex Class to get list of Custom Opp Records.
- Using Std Set Controller vanilla out of box functions to implement Pagination using Next & Previous command links (objController.previous and objController.next) — you dont have to define these functions in Custom Apex Class as they are available to us as we use Std Set Controller.
- Using Std Set Controller vanilla out of box functions available from salesforce to basically compute current page number, page Size and total pages.(objController.getResultSize() and objController.getPageSize()) https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardSetController_methods.htm
- Using TabStyle=Opportunity in VF Page to basically navigate the New VF Page on click to Opportunities tab.
- We also use here custom CSS inside VF page just to highlight the page number we are on.
Code below: 1. Apex Class for VF Page — Custom Controller Class
Code 2: VF Page:
Code Link:
Apex Class: https://github.com/musaratSayed/blogMediumCode/blob/master/StdSetControllerWithPaginationOpps.cls
VF Page: https://github.com/musaratSayed/blogMediumCode/blob/master/StdSetControllerWithPagination.page