網站中如何?分頁

來源:互聯網
上載者:User

標籤:clist   ber   turn   return   getc   count()   大小   http   article   

  最近做一個項目,要用到分頁功能,參考了很多方法,有的不合適,有的不喜歡,於是自己在他們的基礎上整了一個;思路是這樣的:先把需要分頁的資料或是記錄都查詢出來 存入一個集合類裡如List或是Vector,然後利用其sublist(int findex,int eindex)方法,擷取需要分頁的資料或記錄,關於sublist()函數就自己去查API了。下面貼下代碼吧,首先是將分頁所需的一些個東東抽象出一個javabean對象—PageBean:

 

[java] view plain copy 
  1. public class PageBean {  
  2.     private int curPage;             //當前頁  
  3.     private int pageCount;           //總頁數  
  4.     private int rowsCount;           //總行數  
  5.     private int pageSize=10;         //每頁多少行  
  6.       
  7.       
  8.       
  9.     public PageBean(int rows){  
  10.           
  11.         this.setRowsCount(rows);  
  12.         if(this.rowsCount % this.pageSize == 0){  
  13.             this.pageCount=this.rowsCount / this.pageSize;  
  14.         }  
  15.         else if(rows<this.pageSize){  
  16.             this.pageCount=1;  
  17.         }  
  18.         else{  
  19.             this.pageCount=this.rowsCount / this.pageSize +1;  
  20.         }  
  21.     }  
  22.       
  23.       
  24.     public int getCurPage() {  
  25.         return curPage;  
  26.     }  
  27.     public void setCurPage(int curPage) {  
  28.         this.curPage = curPage;  
  29.     }  
  30.     public int getPageCount() {  
  31.         return pageCount;  
  32.     }  
  33.     public void setPageCount(int pageCount) {  
  34.         this.pageCount = pageCount;  
  35.     }  
  36.     public int getPageSize() {  
  37.         return pageSize;  
  38.     }  
  39.     public void setPageSize(int pageSize) {  
  40.         this.pageSize = pageSize;  
  41.     }  
  42.     public int getRowsCount() {  
  43.         return rowsCount;  
  44.     }  
  45.     public void setRowsCount(int rowsCount) {  
  46.         this.rowsCount = rowsCount;  
  47.     }  
  48. }  

 

分頁顯示:

 

[java] view plain copy 
  1. List clist=adminbiz.queryNotFullCourse();//將查詢結果存放在List集合裡  
  2. PageBean pagebean=new PageBean(clist.size());//初始化PageBean對象  
  3. //設定當前頁  
  4. pagebean.setCurPage(page); //這裡page是從頁面上擷取的一個參數,代表頁數  
  5. //獲得分頁大小  
  6. int pagesize=pagebean.getPageSize();  
  7. //獲得分頁資料在list集合中的索引  
  8. int firstIndex=(page-1)*pagesize;  
  9. int toIndex=page*pagesize;  
  10. if(toIndex>clist.size()){  
  11.     toIndex=clist.size();  
  12. }  
  13. if(firstIndex>toIndex){  
  14.     firstIndex=0;  
  15.     pagebean.setCurPage(1);  
  16. }  
  17. //截取資料集合,獲得分頁資料  
  18. List courseList=clist.subList(firstIndex, toIndex);  

 

網站中如何?分頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.