java 分页 代码

package common.util;

import java.util.*;

public class PageController implements IPageModel {
  private Collection model;

  //数据总行数
  private int totalRowCount = 0; //

  //总页数
  private int pageCount = 0;

  //每页应显示的行数
  private int maxPageRowCount = 0;

  //当前页行数
  private int currPageRowCount = 0;

  //当前页号
  private int currPageNum;

  //默认构造
  public PageController() {
    super();
  }

  //传入模型
  public PageController(Collection model) {
    setPageController(model);
  }

  //设一个分页模型
  public void setPageController(Collection model) {
    this.model = model;
    this.totalRowCount = model.size();
  }
  /**
   * 总页数
   * @return int
   */
  public int getPageCount() {
    return this.pageCount;
  }

  /**
   * getPageContents
   *
   * @param intPageNum int
   * @return Object
   */
  public Object getPageContents(int intPageNum) {
    //非法数据
    if(intPageNum<1){
      intPageNum=1;
    }
    if(intPageNum>pageCount){
      intPageNum=pageCount;
    }
    //指定当前页
    this.currPageNum=intPageNum;
    int i = 0;
    ArrayList arr = new ArrayList();
    //如果是合法的范围
    if (intPageNum > 0 && intPageNum <= pageCount) {
      //计算该页的开始号和结束号
      int lfromrow = (intPageNum - 1) * maxPageRowCount;
      arr = (ArrayList) getElementsAt(model, lfromrow, lfromrow + maxPageRowCount-1);
    }
   currPageNum=intPageNum;
    return arr;
  }

  public Object getLastPage() {
    return this.getPageContents(pageCount);
  }

  public Object getFirstPage() {
    return this.getPageContents(0);
  }

  /**
   * getCurrentPageRowsCount
   *
   * @return int
   */
  public int getCurrentPageRowsCount() {
    if(currPageNum<pageCount){
      return maxPageRowCount;
    }
    else{//最后一页
      return totalRowCount-(pageCount-1)*maxPageRowCount;
    }
  }
  public int getCurrentPageNum(){
    return currPageNum;
  }
  /**
   * setMaxPageRows
   *
   * @return int
   */
  public void setMaxPageRows(int rowCount) {
    maxPageRowCount = rowCount;
    //计算总页数
    if (totalRowCount % maxPageRowCount > 0) { //有余数
      pageCount = totalRowCount / maxPageRowCount + 1;
    }
    else {
      pageCount = totalRowCount / maxPageRowCount;
    }
  }

  /**
   * getMaxPageRows
   */
  public int getMaxPageRows() {
    return maxPageRowCount;
  }
  //私有方法,返回集合中指定范围的数据
  private Object getElementsAt(Collection model, int fromIndex, int toIndex) {
    Iterator iter = model.iterator();
    ArrayList arr = new ArrayList();
    if (iter != null) {
      int i = 0;
      while (iter.hasNext()) {
        Object obj=iter.next();
        if (i >= fromIndex && i <= toIndex) {
          arr.add(obj);
        }
        if (i > toIndex) {
          break;
        }
         i = i + 1;
      }
    }
    return arr;
  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值