Salesforce Visualforce分页

分页的实现总体上分真分页和假分页。

所谓真分页指页面上列出来的数据就是实际查询的数据,假分页则是无论页面上一次显示多少条记录,实际上后台已经加载了所有的记录,分页只是为了展示给用户查看。今天分享一个Visualforce页面的真分页的实现

Apex 类:OppPageController

 1 /*******
 2    *
 3    * @作者:Ricardo
 4    * @Time:2018-06-05
 5    * @function:业务机会的分页列表展示
 6    *
 7    */
 8 public with sharing class OppPageController {
 9 
10    //分页参数
11    public  Integer counter=0;  //偏移量
12    public static  Integer LIST_SIZE = 10;//每页显示记录数
13    public  Integer total_size; //总记录数
14 
15    public OppPageController () {
16       total_size = [select count() from Opportunity]; 
17    }
18 
19    //变量 Opportunitys 的get方法
20    public List<Opportunity> getOpportunitys() {
21       try {
22          
23          List<Opportunity> Opportunitys= [select Id,Name,StageName,Account.Name,Type,Probability from Opportunity limit :LIST_SIZE  offset :counter];// limit x,y
24 
25          return Opportunitys;      
26       } catch (Exception e) {      
27          ApexPages.addMessages(e);   
28          return null;      
29       }
30    }
31 
32    //变量 DisablePrevious 的get方法
33    //控制上一页按钮是否可点击
34    public Boolean getDisablePrevious() { 
35       if (counter>0) 
36          return false; 
37       else 
38          return true;
39    }
40 
41    //变量 DisableNext 的get方法
42    //控制下一页按钮是否可点击
43    public Boolean getDisableNext() {
44       if (counter + LIST_SIZE   < total_size) 
45          return false; 
46       else 
47          return true;
48    }
49 
50    //变量 Total_size 的get方法
51    //返回Total_size的值
52    public Integer getTotal_size() {
53       return total_size;
54    }
55 
56    //变量 PageNumber 的get方法
57    //计算当前页码
58    public Integer getPageNumber() {
59       return counter/LIST_SIZE   + 1;
60    }
61 
62    //变量 TotalPages 的get方法
63    //计算总页数
64    public Integer getTotalPages() {
65       if (math.mod(total_size, LIST_SIZE )  > 0) {
66          return total_size/LIST_SIZE   + 1;
67       } else {
68          return (total_size/LIST_SIZE ) ;
69       }
70    }
71 
72    //首页
73    public PageReference First() {
74       counter = 0;
75       return null;
76    }
77 
78    //上一页
79    public PageReference Previous() { 
80       counter -= LIST_SIZE ; 
81       return null;
82    }
83 
84    //下一页
85    public PageReference Next() { 
86       counter += LIST_SIZE ; 
87       return null;
88    }
89 
90    //尾页
91    public PageReference End() { 
92       counter = total_size - math.mod(total_size, LIST_SIZE ) ;
93       return null;
94    }
95 }

Visualforce 页面

 1 <apex:page controller="OppPageController" showHeader="false">
 2 <style type="text/css">
 3   /* 控制footer居右显示 */
 4   .footer{
 5     text-align: right;
 6   }
 7 </style>
 8   <apex:sectionHeader subtitle="业务机会分页显示列表" title="业务机会"/>
 9   <apex:form >
10     <apex:pageBlock > 
11       <!--   显示错误异常信息 -->
12       <apex:pageMessages id="message"/>
13       <apex:pageBlockButtons location="bottom" style="text-align: center;">
14         <!-- 按钮显示效果 -->
15         <apex:outputPanel id="buttons">        
16           <apex:commandButton action="{!First}" title="First" value="首页" disabled="{!disablePrevious}" reRender="showpanel,buttons"/>
17           <apex:commandButton action="{!Previous}" title="Previous" value="上一页" disabled="{!disablePrevious}" reRender="showpanel,buttons"/>        
18           <apex:commandButton action="{!Next}" title="Next" value="下一页" disabled="{!disableNext}" reRender="showpanel,buttons"/>
19           <apex:commandButton action="{!End}" title="End" value="尾页" disabled="{!disableNext}" reRender="showpanel,buttons"/>  
20         </apex:outputPanel>
21       </apex:pageBlockButtons>
22 
23       <apex:outputPanel id="showpanel">
24         <apex:pageMessages id="theMessages" />
25         <apex:pageBlockTable value="{!Opportunitys}" var="opp" footerClass="footer">
26           <apex:column value="{!opp.Name}" />
27           <apex:column value="{!opp.StageName}" />
28           <apex:column value="{!opp.Account.Name}" />
29           <apex:column value="{!opp.Type}" />
30           <apex:column value="{!opp.Probability}" />           
31           <apex:facet name="footer">第{!pageNumber}/{!totalPages}页 共计{!total_size}条</apex:facet>
32         </apex:pageBlockTable>
33       </apex:outputPanel>  
34     </apex:pageBlock>
35   </apex:form>
36 </apex:page>

完成后的页面效果图

可以看到完成后的页面,是比较符合Salesforce原生态样式的

OppPageController中的代码结构比较简单,主要是根据偏移量,查询每次需要展示的记录数据,并刷新页面显示,以达到页面分页显示的效果,也就是说,这是一种真分页的实现。

本文仅供参考,如有错漏之处欢迎指正,如有疑问,欢迎评论区留言

原文地址:https://www.cnblogs.com/luqinghua/p/9139017.html