本篇介紹通過使用VF自帶標簽和Apex實現簡單的數據翻頁功能。
代碼上來之前首先簡單介紹一下本篇用到的主要知識:
1.ApexPages命名空間
此命名空間下的類用於VF的控制。
主要的類包括但不限於以下:
- ApexPages.StandardController:當為一個標准Controller定義擴展的時候使用此類。StandardController對象為Salesforce提供的預構建VF的控制器對象引用;
- ApexPages.Action:使用Action類和方法用於VF自定義控制器和擴展中,實現前后台交互;
- ApexPages.Message:可以使用此類將信息傳遞到前台顯示,常用於顯示異常信息(系統異常or自定義異常);
2.PageReference類
PageReference類位於System命名空間下,用於一個實例化頁面的引用。他的作用為可以通過方法將結果導航到其他頁面,可以視圖。
3.基礎知識(當我沒說)
如果此部分掌握不好,請移步官方PDF文檔,先好好鑽研一下基礎知識。畢竟基礎還是最重要的。
注:上述只是介紹較為常用的內容,如果需要深入了解關於前后台交互的內容,請詳細查閱官方PDF,掌握好ApexPages以及Controller等等之間的關系及交互。
廢話少說,上代碼,以Goods表為例,前幾篇有過介紹,這里只是說一下里面的field主要內容:
GoodsName__c, GoodsType__c, GoodsBrands__c, GoodsPrice__c。
1 public class CategoryWrapper { 2 public Boolean checked{ get; set; } 3 public GOODS__c goods { get; set;} 4 5 public CategoryWrapper(){ 6 goods = new GOODS__c(); 7 checked = false; 8 } 9 10 public CategoryWrapper(GOODS__c goods){ 11 this.goods = goods; 12 checked = false; 13 } 14 }
CategoryWrapper類有兩個變量,一個為Goods對象,用來獲取商品基本信息,一個為布爾類型的checked,用來作為判斷數據行是否被選的屬性。
1 public with sharing class PagingController { 2 3 List<categoryWrapper> categories {get;set;} 4 5 // instantiate the StandardSetController from a query locator 6 public ApexPages.StandardSetController con { 7 get { 8 if(con == null) { 9 con = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT GOODSBRAND__c,GOODSDESCRIBE__c,GOODSNAME__c, GOODSTYPE__c, GoodsPrice__c, IsStatus__c, Id FROM GOODS__c limit 100])); 10 // sets the number of records in each page set 11 con.setPageSize(20); 12 } 13 return con; 14 } 15 set; 16 } 17 18 // returns a list of wrapper objects for the sObjects in the current page set 19 public List<categoryWrapper> getCategories() { 20 categories = new List<categoryWrapper>(); 21 for (GOODS__c category1 : (List<GOODS__c>)con.getRecords()) 22 categories.add(new CategoryWrapper(category1)); 23 24 return categories; 25 } 26 27 // displays the selected items 28 public PageReference process() { 29 for (CategoryWrapper cw : categories) { 30 if (cw.checked) 31 ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,cw.goods.GOODSNAME__c)); 32 } 33 return null; 34 } 35 36 // indicates whether there are more records after the current page set. 37 public Boolean hasNext { 38 get { 39 return con.getHasNext(); 40 } 41 set; 42 } 43 44 // indicates whether there are more records before the current page set. 45 public Boolean hasPrevious { 46 get { 47 return con.getHasPrevious(); 48 } 49 set; 50 } 51 52 // returns the page number of the current page set 53 public Integer pageNumber { 54 get { 55 return con.getPageNumber(); 56 } 57 set; 58 } 59 60 // returns the first page of records 61 public void first() { 62 con.first(); 63 } 64 65 // returns the last page of records 66 public void last() { 67 con.last(); 68 } 69 70 // returns the previous page of records 71 public void previous() { 72 con.previous(); 73 } 74 75 // returns the next page of records 76 public void next() { 77 con.next(); 78 } 79 80 // returns the PageReference of the original page, if known, or the home page. 81 public void cancel() { 82 con.cancel(); 83 } 84 85 }
使用StandardController控制頁面顯示的內容以及每頁顯示多少行數據,是否含有上一頁下一頁等等功能。通過PageReference作為當前頁面的引用,控制頁面數據。
1 <apex:page controller="PagingController"> 2 <apex:form > 3 <apex:pageBlock title="Goods"> 4 5 <apex:pageBlockButtons location="top"> 6 <apex:commandButton action="{!process}" value="Selected" /> 7 <apex:commandButton action="{!cancel}" value="Cancel" /> 8 </apex:pageBlockButtons> 9 <apex:pageMessages /> 10 11 <apex:pageBlockSection title="Goods - Page {!pageNumber}" columns="1"> 12 <apex:pageBlockTable value="{!categories}" var="c"> 13 <apex:column width="25px"> 14 <apex:inputCheckbox value="{!c.checked}" /> 15 </apex:column> 16 <apex:column value="{!c.goods.GoodsName__c}" headerValue="Name" /> 17 <apex:column value="{!c.goods.GoodsType__c}" headerValue="type" /> 18 <apex:column value="{!c.goods.GoodsBrand__c}" headerValue="brand" /> 19 <apex:column value="{!c.goods.GoodsPrice__c}" headerValue="Price" /> 20 </apex:pageBlockTable> 21 </apex:pageBlockSection> 22 </apex:pageBlock> 23 24 <apex:panelGrid columns="4"> 25 <apex:commandLink action="{!first}">First</apex:commandlink> 26 <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink> 27 <apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink> 28 <apex:commandLink action="{!last}">Last</apex:commandlink> 29 </apex:panelGrid> 30 31 </apex:form> 32 </apex:page>
頁面顯示樣式如下:
總結:本篇只是簡單的實現數據分頁功能,在真正項目中應該很少會有直接使用VF標簽和使用Apex接口配合實現分頁的(吐槽:自動忽略。。。因為VF的布局很丑),通常使用HTML的布局結合着Controller實現精美樣式, 不過可以通過本篇的內容了解ApexPage命名空間里的類和VF頁面的關系以及PageReference的用法和作用,如果內容有寫的錯誤的地方歡迎批評指正,如果有問題,請留言。