分页技术在Web应用中是必不可少的,同时它也是面试必考的一道题目。因此,对于程序员来说分页技术尤为显得重要。下面我们就分析一下Google的分页技术。
Google分页的索引值计算方法
分析:以下是以显示6个页为参考(Google与百度均是20页)
1 2 3 当前页是1 1 2 3
1 2 3 4 当前页是2 起始值1 未页 4
1 2 3 4 5 当前页是3 起始值1 未页 5
1 2 3 4 5 6 当前页是4 起始值1 未页 6
从以上得知:当前页小于等于4时(nowPage),起始页为1(startIndex), 末页(endIndex)=当前页(nowPage)+2
startIndex=1;
endIndex=nowpage+2;
注意:如果求和值后的endIndex 如果endIndex大于总页数,那么this.endIndex=this.countPage;
java的实现代码如下:
if (this.nowPage <= 4) { //判断小于等于4的情况
this.startIndex = 1;
this.endIndex = this.nowPage + 2;
if(this.endIndex>this.countPage){
this.endindex=this.countpage;
}
}
当大于4时
2 3 4 5 6 7 当前页是5 起始值是2 未页 7
3 4 5 6 7 8 当前页是6 起始值是3 未页 8
4 5 6 7 8 9 当前页是7 起始值是4 未页 9
5 6 7 8 9 10 当前页是8 起始值是5 未页 10
从上图得知 当前页大于4时:
1、当前页与尾页的差少于2,开始页与当前页差3
endindex=nowpage+2;
startindex=nowapge-3;
注意:求和值后的endindex,如果endindex大于总页数
this.endindex=this.countpage;
this.startindex=this.countpage-5;
Java实现代码如下:
if(this.nowpage>4){ //大于4的情况
this.startindex=this.nowpage-3;
this.endindex=this.nowpage+2;
if(this.endindex>this.countpage){
this.endindex=this.countpage;
this.startindex=this.countpage-5;
}
}
整合后的代码如下:
// 计算索引位置
if (this.nowpage <= 4) { //判断小于等于4的情况
this.startindex = 1;
this.endindex = this.nowpage + 2;
if(this.endindex>this.countpage){
this.endindex=this.countpage;
}
}else if(this.nowpage>4){ //大于4的情况
this.startindex=this.nowpage-3;
this.endindex=this.nowpage+2;
if(this.endindex>this.countpage){
if(this.endindex>sumindex){
this.endindex=this.countpage;
this.startindex=this.countpage-5;
else{
this.startindex=1;
this.endindex=this.countpage;
}
}
}
下面我们将它封装在一个Java类中,为以后的使用提供便利,最重要的是它具有通用性。完整的代码如下:
package cn.csdn.util;
import java.util.List;
public class Pagination<T> {
private int nowPage; // 当前页
private int totalPage;
private int totalRecord;
public static final int PAGESIZE = 8;
private final int SHOWPAGE = 6;
private int startPage;
private int endPage;
private List<T> entity;
public Pagination() {
// TODO Auto-generated constructor stub
}
public Pagination(int nowPage, int totalRecord) {
this.nowPage = nowPage;
this.totalRecord = totalRecord;
this.totalPage = this.totalRecord % this.PAGESIZE == 0 ? this.totalRecord
/ this.PAGESIZE
: this.totalRecord / this.PAGESIZE + 1;
// 总页数是否小于4
if (this.totalPage < (this.SHOWPAGE / 2 + 1)) {
this.startPage = 1;
this.endPage = this.totalPage;
} else {
// 总页数大于4的情况
// 当前页的值是否小于等于4
if (this.nowPage <= (this.SHOWPAGE / 2 + 1)) {
this.startPage = 1;
this.endPage = this.nowPage + 2;
if (this.endPage >= this.totalPage) {
this.endPage = this.totalPage;
}
} else {
this.startPage = this.nowPage - 3;
this.endPage = this.nowPage + 2;
if (this.endPage >= this.totalPage) {
this.endPage = this.totalPage;
if (this.totalPage < this.SHOWPAGE) {
this.startPage = 1;
} else {
this.startPage = this.endPage - 5;
}
}
}
}
}
public int getNowPage() {
return nowPage;
}
public void setNowPage(int nowPage) {
this.nowPage = nowPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public int getStartPage() {
return startPage;
}
public void setStartPage(int startPage) {
this.startPage = startPage;
}
public int getEndPage() {
return endPage;
}
public void setEndPage(int endPage) {
this.endPage = endPage;
}
public List<T> getEntity() {
return entity;
}
public void setEntity(List<T> entity) {
this.entity = entity;
}
}
看到这相信你对分页技术一定更为熟识,使用起来也更加的得心应手。只要创建Pagination的对象即可把它应用到你的项目中去。最后要感谢CSDN陈红军老师的指导。