當(dāng)你想要在VF頁(yè)面上展示1000+的數(shù)據(jù)時(shí)砾跃,你就必須使用分頁(yè)朗若,否則就會(huì)報(bào)錯(cuò)“Collection size xxxx exceeds maximum size of 1000”。本文將教你如何使用VF自帶標(biāo)簽實(shí)現(xiàn)最基本的分頁(yè)功能瞳氓。
一、需求闡述
自定義頁(yè)面,展示系統(tǒng)中所有accounts的名稱(chēng)似谁、類(lèi)型和電話,點(diǎn)擊名稱(chēng)可以進(jìn)入某個(gè)account的詳細(xì)頁(yè)面掠哥;并且在該自定義頁(yè)面中巩踏,可以編輯和刪除account。
二续搀、邏輯梳理
這個(gè)需求里并沒(méi)有復(fù)雜的邏輯塞琼,實(shí)現(xiàn)起來(lái)也非常簡(jiǎn)單,下面直接上代碼>-<
三禁舷、代碼實(shí)現(xiàn)
1. Controller
public with sharing class SepPageCtl2 {
List<Account> accounts {get; set;}
public SepPageCtl2 (ApexPages.StandardController controller){
}
//instantiate the StandardSetController from a query locator
public ApexPages.StandardSetController con {
get{
if(con == null){
con = new ApexPages.StandardSetController(Database.getQueryLocator(
[SELECT Id, Name, Phone, Type, Industry, Owner.Name FROM Account ORDER BY Name LIMIT 100]));
// sets the number of records in each page set
con.setPageSize(5);
}
return con;
}
set;
}
// returns a list of account in the current page set
public List<Account> getAccounts(){
return (List<Account>) con.getRecords();
}
public PageReference process(){
return null;
}
// indicates whether there are more records after the current page set.
public Boolean hasNext{
get{
return con.getHasNext();
}
set;
}
// indicates whether there are more records before the current page set.
public Boolean hasPrevious{
get{
return con.getHasPrevious();
}
set;
}
// returns the page number of the current page set
public Integer pageNumber{
get{
return con.getPageNumber();
}
set;
}
// returns the first page of records
public void first(){
con.first();
}
// returns the last page of records
public void last(){
con.last();
}
// returns the previous page of records
public void previous(){
con.previous();
}
// returns the next page of records
public void next(){
con.next();
}
// returns the PageReference of the original page, if known, or the home page.
public void cancel(){
con.cancel();
}
}
2. VF Page
<apex:page id="SepPageDemo2" standardController="Account" extensions="SepPageCtl2">
<apex:form>
<apex:pageBlock title="Account List">
<apex:pageBlockSection title="Page {!pageNumber}" columns="1">
<apex:pageBlockTable value="{!Accounts}" var="a">
<apex:column headerValue="操作" style="width:5%">
<a href="{!URLFOR($Action.Account.Edit,a.id,[retURL=''])}">編輯</a>|
<a href="{!URLFOR($Action.Account.Delete,a.id,[retURL=''])}">刪除</a>
</apex:column>
<apex:column headerValue="名稱(chēng)" style="width:15%">
<apex:outputLink value="{!URLFOR($Action.Account.View,a.id,[retURL=''])}">{!a.Name}</apex:outputLink>
</apex:column>
<apex:column headerValue="電話" style="width:15%">
<a href="#" onclick="singheadDial('6112',{!a.Phone});" id="dial2">
<apex:outputPanel rendered="{!IF(a.Phone != '',true,false)}">
{!a.Phone}
</apex:outputPanel>
</a>
</apex:column>
<apex:column headerValue="類(lèi)型" style="width:15%">
<apex:outputField value="{!a.Type}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:panelGrid columns="4">
<apex:commandLink action="{!first}">First</apex:commandLink>
<apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandLink>
<apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandLink>
<apex:commandLink action="{!last}">Last</apex:commandLink>
</apex:panelGrid>
</apex:form>
</apex:page>
3. 效果
四、知識(shí)點(diǎn)回顧
1. ApexPages.StandardSetController
當(dāng)為一個(gè)標(biāo)準(zhǔn)Controller定義拓展時(shí)牵咙,即可使用StandardSetController類(lèi)派近。具體用法可參考官方文檔:
此外,在ApexPages命名空間下霜大,還有以下幾個(gè)類(lèi)比較常用:
- ApexPages.Message: 使用此類(lèi)將信息傳遞到前臺(tái)顯示构哺,常用于顯示異常信息(系統(tǒng)異常or自定義異常);
- ApexPages.Action: 實(shí)現(xiàn)前后臺(tái)交互。
2. PageReference
在之前的筆記中有過(guò)講述战坤,這里不贅述曙强。→傳送門(mén)
3. URLFOR
在文中有這樣一段代碼途茫,用于給“編輯”和“刪除”添加超鏈接碟嘴。
<apex:column headerValue="操作" style="width:5%">
<a href="{!URLFOR($Action.Account.Edit,a.id,[retURL=''])}">編輯</a>|
<a href="{!URLFOR($Action.Account.Delete,a.id,[retURL=''])}">刪除</a>
</apex:column>
其中,URLFOR用于獲取一個(gè)對(duì)象動(dòng)作的超鏈接囊卜。使用方法如下:
{!URLFOR(target, id, [inputs], [no override])}
- target: You can replace target with a URL or action, s-control or static resource.
- id: This is id of the object or resource name (string type) in support of the provided target.
- inputs: Any additional URL parameters you need to pass you can use this parameter. You will to put the URL parameters in brackets and separate them with commas. ex: [param1="value1", param2="value2"]
- no override: A Boolean value which defaults to false, it applies to targets for standard Salesforce pages. Replace "no override" with "true" when you want to display a standard Salesforce page regardless of whether you have defined an override for it elsewhere.
一般來(lái)說(shuō)娜扇,使用URLFOR有三個(gè)目的:獲取s-control的URL;獲取靜態(tài)資源的URL栅组;獲取一個(gè)對(duì)象動(dòng)作的URL雀瓢。在本例中,URLFOR用于獲取一個(gè)對(duì)象動(dòng)作的URL玉掸。那么刃麸,一個(gè)對(duì)象可以有哪些動(dòng)作呢?常見(jiàn)的有以下幾個(gè):
- View: Shows the detail page of an object
- Edit: Shows the object in Edit mode
- Delete: URL for deleting an object
- New: URL to create a new record of an object
- Tab: URL to the home page of an object
<!-- Use $Action global varialble to access the New action reference -->
<apex:outputLink value="{!URLFOR($Action.Account.New)}">New</apex:outputLink>
<br/>
<!-- View action requires the id parameter, a standard controller can be used to obtain the id -->
<apex:outputLink value="{!URLFOR($Action.Account.view, account.id)}">View</apex:outputLink>
<br/>
<!-- Edit action requires the id parameter, id is taken from standard controller in this example -->
<apex:outputLink value="{!URLFOR($Action.Account.Edit, account.id)}">Edit</apex:outputLink>
<br/>
<!-- Delete action requires the id parameter, also a confirm message is added to prevent deleting the record when clicked by mistake -->
<apex:outputLink value="{!URLFOR($Action.Account.delete, account.id)}" onclick="return window.confirm('Are you sure?');">Delete</apex:outputLink>
<br/>
<!-- From all custom buttons, links, s-controls and visualforce pages you can use the following to get the link of the object's homepage -->
<apex:outputLink value="{!URLFOR($Action.Account.Tab, $ObjectType.Account)}">Home</apex:outputLink>
關(guān)于URLFOR用于"獲取s-control的URL"和"獲取靜態(tài)資源的URL"的用法請(qǐng)參考http://salesforcesource.blogspot.com/2008/12/urlfor-function-finally-explained.html(可能需要翻墻才能看)司浪。
4. rendered
在文中有這樣一段代碼泊业,筆者發(fā)現(xiàn)把rendered去掉之后把沼,Next和Previous就不能像現(xiàn)在這樣智能地顯示了(即有前一頁(yè)的時(shí)候才顯示Previous,否則不顯示吁伺,Next同理)饮睬。那rendered究竟有啥用呢?事實(shí)上篮奄,render用于顯示/隱藏某個(gè)block, output panel 或input/output fields 捆愁。
<apex:panelGrid columns="4">
<apex:commandLink action="{!first}">First</apex:commandLink>
<apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandLink>
<apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandLink>
<apex:commandLink action="{!last}">Last</apex:commandLink>
</apex:panelGrid>
經(jīng)常會(huì)有人混淆render, reRerender和renderAs。下面我們就來(lái)辨析一下~
-
render:布爾值宦搬,默認(rèn)為true牙瓢。用于顯示/隱藏某個(gè)block, output panel 或input/output fields 。
我們也可以使用在render屬性?xún)?nèi)使用IF條件间校,e.g.
<apex:inputField id="Id1" value="{!Obj.Field1}" rendered="{!IF(Obj.fieldname = 'dk'|| Obj.fieldname = 'Dinesh' ,true,false)}"/>
- reRerender:用于在服務(wù)器響應(yīng)完成后刷新output panel, block或fields矾克。例如,你有一個(gè)用于添加account和相應(yīng)contact的頁(yè)面憔足,你希望每次操作后都能把最近添加的記錄展示出來(lái)胁附,那么就可以用reRerender來(lái)刷新。
-
renderAs:用于用HTML, pdf, excel等格式顯示VF頁(yè)面滓彰。
for pdf – renderAs =”pdf”
for HTML – renderAs =”html”
for excel – <apex:page controller=”contactquery” contentType=”application/vnd.ms-excel#SalesForceExport.xls” cache=”true”>
五控妻、總結(jié)
懶不得寫(xiě)了~~
參考:
https://www.cnblogs.com/zero-zyq/p/5343287.html
https://blog.csdn.net/itsme_web/article/details/68063029
https://sfdcpanther.wordpress.com/2017/10/04/difference-between-render-rerender-and-renderas/