salesforce 零基础开发入门学习(八)数据分页简单制作
本篇介绍通过使用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 零基础开发入门学习(八)数据分页简单制作的更多相关文章
- 【转载】salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建
salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建 VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的 ...
- salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建
VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的标签相对简单,如果需要深入了解VF相关知识以及标签, 可以通过以下链接查看或下载 ...
- 【转载】salesforce 零基础开发入门学习(五)异步进程介绍与数据批处理Batchable
salesforce 零基础开发入门学习(五)异步进程介绍与数据批处理Batchable 本篇知识参考:https://developer.salesforce.com/trailhead/for ...
- 【转载】salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解
salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解 建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schem ...
- 【转载】salesforce 零基础开发入门学习(三)sObject简单介绍以及简单DML操作(SOQL)
salesforce 零基础开发入门学习(三)sObject简单介绍以及简单DML操作(SOQL) salesforce中对于数据库操作和JAVA等语言对于数据库操作是有一定区别的.salesfo ...
- 【转载】salesforce 零基础开发入门学习(二)变量基础知识,集合,表达式,流程控制语句
salesforce 零基础开发入门学习(二)变量基础知识,集合,表达式,流程控制语句 salesforce如果简单的说可以大概分成两个部分:Apex,VisualForce Page. 其中Apex ...
- 【转载】salesforce 零基础开发入门学习(一)Salesforce功能介绍,IDE配置以及资源下载
salesforce 零基础开发入门学习(一)Salesforce功能介绍,IDE配置以及资源下载 目前国内已经有很多公司做salesforce,但是国内相关的资料确是少之又少.上个月末跳槽去了新 ...
- salesforce 零基础开发入门学习(一)Salesforce功能介绍,IDE配置以及资源下载
目前国内已经有很多公司做salesforce,但是国内相关的资料确是少之又少.上个月末跳槽去了新公司,主要做的就是salesforce,不过当时想要看一些相关资料确实比较难.为了避免想要零基础学习的人 ...
- salesforce 零基础开发入门学习(五)异步进程介绍与数据批处理Batchable
本篇知识参考:https://developer.salesforce.com/trailhead/force_com_dev_intermediate/asynchronous_apex/async ...
随机推荐
- (转)Java中的static关键字解析
转自http://www.cnblogs.com/dolphin0520/p/3799052.html 一.static关键字的用途 在<Java编程思想>P86页有这样一段话: “sta ...
- 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings
谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...
- css study
---恢复内容开始--- html{filter:expression(document.execCommand("BackgroundImageCache",false,true ...
- 一个实现了View接口的Fragment
小程序并不新鲜,模式上先有百度轻应用,后有支付宝的各类小服务,再来还有腾讯自家QQ右下角的应用宝:技术上也就是FaceBook RN的那一套.一个技术上无创新,形式上无创意的事物,凭什么勾起了开发者们 ...
- sqlserver游标的使用方式
----臨時表 把數據抄寫到此表,然後做2個表的同步 SELECT [FA_NUMBER] ,[STATUS] ,[FA_REQUESTOR] ,[CALI_NUMBER] ,[AMT] FROM [ ...
- Java程序,取随机数的两种实现方法
1.随机数的第一种程序(取0-9的整型随机数) public class random_1 { public static void main(String[] args) { Random r=ne ...
- HTML5界面开发工具jQuery EasyUI更新至v1.3.5
本文转自:evget.com HTML5界面开发工具 jQuery EasyUI 最新发布v1.3.5,新版修复了多个bug,并改进了menu,tabs和slider等多个控件.jQuery Easy ...
- hibernate中保存一个对象后再设置此对象的属性为什么不需要调用update方法了
hibernate中保存一个对象后再设置此对象的属性为什么不需要调用update方法了 例如session.save(user);user.setAge(20); 原因: hibernate对象的三种 ...
- WPF 自定义TextBox,可控制键盘输入内容
非原创,整理之前的代码的时候找出来的,可用,与大家分享一下! public class NumbericBoxWithZero : NumericBox { public NumbericBoxWit ...
- EJB概念理解
转自http://blog.csdn.net/jojo52013145/article/details/5783677 1. 我们不禁要问,什么是"服务集群"?什么是"企 ...