Apex:使用VF自帶標(biāo)簽實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)分頁功能

當(dāng)你想要在VF頁面上展示1000+的數(shù)據(jù)時(shí),你就必須使用分頁,否則就會(huì)報(bào)錯(cuò)“Collection size xxxx exceeds maximum size of 1000”。本文將教你如何使用VF自帶標(biāo)簽實(shí)現(xiàn)最基本的分頁功能。

一、需求闡述

自定義頁面,展示系統(tǒng)中所有accounts的名稱、類型和電話,點(diǎn)擊名稱可以進(jìn)入某個(gè)account的詳細(xì)頁面;并且在該自定義頁面中,可以編輯和刪除account。

二、邏輯梳理

這個(gè)需求里并沒有復(fù)雜的邏輯,實(shí)現(xiàn)起來也非常簡(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="名稱" 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="類型" 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. 效果
第一頁長(zhǎng)這樣~

第二頁~ 咦?出現(xiàn)了個(gè)Previous?

四、知識(shí)點(diǎn)回顧

1. ApexPages.StandardSetController

當(dāng)為一個(gè)標(biāo)準(zhǔn)Controller定義拓展時(shí),即可使用StandardSetController類。具體用法可參考官方文檔:

此外,在ApexPages命名空間下,還有以下幾個(gè)類比較常用:

  • ApexPages.Message: 使用此類將信息傳遞到前臺(tái)顯示,常用于顯示異常信息(系統(tǒng)異常or自定義異常);
  • ApexPages.Action: 實(shí)現(xiàn)前后臺(tái)交互。
2. PageReference

在之前的筆記中有過講述,這里不贅述?!?a href="http://www.itdecent.cn/p/68b2fb5b7d9b" target="_blank">傳送門

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.

一般來說,使用URLFOR有三個(gè)目的:獲取s-control的URL;獲取靜態(tài)資源的URL;獲取一個(gè)對(duì)象動(dòng)作的URL。在本例中,URLFOR用于獲取一個(gè)對(duì)象動(dòng)作的URL。那么,一個(gè)對(duì)象可以有哪些動(dòng)作呢?常見的有以下幾個(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)在這樣智能地顯示了(即有前一頁的時(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。下面我們就來辨析一下~

  • render:布爾值,默認(rèn)為true。用于顯示/隱藏某個(gè)block, output panel 或input/output fields 。
    我們也可以使用在render屬性內(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的頁面,你希望每次操作后都能把最近添加的記錄展示出來,那么就可以用reRerender來刷新。
  • renderAs:用于用HTML, pdf, excel等格式顯示VF頁面。
    for pdf – renderAs =”pdf”
    for HTML – renderAs =”html”
    for excel – <apex:page controller=”contactquery” contentType=”application/vnd.ms-excel#SalesForceExport.xls” cache=”true”>

五、總結(jié)

懶不得寫了~~


參考:
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/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評(píng)論 0 10
  • D4-32號(hào)-瘋帽先生-薩日朗 ?任務(wù)四 這是個(gè)剛開始涅槃的鳳凰,黑色代表她所受的折磨,白色代表升華的希望,藍(lán)色是...
    薩行颯風(fēng)閱讀 216評(píng)論 0 0
  • 真的是因?yàn)閴毫κ??因?yàn)槭謾C(jī)失眠?因?yàn)檎J(rèn)床失眠?我覺得我的失眠首先沒有困意,戓者忽然醒了,后面就開始豪情萬...
    中分大叔閱讀 213評(píng)論 0 0
  • 昨天沒更文。因?yàn)槊Γ?3點(diǎn)多才下班。忙的過程中居然忘了日更這回事。不知道是有意還是忙的緣故,就這樣停了一天。 今天...
    拾一片梧桐閱讀 198評(píng)論 0 0
  • petsym閱讀 175評(píng)論 0 0

友情鏈接更多精彩內(nèi)容