salesforce 零基础学习(四十九)自定义列表分页之使用Pagination实现分页效果 ※※※

上篇内容为Pagination基类的封装,此篇接上篇内容描述如何调用Pagination基类。

首先先创建一个sObject,起名Company info,此object字段信息如下:

为了国际化考虑,setup中输入框输入translate对字段的label进行国际化处理,此处只处理中文。

sobject创建好之后,我们便要进行设计。首先要有一个helper类,对相关查询方法进行封装,然后controller层进行调用以及和page页面交互一个page页面显示。

MyPaginationEnhancement设计:

  1 public without sharing class MyPaginationEnhancement{
  2     
  3     public static final Integer DEFAULT_PAGE_SIZE=20;
  4 
  5     public MyPaginationEnhancement() {
  6     }
  7     
  8     public MyPaginationEnhancement(ApexPages.StandardSetController controller) {                  
  9     }
 10     
 11     public MyPaginationEnhancement(ApexPages.StandardController controller) {      
 12     }
 13     
 14     private List<sObject> sObjectList;
 15 
 16     private String countStr;
 17 
 18     private String queryStr;
 19 
 20     private String queryCondition;
 21     
 22     private String groupBy;
 23 
 24     private String orderBy;
 25 
 26     private Integer offset=0;
 27     
 28     public List<sObject> resultList{
 29         get {
 30             if(sObjectList == null){
 31                 return new List<sObject>();
 32             }
 33             return sObjectList;            
 34         }
 35         set;
 36     }
 37     
 38     public List<sObject> getQueryResult(String countStr,String queryStr,String queryCondition){
 39         setQueryCondition(countStr,queryStr,queryCondition,null,null);
 40         buildAndQuery();        
 41         return sObjectList;
 42     }
 43     
 44     public List<sObject> getQueryResult(String countStr,String queryStr,String queryCondition,String groupBy,String orderBy){
 45         setQueryCondition(countStr,queryStr,queryCondition,groupBy,orderBy);
 46         buildAndQuery();
 47         return sObjectList;
 48     }
 49     
 50     public List<sObject> updateQueryResult(String queryStr,String queryCondition,String groupBy,String orderBy){
 51         String querySql = queryStr;
 52         if(queryCondition!=null){
 53             querySql += queryCondition;
 54         }
 55         if(groupBy!=null){
 56             querySql +=groupBy;
 57         }
 58         if(orderBy!=null){
 59             querySql +=orderBy;
 60         }
 61         sObjectList = Database.query(querySql);
 62         return sObjectList;
 63     }
 64 
 65     private void setQueryCondition(String countStr,String queryStr,String queryCondition,String groupBy,String orderBy){
 66         this.countStr=countStr;
 67         this.queryStr=queryStr;
 68         this.queryCondition=queryCondition;
 69         this.groupBy=groupBy;
 70         this.orderBy=orderBy;
 71     }
 72     
 73     private void buildAndQuery(){
 74         List<String> queryArgs = new List<String>();
 75         List<String> countArgs= new List<String>();
 76         if(String.isNotBlank(countStr)){
 77             countArgs.add(countStr);
 78         }
 79         if(String.isNotBlank(queryStr)){
 80             queryArgs.add(queryStr);
 81         }
 82         if(String.isNotBlank(queryCondition)){
 83             queryArgs.add(queryCondition);
 84             countArgs.add(queryCondition);
 85         }
 86         if(String.isNotBlank(groupBy)){
 87             queryArgs.add(groupBy);
 88             countArgs.add(groupBy);
 89         }        
 90         if(String.isNotBlank(orderBy)){
 91             queryArgs.add(orderBy);
 92         }
 93         initTotalNum(countArgs);        
 94         queryResult(queryArgs);
 95     }    
 96 
 97     private void initTotalNum(List<String> countArgs){
 98        String countqueryStr=String.join(countArgs,' ');
 99        
100        if(String.isNotBlank(countqueryStr)){
101                
102            totalNumber=Database.countquery(countqueryStr);
103        } else {
104             totalNumber=0;
105        }
106        
107        if(totalNumber !=0 && pageNumber==0){
108            pageNumber = 1;
109        }
110     }     
111     
112     private List<sObject> queryResult(List<String> queryArgs){
113         queryStr=String.join(queryArgs,' ');       
114         if(String.isBlank(queryStr)){
115             sObjectList = new List<sObject>();            
116         }else{
117             String querySql=queryStr+' limit '+pageSize+' offset '+offset;            
118             
119             sObjectList = Database.query(querySql);
120         }
121         return sObjectList;
122     }
123             
124     public void changePageSize(Integer pageSize) {        
125         if (pageSize!=null){                    
126             this.pageSize=pageSize;                     
127         }       
128     }    
129     
130     public Boolean hasNext {
131         get {
132             return pageSize*pageNumber<totalNumber;               
133         }
134         set;
135     } 
136  
137     public Boolean hasPrevious {
138         get {
139             return pageSize*(pageNumber-1)>0;            
140         }
141         set;
142     } 
143    
144     public Integer pageNumber {
145         get {
146             if(pageNumber==null){
147                 pageNumber=0;
148             }
149             return pageNumber;                 
150         }
151         set;
152     }
153     
154     public Integer pageSize{
155         get{
156             if(pageSize==null){
157                 pageSize=DEFAULT_PAGE_SIZE;               
158             } 
159             return pageSize;          
160         }
161         set;
162     }
163     
164     public Integer totalNumber{
165         get{
166             if(totalNumber==null){
167                 totalNumber=0;
168             }
169             return totalNumber;
170         }
171         set;
172     }
173     
174     public Integer totalPage{
175         get{
176             if(totalNumber==0 || math.mod(totalNumber,pageSize)!=0){                       
177                 return totalNumber/pageSize+1;                        
178             }else{
179                 return totalNumber/pageSize;
180             }          
181         }
182         set;
183     }
184     
185     public Boolean hasRecord{
186         get {
187             if(totalNumber!=0){
188                 return true;
189             }else{
190                 return false;
191             }           
192         }
193         set;
194     } 
195     
196     public void  first() {
197         offset=0;
198         pageNumber=1;       
199     } 
200     
201     public void  last() {
202         offset=(totalPage-1)*pageSize;
203         pageNumber=totalPage;                       
204     } 
205    
206     public void  previous() {
207         pageNumber--;
208         if(pageNumber<0){
209             pageNumber=0;
210             offset=0;
211         }else{
212             offset=(pageNumber-1)*pageSize;
213         }          
214     } 
215    
216     public void  next() {             
217         pageNumber++;       
218         if(pageNumber>totalPage){
219             pageNumber=totalPage;
220         }      
221         offset=(pageNumber-1)*pageSize;        
222     }
223     
224      //指定页
225     public virtual void specifiedPage(Integer pageNumber) {
226         this.pageNumber = pageNumber;
227         if(pageNumber < 0) {
228             pageNumber = 0;
229             offset = 0;
230         } else {
231             offset = (pageNumber - 1) * pageSize;
232         }
233     }
234     
235     public Integer getOffset(){
236             return offset;
237     }
238 
239     public Integer getSize(){
240             return pageSize;
241     }    
242 
243 }

