憨厚生

----Java's Slave----
***Java's Host***

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  165 随笔 :: 17 文章 :: 90 评论 :: 0 Trackbacks

在工作中需要一个翻页效果,所以写下了如下方法。实现了常用的翻页效果,但代码还没有进行优化(缺点:不适合数据量大的情况,因为所有的记录存在一个Vector变量中)贴出来供需要着参考一下!
import java.util.*;

public class Pages {
 int allRows;//所有的行数
 int currRow;//当前是第几行
 int onePageRows;//一页显示多少行
 int allPages;//所有的页数
 int currPage= 1;
 boolean firstPage= false;
 boolean lastPage= false;

 public Pages(int onePageRows) {
  this.onePageRows= onePageRows;
 }
/*
   vector1 为所有的记录
*/

 public Vector downPage(Vector vector1, int currPage) {
  Vector vector= new Vector();
  this.currPage= currPage;
  this.allRows= vector1.size();
  this.currRow= currPage * (this.onePageRows);
  int j= this.allRows % this.onePageRows;
  this.allPages= (this.allRows / this.onePageRows) + (j == 0 ? 0 : 1);
  this.currPage++;
  if (this.currPage < this.allPages) {
   this.lastPage= true;
  } else {
   this.lastPage= false;
  }
  this.firstPage= true;
  int i= 0;
  for (i= 0; i < this.onePageRows && currPage * this.onePageRows + i < this.allRows; i++) {
   vector.add(i, vector1.get(currRow + i));
  }
  this.currRow= this.currRow + i;
  return vector;
 }

 public Vector upPage(Vector vector1, int currPage) {
  Vector vector= new Vector();
  this.currPage= currPage;
  this.allRows= vector1.size();
  this.currRow= (currPage - 2) * (this.onePageRows);
  int j= this.allRows % this.onePageRows;
  this.allPages= (this.allRows / this.onePageRows) + (j == 0 ? 0 : 1);
  this.currPage--;
  if (this.currPage > 1) {
   this.firstPage= true;
  } else {
   this.firstPage= false;
  }
  this.lastPage= true;
  for (int i= 0; i < this.onePageRows; i++) {
   vector.add(i, vector1.get(this.currRow + i));
  }
  return vector;
 }
 public Vector firstPage(Vector vector1) {
  Vector vector= new Vector();
  this.allRows= vector1.size();
  this.currRow= this.onePageRows;
  int j= this.allRows % this.onePageRows;
  this.allPages= (this.allRows / this.onePageRows) + (j == 0 ? 0 : 1);
  this.firstPage= false;
  if (allPages > 1)
   this.lastPage= true;
  for (int i= 0; i < this.onePageRows && i < vector1.size(); i++) {
   vector.add(i, vector1.get(i));
  }
  return vector;
 }
 public Vector lastPage(Vector vector1) {
  Vector vector= new Vector();
  this.allRows= vector1.size();
  int j= this.allRows % this.onePageRows;
  this.allPages= (this.allRows / this.onePageRows) + (j == 0 ? 0 : 1);
  this.currPage= this.allPages;
  this.lastPage= false;
  if (allPages > 1)
   this.firstPage= true;
  int m= j == 0 ? this.onePageRows : this.allRows - (this.allPages - 1) * this.onePageRows;
  int n= m;
  for (int i= 0; i < m; i++) {
   vector.add(i, vector1.get(this.allRows - n));
   n--;
  }
  return vector;
 }

 public String getToolBar() {
  String form= "<form  id=\"form\" method=\"post\">";
  String table1= "<table align=\"center\" width=\"70%\"><tr><td>";
  String button1= "<input type=\"button\" value=\"首页\" onClick=\"dealPage();\" id=\"firstPage\" />&nbsp;";
  String disabled= "disabled=";
  if (!this.firstPage)
   disabled= disabled + "disabled";
  else
   disabled= "";
  String button2= "<input type=\"button\" value=\"上页\" id=\"upPage\" onClick=\"dealPage();\" " + disabled + " />&nbsp;";
  if (!this.lastPage)
   disabled= disabled + "disabled";
  else
   disabled= "";
  String button3= "<input type=\"button\" value=\"下页\" id=\"downPage\" onClick=\"dealPage();\" " + disabled + " />&nbsp;";
  String button4= "<input type=\"button\" value=\"尾页\"  id=\"lastPage\" onClick=\"dealPage();\"/>";
  String table2= "</td></tr><tr><td align=\"center\">";
  String table3= "共" + this.allRows + "行/第" + this.currPage + "页/共" + this.allPages + "页</td></tr></table></form>";
  String result= form + table1 + button1 + button2 + button3 + button4 + table2 + table3;
  return result;
 }
 
 public String getJavaScript() {
  StringBuffer result= new StringBuffer();
  if (this.getAllRows() > this.getOnePageRows()) {
   result.append("<SCRIPT language=JavaScript>");
   result.append("function dealPage(){");
   result.append("document.all.form.action=\"/mydomain/javaoa/selectqh.do?method=\"+event.srcElement.id+\"&currPage=");
   result.append(this.getCurrPage() + "&onePageRows=" + this.getOnePageRows() + "\";");
   result.append("document.all.form.submit();");
   result.append("}");
   result.append("</SCRIPT>");
  }
  return result.toString();
 }
 // public static void main(String[] args) {
 //  Pages page= new Pages(5);
 //  Vector vector= new Vector();
 //  Vector vector1= new Vector();
 //  for (int i= 0; i < 7; i++) {
 //   vector.add(i, new String(new Integer(i).toString()));
 //  }
 //  vector1= page.upPage(vector, 2);
 //  String s= page.getJavaScript();
 //  System.out.println("==" + s);
 //     for (int i= 0; i < vector1.size(); i++) {
 //      System.out.println(i + "=" + vector1.get(i));
 //     }
 //     System.out.println("allRows=" + page.allRows);
 //     System.out.println("currRow=" + page.currRow);
 //     System.out.println("onePageRows=" + page.onePageRows);
 //     System.out.println("allPages=" + page.allPages);
 //     System.out.println("currPage=" + page.currPage);
 //     System.out.println("firstPage=" + page.firstPage);
 //     System.out.println("lastPage=" + page.lastPage);
 // }
 /**
  * @return
  */
 public int getAllPages() {
  return allPages;
 }

 /**
  * @return
  */
 public int getAllRows() {
  return allRows;
 }

 /**
  * @return
  */
 public int getCurrPage() {
  return currPage;
 }

 /**
  * @return
  */
 public int getCurrRow() {
  return currRow;
 }

 /**
  * @return
  */
 public boolean isFirstPage() {
  return firstPage;
 }

 /**
  * @return
  */
 public boolean isLastPage() {
  return lastPage;
 }

 /**
  * @return
  */
 public int getOnePageRows() {
  return onePageRows;
 }

}

posted on 2007-03-03 16:45 二胡 阅读(229) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: