分页

来源:互联网 发布:java毕业论文开题报告 编辑:程序博客网 时间:2024/06/09 23:59
  1. package org.grey.utils;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.List;  
  6. @SuppressWarnings({"unchecked","unused"})  
  7. public class Page {  
  8.     private int pageNo=1//当前页  
  9.     private int pageSize = 10//每页大小  
  10.     private int totalPages;  //总页  
  11.     private int totalRows; //总行数  
  12.     private List list = new ArrayList(0);  //存放数据  
  13.     //翻页条 示例 1 2 3 4 5   
  14.     private int barNumbers = 5//几个为一条,默认为5  
  15.     private List listNumbers = new ArrayList(); //数字翻页条  
  16.     public Page(int pageNo, int pageSize, int totalPages, List list) {  
  17.         this.pageNo = pageNo;  
  18.         this.pageSize = pageSize;  
  19.         this.totalPages = totalPages;  
  20.         this.list = list;  
  21.     }  
  22.       
  23.     public int getPageNo() {  
  24.         return pageNo>this.getTotalPages() ? this.getTotalPages() : (pageNo ==0 ? 1 : pageNo);  
  25.     }  
  26.     public void setPageNo(int pageNo) {  
  27.         this.pageNo = pageNo;  
  28.     }  
  29.     public int getPageSize() {  
  30.         return pageSize;  
  31.     }  
  32.     public void setPageSize(int pageSize) {  
  33.         this.pageSize = pageSize;  
  34.     }  
  35.     public int getTotalPages() {  
  36.         totalPages = this.getTotalRows()%this.getPageSize()==0 ? this.getTotalRows()/this.getPageSize() : (this.getTotalRows()/this.getPageSize())+1;  
  37.         return totalPages;  
  38.     }  
  39.     public void setTotalPages(int totalPages) {  
  40.         this.totalPages = totalPages;  
  41.     }  
  42.     public int getTotalRows() {  
  43.         return totalRows;  
  44.     }  
  45.     public void setTotalRows(int totalRows) {  
  46.         this.totalRows = totalRows;  
  47.     }  
  48.       
  49.     public List getList() {  
  50.         return list;  
  51.     }  
  52.     public void setList(List list) {  
  53.         this.list = list;  
  54.     }  
  55.     public int getBarNumbers() {  
  56.         return barNumbers;  
  57.     }  
  58.     public void setBarNumbers(int barNumbers) {  
  59.         this.barNumbers = barNumbers;  
  60.     }  
  61.     //分页条 如:1 2 3 4 5 6   
  62.     public List getListNumbers() {  
  63.         List listn = new ArrayList();  
  64.         //总显示条数  
  65.         int totbars = this.getTotalPages()%this.getBarNumbers()==0 ? this.getTotalPages()/this.getBarNumbers() : this.getTotalPages()/this.getBarNumbers()+1;  
  66.         //当前显示条数  
  67.         int currentBar = this.getPageNo()%this.getBarNumbers()==0 ? this.getPageNo() /this.getBarNumbers() : this.getPageNo() /this.getBarNumbers()+1;  
  68.         if(this.getPageNo()==this.getBarNumbers()*currentBar && currentBar != totbars) {  
  69.             currentBar++;  
  70.         }  
  71.         if(this.getPageNo()==(this.getBarNumbers()*currentBar-this.getBarNumbers()+1) && currentBar >1) {  
  72.             currentBar--;  
  73.         }  
  74.         for(int i=this.getBarNumbers()*currentBar; i>(currentBar==1 ? 0 : this.getBarNumbers()*(currentBar-1)); i--) {  
  75.             if(i<=this.getTotalPages()) {  
  76.                 listn.add(i);  
  77.             }  
  78.         }  
  79.         Collections.sort(listn);  
  80.         return listn;  
  81.     }  
  82.       
  83. //  public static void main(String[] args) {  
  84. //      Page p = new Page();  
  85. //      p.setTotalRows(120);  
  86. //      //p.setBarNumbers(6);  
  87. //      p.setPageNo(13);  
  88. //      System.out.println(p.getTotalPages());  
  89. //      System.out.println(p.getListNumbers());  
  90. //  }  
  91. }
0 0
原创粉丝点击