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中的代碼結構比較簡單,主要是根據偏移量,查詢每次需要展示的記錄數據,並刷新頁面顯示,以達到頁面分頁顯示的效果,也就是說,這是一種真分頁的實現。

本文僅供參考,如有錯漏之處歡迎指正,如有疑問,歡迎評論區留言

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM