Salesforce简单分页查询
今天 , 我们根据前一天创建的狗对象来进行分页点击查看
首先来了解今天的主要知识 :
1. ApexPages
2. PageReference
- 首先 , 创建
DogWrapper
代码如下 :
public class DogWrapper {
public Boolean checked{ get; set; }
public Dog__c dog { get; set;}
public DogWrapper(){
dog = new Dog__c();
checked = false;
}
public DogWrapper(Dog__c dog){
this.dog = dog;
checked = false;
}
}
DogWrapper 类有两个变量 , 一个是 Dog 对象 , 用来获取狗狗的基本信息 , 一个为布尔类型的checked , 用来作为判断数据行是否被选的属性.
- 再创建一个
DogPageController
类
代码如下 :
public with sharing class DogPageController {
List<DogWrapper> categories {get;set;}
//从查询定位符实例化StandardSetController
public ApexPages.StandardSetController con {
get{
if (con == null) {
con = new ApexPages.StandardSetController(
Database.getQueryLocator([
SELECT DogBreed__c,DogName__c,DogSex__c From Dog__c limit 100
])
);
//设置每页中的记录数
con.setPageSize(2);
}
return con ;
}
set;
}
//返回当前页集中sObjects的包装器对象列表
public List<DogWrapper> getCategories() {
categories = new List<DogWrapper>();
for (Dog__c category1 : (List<Dog__c>)con.getRecords())
categories.add(new DogWrapper(category1));
return categories;
}
//显示所选项目
public PageReference process() {
for (DogWrapper cw : categories) {
if (cw.checked)
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,cw.dog.DOGNAME__c));
}
return null;
}
//指示在当前页设置之后是否有更多记录
public Boolean hasNext{
get{
return con.getHasNext();
}
set;
}
//指示当前页集之前是否有更多记录
public Boolean hasPrevious{
get{
return con.getHasPrevious();
}
set;
}
//返回当前页集的页码
public Integer pageNumber{
get{
return con.getPageNumber();
}
set;
}
//返回记录的第一页
public void first(){
con.first();
}
//返回记录的最后一页
public void last(){
con.last();
}
//返回前一页的记录
public void previous(){
con.previous();
}
//返回下一页的记录
public void next(){
con.next();
}
//返回原始页面(如果已知)或主页的PageReference
public void cancel(){
con.cancel();
}
}
每行代码都已备注.
- 通过
DogPageReference
作为当前页面的引用 , 控制页面数据
代码如下 :
<apex:page controller="DogPageController">
<apex:form >
<apex:pageBlock title="Dog">
<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.dog.DogName__C}" headerValue="Name" />
<apex:column value="{!c.dog.DogBreed__c}" headerValue="Breed" />
<apex:column value="{!c.dog.DogSex__c}" headerValue="Sex" />
</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>
代码写好之后 ,我们提交到Salesforce
页面显示如下 :
那么 , 分页的代码已经写好了 , 自己练一下吧
会了 , 就点击关注我哟💕