Pagination using standardsetcontroller

As the number of records increases, the time required for the browser to render them increases. Paging is used to reduce the amount of data exchanged with the client. Paging is typically handled on the server side (standardsetcontroller). The page sends parameters to the controller, which the controller needs to interpret and then respond with the appropriate data set.

 

Here is the controller which makes use of standard set controller for pagination

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public with sharing class Pagination {
     Public Integer noOfRecords{get; set;}
     Public Integer size{get;set;}
     public ApexPages.StandardSetController setCon {
         get{
             if (setCon == null ){
                 size = 10 ;
                 string queryString = 'Select Name, Type, BillingCity, BillingState, BillingCountry from Account order by Name' ;
                 setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                 setCon.setPageSize(size);
                 noOfRecords = setCon.getResultSize();
             }
             return setCon;
         }set;
     }
     
     Public List<Account> getAccounts(){
         List<Account> accList = new List<Account>();
         for (Account a : (List<Account>)setCon.getRecords())
             accList.add(a);
         return accList;
     }
     
     public pageReference refresh() {
         setCon = null ;
         getAccounts();
         setCon.setPageNumber( 1 );
         return null ;
     }
     
     public Boolean hasNext {
         get {
             return setCon.getHasNext();
         }
         set;
     }
     public Boolean hasPrevious {
         get {
             return setCon.getHasPrevious();
         }
         set;
     }
  
     public Integer pageNumber {
         get {
             return setCon.getPageNumber();
         }
         set;
     }
  
     public void first() {
         setCon.first();
     }
  
     public void last() {
         setCon.last();
     }
  
     public void previous() {
         setCon.previous();
     }
  
     public void next() {
         setCon.next();
     }
}

Using the above controller methods we can define the pagination.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
< apex:page controller = "Pagination" >
     < apex:form >
         < apex:pageBlock id = "pb" >
             < apex:pageBlockTable value = "{!Accounts}" var = "a" >
                 < apex:column value = "{!a.Name}" />
                 < apex:column value = "{!a.Type}" />
                 < apex:column value = "{!a.BillingCity}" />
                 < apex:column value = "{!a.BillingState}" />
                 < apex:column value = "{!a.BillingCountry}" />
             </ apex:pageBlockTable >
             < apex:panelGrid columns = "7" >
                 < apex:commandButton status = "fetchStatus" reRender = "pb" value = "|<" action = "{!first}" disabled = "{!!hasPrevious}" title = "First Page" />
                 < apex:commandButton status = "fetchStatus" reRender = "pb" value = "<" action = "{!previous}" disabled = "{!!hasPrevious}" title = "Previous Page" />
                 < apex:commandButton status = "fetchStatus" reRender = "pb" value=">" action="{!next}" disabled="{!!hasNext}" title="Next Page"/>
                 < apex:commandButton status = "fetchStatus" reRender = "pb" value=">|" action="{!last}" disabled="{!!hasNext}" title="Last Page"/>
                 < apex:outputText >{!(pageNumber * size)+1-size}-{!IF((pageNumber * size)>noOfRecords, noOfRecords,(pageNumber * size))} of {!noOfRecords}</ apex:outputText >
                 < apex:commandButton status = "fetchStatus" reRender = "pb" value = "Refresh" action = "{!refresh}" title = "Refresh Page" />
                 < apex:outputPanel style = "color:#4AA02C;font-weight:bold" >
                     < apex:actionStatus id = "fetchStatus" startText = "Fetching..." stopText = "" />
                 </ apex:outputPanel >
             </ apex:panelGrid >
         </ apex:pageBlock >
     </ apex:form >
</ apex:page >

Do you really feel the code is little larger then what you expect, here is the minified version of controller and page
Controller Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public with sharing class Pagination_min {
     Public Integer noOfRecords{get; set;}
     Public Integer size{get;set;}
     public ApexPages.StandardSetController setCon {
         get{
             if (setCon == null ){
                 size = 10 ;
                 string queryString = 'Select Name, Type, BillingCity, BillingState, BillingCountry from Account order by Name' ;
                 setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                 setCon.setPageSize(size);
                 noOfRecords = setCon.getResultSize();
             }
             return setCon;
         }set;
     }
     
     Public List<Account> getAccounts(){
         List<Account> accList = new List<Account>();
         for (Account a : (List<Account>)setCon.getRecords())
             accList.add(a);
         return accList;
     }
     
     public pageReference refresh() {
         setCon = null ;
         getAccounts();
         setCon.setPageNumber( 1 );
         return null ;
     }
}

Page Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
< apex:page controller = "Pagination_min" >
     < apex:form >
         < apex:pageBlock id = "pb" >
             < apex:pageBlockTable value = "{!Accounts}" var = "a" >
                 < apex:column value = "{!a.Name}" />
                 < apex:column value = "{!a.Type}" />
                 < apex:column value = "{!a.BillingCity}" />
                 < apex:column value = "{!a.BillingState}" />
                 < apex:column value = "{!a.BillingCountry}" />
             </ apex:pageBlockTable >
             < apex:panelGrid columns = "7" >
                 < apex:commandButton status = "fetchStatus" reRender = "pb" value = "|<" action = "{!setCon.first}" disabled = "{!!setCon.hasPrevious}" title = "First Page" />
                 < apex:commandButton status = "fetchStatus" reRender = "pb" value = "<" action = "{!setCon.previous}" disabled = "{!!setCon.hasPrevious}" title = "Previous Page" />
                 < apex:commandButton status = "fetchStatus" reRender = "pb" value=">" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
                 < apex:commandButton status = "fetchStatus" reRender = "pb" value=">|" 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:commandButton status = "fetchStatus" reRender = "pb" value = "Refresh" action = "{!refresh}" title = "Refresh Page" />
                 < apex:outputPanel style = "color:#4AA02C;font-weight:bold" >
                     < apex:actionStatus id = "fetchStatus" startText = "Fetching..." stopText = "" />
                 </ apex:outputPanel >
             </ apex:panelGrid >
         </ apex:pageBlock >
     </ apex:form >
</ apex:page >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值