CompanyHelper设计:

 1 public without sharing class CompanyHelper {
 2     
 3     public static final String BASE_COMPANY_QUERY = 'SELECT Company_Code_Unique__c, Name,' + 
 4                                                     ' Company_Name__c, Company_Phone__c, Company_Place__c,' +
 5                                                     ' Company_Type__c, CreatedById, CreatedDate, IsDeleted,' + 
 6                                                     ' Employees_Number__c, LastModifiedById, LastModifiedDate,' +
 7                                                     ' OwnerId, Id, SystemModstamp FROM Company_Info__c where IsDeleted = false';
 8     public static final String BASE_COMPANY_COUNT_QUERY = 'SELECT count() from Company_Info__c where IsDeleted = false';
 9     public static MyPaginationEnhancement getCompanyList(String companyName,String companyCode,String companyPlace,String companyType,MyPaginationEnhancement pagination) {
10         String queryCondition= '';
11         String orderBy ='';
12         if(companyName != null) {
13             queryCondition += ' and Company_Name__c like %\'' + companyName + '%\'';
14         }
15         if(companyCode != null) {
16             queryCondition += ' and Company_Code_Unique__c like %\'' + companyCode + '%\'';
17         }
18         if(companyPlace != null) {
19             queryCondition += ' and Company_Place__c like %\'' + companyPlace + '%\'';
20         }
21         if(companyType != null) {
22             queryCondition += ' and Company_Type__c like %\'' + companyType + '%\'';
23         }
24         
25         orderBy = ' order by createddate';
26         
27         pagination.getQueryResult(BASE_COMPANY_COUNT_QUERY,BASE_COMPANY_QUERY,queryCondition,null,orderBy);
28        // pagination.getQueryResult();
29         return pagination;
30     }
31 }

