本篇介绍通过使用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。

 public class CategoryWrapper {
public Boolean checked{ get; set; }
public GOODS__c goods { get; set;} public CategoryWrapper(){
goods = new GOODS__c();
checked = false;
} public CategoryWrapper(GOODS__c goods){
this.goods = goods;
checked = false;
}
}

CategoryWrapper类有两个变量,一个为Goods对象,用来获取商品基本信息,一个为布尔类型的checked,用来作为判断数据行是否被选的属性。

 public with sharing class PagingController {

     List<categoryWrapper> categories {get;set;}

     // instantiate the StandardSetController from a query locator
public ApexPages.StandardSetController con {
get {
if(con == null) {
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]));
// sets the number of records in each page set
con.setPageSize(20);
}
return con;
}
set;
} // returns a list of wrapper objects for the sObjects in the current page set
public List<categoryWrapper> getCategories() {
categories = new List<categoryWrapper>();
for (GOODS__c category1 : (List<GOODS__c>)con.getRecords())
categories.add(new CategoryWrapper(category1)); return categories;
} // displays the selected items
public PageReference process() {
for (CategoryWrapper cw : categories) {
if (cw.checked)
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,cw.goods.GOODSNAME__c));
}
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();
} }

使用StandardController控制页面显示的内容以及每页显示多少行数据,是否含有上一页下一页等等功能。通过PageReference作为当前页面的引用,控制页面数据。

 <apex:page controller="PagingController">
<apex:form >
<apex:pageBlock title="Goods"> <apex:pageBlockButtons location="top">
<apex:commandButton action="{!process}" value="Selected" />
<apex:commandButton action="{!cancel}" value="Cancel" />
</apex:pageBlockButtons>
<apex:pageMessages /> <apex:pageBlockSection title="Goods - Page {!pageNumber}" columns="1">
<apex:pageBlockTable value="{!categories}" var="c">
<apex:column width="25px">
<apex:inputCheckbox value="{!c.checked}" />
</apex:column>
<apex:column value="{!c.goods.GoodsName__c}" headerValue="Name" />
<apex:column value="{!c.goods.GoodsType__c}" headerValue="type" />
<apex:column value="{!c.goods.GoodsBrand__c}" headerValue="brand" />
<apex:column value="{!c.goods.GoodsPrice__c}" headerValue="Price" />
</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>

页面显示样式如下:

总结:本篇只是简单的实现数据分页功能,在真正项目中应该很少会有直接使用VF标签和使用Apex接口配合实现分页的(吐槽:自动忽略。。。因为VF的布局很丑),通常使用HTML的布局结合着Controller实现精美样式, 不过可以通过本篇的内容了解ApexPage命名空间里的类和VF页面的关系以及PageReference的用法和作用,如果内容有写的错误的地方欢迎批评指正,如果有问题,请留言。

salesforce 零基础开发入门学习(八)数据分页简单制作的更多相关文章

  1. 【转载】salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建

    salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建   VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的 ...

  2. salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建

    VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的标签相对简单,如果需要深入了解VF相关知识以及标签, 可以通过以下链接查看或下载 ...

  3. 【转载】salesforce 零基础开发入门学习(五)异步进程介绍与数据批处理Batchable

    salesforce 零基础开发入门学习(五)异步进程介绍与数据批处理Batchable   本篇知识参考:https://developer.salesforce.com/trailhead/for ...

  4. 【转载】salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解

    salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解   建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schem ...

  5. 【转载】salesforce 零基础开发入门学习(三)sObject简单介绍以及简单DML操作(SOQL)

    salesforce 零基础开发入门学习(三)sObject简单介绍以及简单DML操作(SOQL)   salesforce中对于数据库操作和JAVA等语言对于数据库操作是有一定区别的.salesfo ...

  6. 【转载】salesforce 零基础开发入门学习(二)变量基础知识,集合,表达式,流程控制语句

    salesforce 零基础开发入门学习(二)变量基础知识,集合,表达式,流程控制语句 salesforce如果简单的说可以大概分成两个部分:Apex,VisualForce Page. 其中Apex ...

  7. 【转载】salesforce 零基础开发入门学习(一)Salesforce功能介绍,IDE配置以及资源下载

    salesforce 零基础开发入门学习(一)Salesforce功能介绍,IDE配置以及资源下载   目前国内已经有很多公司做salesforce,但是国内相关的资料确是少之又少.上个月末跳槽去了新 ...

  8. salesforce 零基础开发入门学习(一)Salesforce功能介绍,IDE配置以及资源下载

    目前国内已经有很多公司做salesforce,但是国内相关的资料确是少之又少.上个月末跳槽去了新公司,主要做的就是salesforce,不过当时想要看一些相关资料确实比较难.为了避免想要零基础学习的人 ...

  9. salesforce 零基础开发入门学习(五)异步进程介绍与数据批处理Batchable

    本篇知识参考:https://developer.salesforce.com/trailhead/force_com_dev_intermediate/asynchronous_apex/async ...

随机推荐

  1. java的三元运算符

    1.三元运算符语法:判断表达式?表达式1:表达式2: (1)三元运算符适合于判断2个值到底使用哪一个! public static void mian(String[] args){ int sex= ...

  2. spring自动装配

    spring提供了自动装配(autowiring)和自动检测(autodiscovery)用来减少XML的配置数量. 自动装配bean属性 byName——把与Bean的属性具有相同名字(或ID)的其 ...

  3. HTTP1.1初识

    Http(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议. Http1.1版中,有一个持续连接(Persistent Connections)机制,其作为Http1.1中建立连接的 ...

  4. [UCSD白板题] The Last Digit of a Large Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  5. Learning ROS for Robotics Programming - Second Edition(《学习ROS机器人编程-第二版》)

    Learning ROS for Robotics Programming - Second Edition <学习ROS机器人编程-第二版> ----Your one-stop guid ...

  6. 使用Xcode6创建EmptyProject

    多年不写文章,今天突然准备好好写一些博客,以记录自己在编程这条道路上的成长,与所学所悟.提起笔来,才发现,自己的语言之匮乏,思虑再三,始觉不顺.也罢,从头开始慢慢训练吧. 自Xcode6更新之后,默认 ...

  7. 对json排序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. js 监听输入框输入事件兼容ie7

    $(element).bind("input propertychange",function(){});

  9. AngularJs 与Jquery的对比分析,超详细!

    闲来无事,在网上发现了一篇对比AngularJs和Jquery的文章.恰好最近自己也在研究AngularJs.特此收藏.需要的朋友可以参考. 原问题:假如我熟悉利用jQuery去开发客户端应用,那么我 ...

  10. UIView动画

    UIView动画 一.commitAnimations方式使用UIView动画 1.commitAnimations方式使用UIView动画 [UIView beginAnimations:@&quo ...