網上流傳的一些java生成分頁導航工具都是有那麼些bug,抽空自己寫了個,如果發現問題請聯繫我;
順便利用這種思路寫了個java分頁導航生成工具
看看效果圖
/**
* 分頁導航生成工具類
*
* @author shadow
*
*/
public class NavUtils {
private static final int show_ = 10;
private static final int sep_ = show_ / 2;
private static final int split_ = sep_ / 2;
public static String build(int offset, int limit, int total) {
int count = total % limit == 0 ? total / limit : total / limit + 1;
StringBuffer buffer = new StringBuffer();
buffer.append("<span class='count_result'>共 " + count + " 頁 " + total + " 條記錄 </span>");
// 判斷是否顯示上頁
if (offset > 1) {
int prev = offset - 1;
buffer.append(getNormalPart("上頁", prev, limit));
}
// 頁數不超過限制的情況
if (count <= show_)
buffer.append(getPart(1, count, offset, limit));
// 頁數大於限制的情況
if (count > show_) {
if (offset <= sep_) {
buffer.append(getPart(1, sep_ + split_, offset, limit));
buffer.append(getEllipsis("...")).append(getNormalPart(String.valueOf(count), count, limit));
} else if (offset > (count - sep_)) {
buffer.append(getNormalPart(String.valueOf(1), 1, limit)).append(getEllipsis("..."));
buffer.append(getPart(count - sep_ - 1, count, offset, limit));
} else {
buffer.append(getNormalPart(String.valueOf(1), 1, limit)).append(getEllipsis("..."));
buffer.append(getPart(offset - split_ - 1, offset + split_ + 1, offset, limit));
buffer.append(getEllipsis("...")).append(getNormalPart(String.valueOf(count), count, limit));
}
}
// 判斷是否顯示下頁
if (offset < count) {
int next = offset + 1;
buffer.append(getNormalPart("下頁", next, limit));
}
return buffer.toString();
}
// 一般按鈕
private static StringBuffer getNormalPart(String content, int offset, int limit) {
StringBuffer buffer = new StringBuffer();
buffer.append("<a href='javascript:void(0);' ").append("οnclick='pageClick(").append(offset).append(",").append(limit).append(
");'").append("'>").append(content).append("</a>");
return buffer;
}
// 拼接中間部分
private static StringBuffer getPart(int begin, int end, int offset, int limit) {
StringBuffer buffer = new StringBuffer();
for (int i = begin; i <= end; i++) {
if (offset == i)
buffer.append(getSelectedPart(String.valueOf(i), i));
else
buffer.append(getNormalPart(String.valueOf(i), i, limit));
}
return buffer;
}
// 選中按鈕
private static StringBuffer getSelectedPart(String content, Integer value) {
StringBuffer buffer = new StringBuffer();
buffer.append("<a href='javascript:void(0);'").append("' class='current'>").append(content).append("</a>");
return buffer;
}
// 省略部分
private static StringBuffer getEllipsis(String content) {
StringBuffer buffer = new StringBuffer();
buffer.append("<a href='javascript:void(0);'>").append(content).append("</a>");
return buffer;
}
怎麼使用呢?看我下面的使用方法(參數vo是繼承Page類)
/**
* 分頁參數對象
*
* @author shadow
* @email [email protected]
* @create 2012.04.28
*/
@SuppressWarnings("serial")
public class Page<T> implements Serializable {
private Integer offset = 1; // 索引值
private Integer limit = 10; // 顯示數
private Integer total = 0; // 記錄數
private Integer index;
private String navigation; // 分頁導航
private List<T> list; // 分頁數據
public Integer getLimit() {
return limit;
}
public void setLimit(Integer limit) {
this.limit = limit;
}
public Integer getCount() {
return total % limit == 0 ? total / limit : total / limit + 1;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Integer getOffset() {
int count = getCount();
if (offset > count)
offset = count;
if (offset < 1)
offset = 1;
return offset;
}
public void setIndex(Integer index) {
this.index = index;
}
public Integer getIndex() {
if (offset < 1)
offset = 1;
index = (offset - 1) * limit;
return index;
}
public void setOffset(Integer offset) {
this.offset = offset;
}
public String getNavigation() {
return navigation;
}
public void setNavigation(String navigation) {
this.navigation = navigation;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
@Override
public Result page(AdminUserVo vo) {
Result result = getInstance();
try {
vo.setTotal(dao.page4total(vo));
List<AdminUser> models = dao.page4list(vo);
vo.setList(models);
vo.setNavigation(NavUtils.build(vo.getOffset(), vo.getLimit(), vo.getCount()));
result.setSuccess(true).setValue(vo);
} catch (Exception e) {
result.setException(new MVCException(e));
}
return result;
}
最後頁面增加一個div存放page.navigation這個字符變成html標籤即可,以及在頁面增加pageClick的js函數作爲回調響應請求,相信大家都懂了吧...thx