CompanyController设计

 1 public with sharing class CompanyController {
 2     
 3     public Map<String,String> parameters;
 4     
 5     public CompanyController() {
 6         parameters=ApexPages.currentPage().getParameters();
 7         init();
 8     }
 9     
10     public MyPaginationEnhancement pagination = new MyPaginationEnhancement();
11     
12     public String companyName{get;set;}
13     
14     public String companyCode{get;set;}
15     
16     public String companyPlace{get;set;}
17     
18     public String companyType{get;set;}
19     
20     public void init() {
21         queryByCondition();
22     }
23     
24     public void queryByCondition() {
25         CompanyHelper.getCompanyList(companyName,companyCode,companyPlace,companyType,pagination);
26         system.debug('====================CurrentPageNumber : ' + pagination.pageNumber);
27     }
28     
29     public MyPaginationEnhancement resultPagination{
30         get{
31             if(pagination ==null){
32                pagination =new MyPaginationEnhancement();
33             }
34             return pagination;
35         }
36         set;
37     }
38     
39     public List<Company_Info__c> resultList{
40         get{
41             if(pagination==null || pagination.resultList==null){
42                return new List<Company_Info__c>(); 
43             }
44             return pagination.resultList;
45         }
46         set;
47     }
48     
49     public void firstPage() {
50        pagination.first();
51        queryByCondition();     
52     } 
53     
54     public void lastPage() {
55        pagination.last();
56        queryByCondition();            
57     } 
58    
59     public void previousPage() {
60        pagination.previous();
61        queryByCondition();      
62     } 
63    
64     public void nextPage() {
65        pagination.next();
66        queryByCondition();
67     }
68 }

CompanyListPage设计

 1 <apex:page controller="CompanyController">
 2     <apex:form >
 3         <apex:outputPanel layout="block">
 4             <apex:outputPanel layout="block">
 5                 <apex:outputPanel layout="block">
 6                     <apex:outputPanel layout="block" id="companyList">
 7                         <apex:dataTable align="center" value="{!resultList}" var="companyInfo">
 8                             <apex:column style="width:180px;">
 9                                 <apex:facet name="header">{!$ObjectType.Company_Info__c.fields.Company_Code_Unique__c.label}</apex:facet>
