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 ...
随机推荐
- C#实现按键精灵的'找图' '找色' '找字'的功能
背景:游戏辅助功能通常使用按键精灵编写脚本,按键精灵的最大卖点就是能够找到画面中字,图,色,这对于模拟用户鼠标操作至关重要,这能找到道具,找到血量,实现自动打怪,自动补血,自动买卖道具,博主闲来无聊, ...
- javaWeb开发小工具---MailUtils及其单元测试
本次介绍的是,在javaWeb开发中,我们不免会遇到发送邮件的需求,比如:用户注册账号,需要激活登录,以及服务器定期向会员发送礼品信息等.所以参考有关资料,写了这个MailUtils工具类. 1.Ma ...
- Centos7安装完毕后重启提示Initial setup of CentOS Linux 7 (core)的解决方法
问题: CentOS7安装完毕,重新开机启动后显示: Initial setup of CentOS Linux 7 (core) 1) [x] Creat user 2) [!] License i ...
- 总有一项适合你:联想 Miix2 8寸版触摸屏失灵的各项解决方案
今天试着自己拆开后盖重新拆了一下排线,果然这个方法才是王道.在搜索攻略的时候看到了下面的帖子,觉得总结的不错,特此转载过来: 白色石头 2015-05-22 10:07● 使用评测 总有一 ...
- JQuery FullCalendar(二)
前言:根据前文介绍,我们对JQuery FullCalendar如何从后台取数据有了初步了解,已经实现最基本的要求.下面介绍一下FullCalendar的事件 $('#calendar').fullC ...
- C# 直接调用vs 委托vs动态调用vs动态类型vs反射,最佳性能测试
懒得解释,自己看代码 测试结果: Direct call:00:00:00.0742191Delegate Direct:00:00:00.0687487Method Factory(IL):00:0 ...
- 非对称加密RSA的应用及在C#中的实现
quote: http://www.cnblogs.com/happinessCodes/archive/2010/07/27/1786404.html 一说到数据的加密,常常会涉及到这几个单词: ...
- 在VS2010配置MPI--win7下64位系统
配置MPI经历了不少波折,把这些经历记录下来,告诫后来人. 1.版本要对 下载MPI,去官方网站 http://www.mpich.org/downloads/ 选择x86-64版本 2.步骤要对 1 ...
- selenium 切换窗口 每次成功code
最近用了网络上别人的一段切换窗口的code每次成功了,不错,学习 // 根据Title切换新窗口 public boolean switchToWindow_Title(WebDriver drive ...
- xxxx年度员工岗位技能调查表
昨天应公司要求设计了一张 <员工岗位技能调查表>,写微博有2个目的:第一是供大家参考,第二是记录下从事质量管理工作走过的点点滴滴.这是我第一次写工作方面的博客,之后会坚持写下去的. --- ...