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

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

一、需求阐述

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

二、逻辑梳理

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

三、代码实现

1. Controller
public with sharing class SepPageCtl2 {
    List 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 getAccounts(){
        return (List) 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

    
        
            
                
                    
                        编辑|
                        删除
                    
                    
                        {!a.Name}
                    
                    
                        
                            
                                {!a.Phone}
                            
                        
                    
                    
                        
                    
                
            
        

        
            First
            Previous
            Next
            Last
        
    

3. 效果
Apex:使用VF自带标签实现简单数据分页功能_第1张图片
第一页长这样~

Apex:使用VF自带标签实现简单数据分页功能_第2张图片
第二页~ 咦?出现了个Previous?

四、知识点回顾

1. ApexPages.StandardSetController

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

  • StandardSetController
  • StandardSetController Methods

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

  • ApexPages.Message: 使用此类将信息传递到前台显示,常用于显示异常信息(系统异常or自定义异常);
  • ApexPages.Action: 实现前后台交互。
2. PageReference

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

3. URLFOR

在文中有这样一段代码,用于给“编辑”和“删除”添加超链接。


    编辑|
    删除

其中,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。那么,一个对象可以有哪些动作呢?常见的有以下几个:

  • 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

New

View
Edit
Delete
Home

关于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 。

 
      First
      Previous
      Next
      Last

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

  • render:布尔值,默认为true。用于显示/隐藏某个block, output panel 或input/output fields 。
    我们也可以使用在render属性内使用IF条件,e.g.

  • reRerender:用于在服务器响应完成后刷新output panel, block或fields。例如,你有一个用于添加account和相应contact的页面,你希望每次操作后都能把最近添加的记录展示出来,那么就可以用reRerender来刷新。
  • renderAs:用于用HTML, pdf, excel等格式显示VF页面。
    for pdf – renderAs =”pdf”
    for HTML – renderAs =”html”
    for excel –

五、总结

懒不得写了~~


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

你可能感兴趣的:(Apex:使用VF自带标签实现简单数据分页功能)