10                                 <apex:outputText value="{!companyInfo.Company_Code_Unique__c}" />
11                             </apex:column>
12                             <apex:column style="width:180px;">
13                                 <apex:facet name="header">{!$ObjectType.Company_Info__c.fields.Company_Name__c.label}</apex:facet>
14                                 <apex:outputText value="{!companyInfo.Company_Name__c}" />
15                             </apex:column>
16                             <apex:column style="width:225px;">
17                                 <apex:facet name="header">{!$ObjectType.Company_Info__c.fields.Company_Place__c.label}</apex:facet>
18                                 <apex:outputText value="{!companyInfo.Company_Place__c}" />
19                             </apex:column>
20                             <apex:column style="width:225px;">
21                                 <apex:facet name="header">{!$ObjectType.Company_Info__c.fields.Company_Type__c.label}</apex:facet>
22                                 <apex:outputText value="{!companyInfo.Company_Type__c}" />
23                             </apex:column>
24                             <apex:column style="width:225px;">
25                                 <apex:facet name="header">{!$ObjectType.Company_Info__c.fields.Employees_Number__c.label}</apex:facet>
26                                 <apex:outputText value="{!companyInfo.Employees_Number__c}" />
27                             </apex:column>
28                             <apex:column style="width:500px;">
29                                 <apex:facet name="header">操作</apex:facet>
30                                 
31                             </apex:column>
32                         </apex:dataTable>
33 
34                         <apex:outputPanel layout="block" styleClass="paginator"
35                             style="padding:0px;">
36                             <apex:panelGrid columns="2" style="width:100%;"
37                                 styleClass="az_text_table" rowClasses="paginator,paginator">
38                                 <apex:outputText rendered="{!!resultPagination.hasRecord}"
39                                     value="第 0 页,共 0 页,每页 {!resultPagination.pageSize} 条" />
40                                 <apex:outputText rendered="{!resultPagination.hasRecord}"
41                                     value="第 {!resultPagination.pageNumber} 页,共 {!resultPagination.totalPage} 页,每页 {!resultPagination.pageSize} 条" />
42                                 <apex:panelGroup >
43                                     <apex:outputPanel >
44                                         <apex:outputText value="首页"
45                                             rendered="{!(!resultPagination.hasRecord)||(!resultPagination.hasPrevious)}"
46                                             style="border: solid 1px #ddd;padding:1px 6px;background: #e8e8e9;margin-right:5px;"></apex:outputText>
47                                         <apex:commandLink action="{!firstPage}"
48                                             rendered="{!resultPagination.hasRecord && resultPagination.hasPrevious}"
49                                             immediate="true" reRender="companyList" value="首页"
50                                             style="margin-right:5px;" />
51                                     </apex:outputPanel>
52                                     <apex:outputPanel >
53                                         <apex:outputText value="上一页"
54                                             rendered="{!!resultPagination.hasRecord || (!resultPagination.hasPrevious)}"
55                                             style="border: solid 1px #ddd;padding:1px 6px;background: #e8e8e9;margin-right:5px;"></apex:outputText>
56                                         <apex:commandLink action="{!previousPage}"
57                                             rendered="{!resultPagination.hasRecord && resultPagination.hasPrevious}"
58                                             immediate="true" reRender="companyList" value="上一页"
59                                             style="margin-right:5px;" />
60                                     </apex:outputPanel>
61                                     <apex:outputPanel >
62                                         <apex:outputText value="{!resultPagination.pageNumber}"
63                                             styleClass="current" />
64                                     </apex:outputPanel>
65                                     <apex:outputPanel >
66                                         <apex:outputText value="下一页"
67                                             rendered="{!!resultPagination.hasRecord || !resultPagination.hasNext}"
68                                             style="border: solid 1px #ddd;padding:1px 6px;background: #e8e8e9;margin-right:5px;margin-left:5px;"></apex:outputText>
69                                         <apex:commandLink action="{!nextPage}"
70                                             rendered="{!resultPagination.hasRecord && resultPagination.hasNext}"
71                                             immediate="true" reRender="companyList" value="下一页"
72                                             style="margin-right:5px;margin-left:5px;" />
73                                     </apex:outputPanel>
74                                     <apex:outputPanel >
75                                         <apex:outputText value="尾页"
76                                             rendered="{!!resultPagination.hasRecord || !resultPagination.hasNext}"
77                                             style="border: solid 1px #ddd;padding:1px 6px;background: #e8e8e9;margin-right:5px;"></apex:outputText>
78                                         <apex:commandLink action="{!lastPage}"
79                                             rendered="{!resultPagination.hasRecord && resultPagination.hasNext}"
80                                             immediate="true" reRender="companyList" value="尾页"
81                                             style="margin-right:5px;" />
82                                     </apex:outputPanel>
83                                 </apex:panelGroup>
84                             </apex:panelGrid>
85                         </apex:outputPanel>
86                     </apex:outputPanel>
87                 </apex:outputPanel>
88             </apex:outputPanel>
89         </apex:outputPanel>
90     </apex:form>
91 </apex:page>

