package com.ibm.abin.util;
import java.util.Random;
/**
* Title: Pager
* Description: 分页工具
*/
public class Pager {
//标准列表分页,用于后台
public static String getPagerNormal(int total, int pagesize, int pagenum,String pageurl) {
int count = total / pagesize;
if (total % pagesize > 0) {
count++;
}
if(pageurl.indexOf("?")>-1){
pageurl = pageurl + "&";
}else{
pageurl = pageurl + "?";
}
StringBuffer buf = new StringBuffer();
buf.append("共有"+total+"条记录 ");
buf.append(pagenum+"/"+ count +" ");
if (pagenum == 1) {
buf.append("<SPAN style='color:#CCCCCC'>【首页】</SPAN><SPAN style='color:#CCCCCC'>【上一页】</SPAN> ");
} else {
buf.append("【<a href='" + pageurl + "pagenum=1'>首页</a>】【<a href='" + pageurl + "pagenum=" + (pagenum - 1)
+ "' >上一页</a>】");
}
int bound1 = ((pagenum - 2) <= 0) ? 1 : (pagenum - 2);
int bound2 = ((pagenum + 2) >= count) ? count : (pagenum + 2);
for (int i = bound1; i <= bound2; i++) {
if (i == pagenum) {
buf.append("<SPAN style='color:#FF0000'>" + i
+ "</SPAN> ");
} else {
buf.append("<a href='" + pageurl + "pagenum=" + i + "'>" + i
+ "</a> ");
}
}
if (bound2 < count) {
buf.append("<SPAN>...</SPAN>");
}
if (pagenum == count||count==0) {
buf.append("<SPAN style='color:#CCCCCC'>【下一页】</SPAN><SPAN style='color:#CCCCCC'>【尾页】</SPAN>");
} else {
buf.append("【<a href='" + pageurl + "pagenum=" + (pagenum + 1)
+ "'>下一页</a>】【<a href='" + pageurl + "pagenum=" + count
+ "'>尾页</a>】");
}
return buf.toString();
}
//标准列表分页2,用于后台
public static String getPagerNormal2(int total, int pagesize, int pagenum,String pageurl) {
int count = total / pagesize;
if (total % pagesize > 0) {
count++;
}
if(pageurl.indexOf("?")>-1){
pageurl = pageurl + "&";
}else{
pageurl = pageurl + "?";
}
StringBuffer buf = new StringBuffer();
buf.append("共有"+total+"条记录 ");
buf.append(pagenum+"/"+ count +" ");
if (pagenum == 1) {
buf.append("<SPAN style='color:#CCCCCC'>【首页】</SPAN><SPAN style='color:#CCCCCC'>【上一页】</SPAN> ");
} else {
buf.append("【<a href='" + pageurl + "pagenum=1'>首页</a>】【<a href='" + pageurl + "pagenum=" + (pagenum - 1)
+ "' >上一页</a>】");
}
int bound1 = ((pagenum - 5) <= 0) ? 1 : (pagenum - 5);
int bound2 = ((pagenum + 5) >= count) ? count : (pagenum + 5);
for (int i = bound1; i <= bound2; i++) {
if (i == pagenum) {
buf.append("<SPAN style='color:#FF0000'>" + i
+ "</SPAN> ");
} else {
buf.append("<a href='" + pageurl + "pagenum=" + i + "'>" + i
+ "</a> ");
}
}
if (bound2 < count) {
buf.append("<SPAN>...</SPAN>");
}
if (pagenum == count||count==0) {
buf.append("<SPAN style='color:#CCCCCC'>【下一页】</SPAN><SPAN style='color:#CCCCCC'>【尾页】</SPAN>");
} else {
buf.append("【<a href='" + pageurl + "pagenum=" + (pagenum + 1)
+ "'>下一页</a>】【<a href='" + pageurl + "pagenum=" + count
+ "'>尾页</a>】");
buf.append("第"+pagenum+"/"+ count+"页 ");
buf.append("<form action='"+pageurl+"' method='post'><input name='pagenum' format='*N' size='3' maxlength='5' value='' emptyok='true' /><input type='submit' value='跳转' /></form>");
}
return buf.toString();
}
//简单列表分页,用于wap1.0前台
public static String getPagerSimple(int total, int pagesize, int pagenum,String pageurl) {
int count = total / pagesize;
if (total % pagesize > 0) {
count++;
}
if(pageurl.indexOf("?")>-1){
pageurl = pageurl + "&";
}else{
pageurl = pageurl + "?";
}
StringBuffer buf = new StringBuffer();
if(count>1){
if (pagenum == count) {
buf.append("[下页] ");
} else {
buf.append("[<a href='" + pageurl + "pn=" + (pagenum + 1)
+ "'>下页</a>] ");
}
if (pagenum == 1) {
buf.append("[上页] ");
} else {
buf.append("[<a href='" + pageurl + "pn=" + (pagenum - 1)
+ "'>上页</a>] ");
}
buf.append("第"+pagenum+"/"+ count+"页 ");
Random r = new Random();
int inputpage = r.nextInt(100);
buf.append("<input name='page"+inputpage+"' value='"+pagenum+"' emptyok='true' format='*N' size='3'/>[<a href='"+pageurl+"pn=$(page"+inputpage+")'>跳转</a>]");
}
return buf.toString();
}
//简单列表分页,用于wap2.0前台
public static String getPagerSimple2(int total, int pagesize, int pagenum,String pageurl) {
int count = total / pagesize;
if (total % pagesize > 0) {
count++;
}
String pageurl2 ="";
if(pageurl.indexOf("?")>-1){
pageurl2 = pageurl + "&";
}else{
pageurl2 = pageurl + "?";
}
StringBuffer buf = new StringBuffer();
if(count>1){
if (pagenum == count) {
buf.append("[下页] ");
} else {
buf.append("[<a href='" + pageurl2 + "pn=" + (pagenum + 1)
+ "'>下页</a>] ");
}
if (pagenum == 1) {
buf.append("[上页] ");
} else {
buf.append("[<a href='" + pageurl2 + "pn=" + (pagenum - 1)
+ "'>上页</a>] ");
}
buf.append("第"+pagenum+"/"+ count+"页 ");
buf.append("<form action='"+pageurl+"' method='post'><input name='pn' format='*N' size='3' maxlength='5' value='' emptyok='true' /><input type='submit' value='跳转' /></form>");
}
return buf.toString();
}
public static String getPagerSimple3(int total, int pagesize, int pagenum,String pageurl, String urlLable) {
int count = total / pagesize;
if (total % pagesize > 0) {
count++;
}
if(pageurl.indexOf("?")>-1){
pageurl = pageurl + "&";
}else{
pageurl = pageurl + "?";
}
StringBuffer buf = new StringBuffer();
if(count>1){
if (pagenum != count) {
buf.append("<a href='");
buf.append(pageurl);
buf.append("pn=");
buf.append(pagenum + 1);
buf.append("'>");
buf.append(urlLable);
buf.append("</a><br/> ");
}
}
return buf.toString();
}
//用于长文本分页
public static String getPagerText(int total, int pagesize, int pagenum,String pageurl) {
int count = total / pagesize;
if (total % pagesize > 0) {
count++;
}
if(pageurl.indexOf("?")>-1){
pageurl = pageurl + "&";
}else{
pageurl = pageurl + "?";
}
StringBuffer buf = new StringBuffer();
if(count>1){
buf.append("["+pagenum+"/"+count+"]");
if (pagenum < count-1) {
buf.append(" <a href='" + pageurl + "pn=" + (pagenum + 1)
+ "'>下页</a>|");
if (pagenum > 1) {
buf.append("<a href='" + pageurl + "pn=" + (pagenum - 1)
+ "'>上页|</a>");
}
buf.append("<a href='" + pageurl + "pn="+ (pagenum + 1)+"&y=0'>余下全文</a>");
}
if(pagenum==(count-1)){
if(pagenum==1){
buf.append("<a href='" + pageurl + "pn=" + (pagenum + 1)
+ "'>下页</a>");
}
else{
buf.append("<a href='" + pageurl + "pn=" + (pagenum + 1)
+ "'>下页|</a>");
}
if (pagenum > 1) {
buf.append("<a href='" + pageurl + "pn=" + (pagenum - 1)
+ "'>上页</a>");
}
}
if(pagenum==count){
buf.append("<a href='" + pageurl + "pn=" + (pagenum - 1)
+ "'>上页</a>");
}
buf.append("<br/>");
//for(int i=1;i<=count;i++){
// if(pagenum==i){
// buf.append("["+i+"]");
// }else{
// buf.append("[<a href='" + pageurl + "pn=" + i +"'>"+i+"</a>]");
// }
//}
}
return buf.toString();
}
}