分页的实现总体上分真分页和假分页。

所谓真分页指页面上列出来的数据就是实际查询的数据,假分页则是无论页面上一次显示多少条记录,实际上后台已经加载了所有的记录,分页只是为了展示给用户查看。今天分享一个Visualforce页面的真分页的实现

Apex 类:OppPageController

 /*******
*
* @作者:Ricardo
* @Time:2018-06-05
* @function:业务机会的分页列表展示
*
*/
public with sharing class OppPageController { //分页参数
public Integer counter=0; //偏移量
public static Integer LIST_SIZE = 10;//每页显示记录数
public Integer total_size; //总记录数 public OppPageController () {
total_size = [select count() from Opportunity];
} //变量 Opportunitys 的get方法
public List<Opportunity> getOpportunitys() {
try { List<Opportunity> Opportunitys= [select Id,Name,StageName,Account.Name,Type,Probability from Opportunity limit :LIST_SIZE offset :counter];// limit x,y return Opportunitys;
} catch (Exception e) {
ApexPages.addMessages(e);
return null;
}
} //变量 DisablePrevious 的get方法
//控制上一页按钮是否可点击
public Boolean getDisablePrevious() {
if (counter>0)
return false;
else
return true;
} //变量 DisableNext 的get方法
//控制下一页按钮是否可点击
public Boolean getDisableNext() {
if (counter + LIST_SIZE < total_size)
return false;
else
return true;
} //变量 Total_size 的get方法
//返回Total_size的值
public Integer getTotal_size() {
return total_size;
} //变量 PageNumber 的get方法
//计算当前页码
public Integer getPageNumber() {
return counter/LIST_SIZE + 1;
} //变量 TotalPages 的get方法
//计算总页数
public Integer getTotalPages() {
if (math.mod(total_size, LIST_SIZE ) > 0) {
return total_size/LIST_SIZE + 1;
} else {
return (total_size/LIST_SIZE ) ;
}
} //首页
public PageReference First() {
counter = 0;
return null;
} //上一页
public PageReference Previous() {
counter -= LIST_SIZE ;
return null;
} //下一页
public PageReference Next() {
counter += LIST_SIZE ;
return null;
} //尾页
public PageReference End() {
counter = total_size - math.mod(total_size, LIST_SIZE ) ;
return null;
}
}

Visualforce 页面

 <apex:page controller="OppPageController" showHeader="false">
<style type="text/css">
/* 控制footer居右显示 */
.footer{
text-align: right;
}
</style>
<apex:sectionHeader subtitle="业务机会分页显示列表" title="业务机会"/>
<apex:form >
<apex:pageBlock >
<!-- 显示错误异常信息 -->
<apex:pageMessages id="message"/>
<apex:pageBlockButtons location="bottom" style="text-align: center;">
<!-- 按钮显示效果 -->
<apex:outputPanel id="buttons">
<apex:commandButton action="{!First}" title="First" value="首页" disabled="{!disablePrevious}" reRender="showpanel,buttons"/>
<apex:commandButton action="{!Previous}" title="Previous" value="上一页" disabled="{!disablePrevious}" reRender="showpanel,buttons"/>
<apex:commandButton action="{!Next}" title="Next" value="下一页" disabled="{!disableNext}" reRender="showpanel,buttons"/>
<apex:commandButton action="{!End}" title="End" value="尾页" disabled="{!disableNext}" reRender="showpanel,buttons"/>
</apex:outputPanel>
</apex:pageBlockButtons> <apex:outputPanel id="showpanel">
<apex:pageMessages id="theMessages" />
<apex:pageBlockTable value="{!Opportunitys}" var="opp" footerClass="footer">
<apex:column value="{!opp.Name}" />
<apex:column value="{!opp.StageName}" />
<apex:column value="{!opp.Account.Name}" />
<apex:column value="{!opp.Type}" />
<apex:column value="{!opp.Probability}" />
<apex:facet name="footer">第{!pageNumber}/{!totalPages}页 共计{!total_size}条</apex:facet>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>

完成后的页面效果图

可以看到完成后的页面,是比较符合Salesforce原生态样式的

OppPageController中的代码结构比较简单,主要是根据偏移量,查询每次需要展示的记录数据,并刷新页面显示,以达到页面分页显示的效果,也就是说,这是一种真分页的实现。

本文仅供参考,如有错漏之处欢迎指正,如有疑问,欢迎评论区留言

最新文章

  1. Android 扫描条形码(Zxing插件)
  2. Python3.5 day3作业二:修改haproxy配置文件。
  3. MongoDB常用操作--数据库
  4. System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)
  5. SQL基础学习篇--字符函数
  6. QT5删除隐藏目录+隐藏文件(使用Process::start函数调用系统命令,且等待到结束)
  7. 使用Genymotion作Android开发模拟器:安装Genymotion、部署Genymotion Vitrue Device、安装Genymotion eclipse插件
  8. hdu 4465 Candy(二次项概率)
  9. 熟练掌握HDFS的Java API接口访问
  10. Sql 字符串操作类COALESCE
  11. ThinkPHP - 连贯操作
  12. LINUX下C语言编程调用函数、链接头文件以及库文件
  13. Java开发笔记(五)数值变量的类型
  14. Mariadb修改root密码
  15. 使用xmanager接收图形界面
  16. [教程]-三种空格unicode(\u00A0,\u0020,\u3000)表示的区别
  17. Nginx(六):Nginx HTTP负载均衡和反向代理的配置与优化
  18. J2SE 8的反射
  19. 插入数据insert语句中出错:没有与这些操作数匹配的“+”运算符,操作数类型为:const char[ ]+CString
  20. fedora 系统安装后常用设置

热门文章

  1. 深入学习OpenCV中图像灰度化原理,图像相似度的算法
  2. Kafka学习笔记之Kafka背景及架构介绍
  3. AOP中获取自定义注解的参数值
  4. Java基础笔记之String相关知识
  5. DataGridView 行数据验证:当输入数据无效时不出现红色感叹号的Bug
  6. codeforces #578(Div.2)
  7. js对象常用属性和方法:复制一个对象,获取一个对象的所有key和所有value的方法
  8. Excel单元格锁定及解锁
  9. svn忽略target文件
  10. webUploader大文件断点续传学习心得 多文件