其中,CompanyListPage中没有对页面进行样式调整,也没有做相关search,new,edit以及view的操作,感兴趣的小伙伴可以自行补充。

显示效果:

1.语言为英文(我的默认语言)情况下:

2.语言为中文情况下:

3.翻页效果:

4.尾页效果:

总结:此篇结合上篇实现了如果不用标准界面的分页自己创建分页的实例展示,其中有考虑不细致的地方,比如offset超过2000应该有一定提示等,也有没有做完的地方,比如模糊搜索和增删改功能。如果篇中有错误地方欢迎指正,如果有不懂的地方欢迎留言。(ps:效果显示请忽略UI)

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏曾大稳的博客

MediaCodec判断是否可以采用硬解码

具体的类型对应关系可以查看相关文档,这里在Android源码MediaCodec.createDecoderByType()里面有一些相关的对应支持类型。

1132
来自专栏小筱月

java ssm框架实现分页功能 (oracle)

LIMIT a,b : 参数 a:第 a 条数据开始查询(不包括第 a 条), 参数 b:查询 b 条数据

1082
来自专栏码农阿宇

C# MVC的一种高效分页的html方法

首先创建一个html的扩展方法,这个方法是万能的,可以直接拿到您的项目中使用: //主要就是输出分页的超级链接的标签 //自定义分页Helpe...

43411
来自专栏冰枫

CountDownLatch和CyclicBarrier解决运动员赛跑多线程问题

最近有道多线程的面试题: 五个运动员(相当于五个线程),一个裁判(Main线程),满足以下三个条件,如何实现: 1.同时起跑 2.要所有运动员都到达终点以...

4197
来自专栏函数式编程语言及工具

SDP(2):ScalikeJDBC-Connection Pool Configuration

  scalikeJDBC可以通过配置文件来设置连接池及全局系统参数。对配置文件的解析是通过TypesafeConfig工具库实现的。默认加载classpath...

3024
来自专栏salesforce零基础学习

salesforce 零基础学习(四十八)自定义列表分页之Pagination基类封装 ※※※

我们知道,salesforce中系统标准列表页面提供了相应的分页功能,如果要使用其分页功能,可以访问https://cloud.tencent.com/deve...

3427
来自专栏码匠的流水账

聊聊springcloud的featuresEndpoint

spring-cloud-commons-2.0.0.RC1-sources.jar!/org/springframework/cloud/client/Com...

481
来自专栏大数据学习笔记

nested exception is java.lang.ClassCastException:XXX cannot be cast to java.io.Serializable

Type Exception Report Message Request processing failed; nested exception is ja...

2675
来自专栏积累沉淀

研究MapReduce源码之实现自定义LineRecordReader完成多行读取文件内容

TextInputFormat是Hadoop默认的数据输入格式,但是它只能一行一行的读记录,如果要读取多行怎么办? 很简单 自己写一个输入格式,然后写一个对...

1779
来自专栏一个会写诗的程序员的博客

Spring Data JPA WITH Kotlin

异常消息: No default constructor for entity: : com.ak47.cms.cms.entity.StockIndex; n...

782

扫码关注云+社区