Apex:使用VF自带标签实现简单数据分页功能

2019-02-12  本文已影响0人  江江酱酱233

当你想要在VF页面上展示1000+的数据时,你就必须使用分页,否则就会报错“Collection size xxxx exceeds maximum size of 1000”。本文将教你如何使用VF自带标签实现最基本的分页功能。

一、需求阐述

自定义页面,展示系统中所有accounts的名称、类型和电话,点击名称可以进入某个account的详细页面;并且在该自定义页面中,可以编辑和删除account。

二、逻辑梳理

这个需求里并没有复杂的逻辑,实现起来也非常简单,下面直接上代码>-<

三、代码实现

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. 效果
第一页长这样~
第二页~ 咦?出现了个Previous?

四、知识点回顾

1. ApexPages.StandardSetController

当为一个标准Controller定义拓展时,即可使用StandardSetController类。具体用法可参考官方文档:

此外,在ApexPages命名空间下,还有以下几个类比较常用:

2. PageReference

在之前的笔记中有过讲述,这里不赘述。→传送门

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用于获取一个对象动作的超链接。使用方法如下:

{!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有三个目的:获取s-control的URL;获取静态资源的URL;获取一个对象动作的URL。在本例中,URLFOR用于获取一个对象动作的URL。那么,一个对象可以有哪些动作呢?常见的有以下几个:

<!-- 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> 

关于URLFOR用于"获取s-control的URL"和"获取静态资源的URL"的用法请参考http://salesforcesource.blogspot.com/2008/12/urlfor-function-finally-explained.html(可能需要翻墙才能看)。

4. rendered

在文中有这样一段代码,笔者发现把rendered去掉之后,Next和Previous就不能像现在这样智能地显示了(即有前一页的时候才显示Previous,否则不显示,Next同理)。那rendered究竟有啥用呢?事实上,render用于显示/隐藏某个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>

经常会有人混淆render, reRerender和renderAs。下面我们就来辨析一下~

<apex:inputField id="Id1" value="{!Obj.Field1}" rendered="{!IF(Obj.fieldname = 'dk'|| Obj.fieldname = 'Dinesh' ,true,false)}"/>

五、总结

懒不得写了~~


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

上一篇下一篇

猜你喜欢

热点阅读