package com.xindeco.common;
import java.util.* ;
public class Pager {
private static int MAX_PAGE_INDEX = 10; //页脚显示多少页
private static String HEADER = "Result page";
public static String generate(int offset, int length, int size, String url) {
if (length > size) {
String pref;
if (url.indexOf("?") > -1) {
pref = "&";
} else {
pref = "?";
}
String header = "<font face='Helvetica' size='-1'>"+HEADER+": ";
if (offset > 0) {
header += "&<a href=\""+url+pref+"pager.offset="+(offset-size)+"\">[<< Prev]</a>\n";
}
int start;
int radius = MAX_PAGE_INDEX/2*size;
if (offset < radius) {
start = 0;
} else if(offset < length-radius) {
start = offset - radius;
} else {
start = (length/size-MAX_PAGE_INDEX)*size;
}
for(int i=start;i<length && i < start + MAX_PAGE_INDEX*size;i+=size) {
if (i == offset) {
header += "<b>"+(i/size+1)+"</b>\n";
} else {
header += "&<a href=\""+url+pref+"pager.offset="+i+"\">"+(i/size+1)+"</a>\n";
}
}
if(offset < length - size) {
header += "&<a href=\""+url+pref+"pager.offset="+((int)offset+(int)size)+"\">[Next >>]</a>\n";
}
header += "</font>";
return header;
} else {
return "";
}
}
}
import java.util.* ;
public class Pager {
private static int MAX_PAGE_INDEX = 10; //页脚显示多少页
private static String HEADER = "Result page";
public static String generate(int offset, int length, int size, String url) {
if (length > size) {
String pref;
if (url.indexOf("?") > -1) {
pref = "&";
} else {
pref = "?";
}
String header = "<font face='Helvetica' size='-1'>"+HEADER+": ";
if (offset > 0) {
header += "&<a href=\""+url+pref+"pager.offset="+(offset-size)+"\">[<< Prev]</a>\n";
}
int start;
int radius = MAX_PAGE_INDEX/2*size;
if (offset < radius) {
start = 0;
} else if(offset < length-radius) {
start = offset - radius;
} else {
start = (length/size-MAX_PAGE_INDEX)*size;
}
for(int i=start;i<length && i < start + MAX_PAGE_INDEX*size;i+=size) {
if (i == offset) {
header += "<b>"+(i/size+1)+"</b>\n";
} else {
header += "&<a href=\""+url+pref+"pager.offset="+i+"\">"+(i/size+1)+"</a>\n";
}
}
if(offset < length - size) {
header += "&<a href=\""+url+pref+"pager.offset="+((int)offset+(int)size)+"\">[Next >>]</a>\n";
}
header += "</font>";
return header;
} else {
return "";
}
}
}