package com.founder.bbc.generic;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
/**
*作者:张人杰 北京师范大学 计算机系
*转载、使用,请注明作者信息
*此包装类已应用于方正某项目
*/
public class DataPage<T> {
private Integer pageNo;//当前页码
private Integer pageSize;//每页条数
private Integer total;//总条数
private List<T> list=Collections.EMPTY_LIST;//内容
private String formId;//用于带有查询条件方式的分页导航
private String functionSuffix=""+Math.abs(UUID.randomUUID().hashCode());//用于url方式的导航定义页面函数,以区分有多个分页查询的页面中的导航
private String tmpFunctionSuffix;//用于保存functionSuffix
private String orderByColumn;//用于排序
private String orderByColumnOrder;//用于排序
public void setNeedFunctionSuffix(Boolean need){
if(need==false){
if(!"".equals(functionSuffix)){
tmpFunctionSuffix=functionSuffix;
}
functionSuffix="";
}else if(need==true){
if(tmpFunctionSuffix!=null&&!"".equals(tmpFunctionSuffix)){
functionSuffix=tmpFunctionSuffix;
}
tmpFunctionSuffix="";
}
}
public DataPage(){
}
/**
* 初始化
* @param pageNo 当前页码
* @param pageSize 每页条数
* @param total 总条数
* @param list 内容
*/
public DataPage(Integer pageNo,Integer pageSize,Integer total,List<T> list){
this.pageNo=pageNo;
this.pageSize=pageSize;
this.total=total;
this.list=list;
}
/**
* 得到第一条记录的rownum(开区间),用于数据查询
* @return
*/
public Integer getStartRecord(){
return (pageNo-1)*pageSize;
}
/**
* 得到最后一条记录的rownum(闭区间),用于数据查询
* @return
*/
public Integer getEndRecord(){
return pageNo*pageSize;
}
/**
* 得到数据结果集的起始记录,用于页面显示
* @return
*/
public Integer getResultStartRow(){
return getStartRecord();
}
/**
* 得到数据结果集的结束记录,用于页面显示
* @return
*/
public Integer getResultEndRow(){
return getStartRecord()+list.size();
}
/**
* 得到总页数
* @return
*/
public Integer getTotalPageCount(){
if(total>0&&total%pageSize>0){
return total/pageSize+1;
}else if(total>0){
return total/pageSize;
}else{
return 0;
}
}
/**
* 得到最后一页页码
* @return
*/
public Integer getLastPageNo(){
return getTotalPageCount();
}
/**
* 得到用于页面导航的String,包括各页的链接及上一页、下一页
* @return
*/
public String getPageNavigator(){
if(pageSize<0)pageSize=20;
if(pageNo<1)pageNo=1;
if(pageNo>getTotalPageCount())pageNo=getTotalPageCount();
if(total==0){
return "<a href='javascript:void();' class='navFirstPage' pageNo='0' function='firstPage"+functionSuffix+"'>第一页</a><span class='navCurrentPage' function='currentPage"+functionSuffix+"'>1</span><a href='javascript:void();' class='navLastPage' pageNo='0' function='lastPage"+functionSuffix+"'>最后一页</a><span class='navTotal' function='totalPage"+functionSuffix+"'>共0页</span>";
}else{
StringBuffer start=new StringBuffer("<a href='javascript:void();' class='navFirstPage' pageNo='1' function='firstPage"+functionSuffix+"'>第一页</a>");
for(int i=pageNo/10<5?1:pageNo/10-4;i*10<pageNo-2;i++){
start.append("<a href='javascript:void();' class='navTenPage' pageNo='"+(i*10)+"' function='tenPage"+functionSuffix+"'>"+(i*10)+"</a>");
}
if(pageNo>3&&getTotalPageCount()>5)start.append("<span class='naveEllipses' function='ellipses"+functionSuffix+"'>...</span>");
StringBuffer page=new StringBuffer();
if(getTotalPageCount()-pageNo<3){
//前四页
for(int i=1;i<=4-(getTotalPageCount()-pageNo)&&pageNo-i>0;i++){
page.insert(0, "<a href='javascript:void();' class='navPage' pageNo='"+(pageNo-i)+"' function='page"+functionSuffix+"'>"+(pageNo-i)+"</a>");
}
}else{
//前两页
for(int i=1;i<=2&&pageNo-i>0;i++){
page.insert(0, "<a href='javascript:void();' class='navPage' pageNo='"+(pageNo-i)+"' function='page"+functionSuffix+"'>"+(pageNo-i)+"</a>");
}
}
//当前页
page.append("<a href='javascript:void();' class='navCurrentPage' function='currentPage"+functionSuffix+"'>"+pageNo+"</a>");
if(pageNo<3){
//后四页
for(int i=1;i<=5-pageNo&&pageNo+i<=getTotalPageCount();i++){
page.append("<a href='javascript:void();' class='navPage' pageNo='"+(pageNo+i)+"' function='page"+functionSuffix+"'>"+(pageNo+i)+"</a>");
}
}else{
//后两页
for(int i=1;i<=2&&pageNo+i<=getTotalPageCount();i++){
page.append("<a href='javascript:void();' class='navPage' pageNo='"+(pageNo+i)+"' function='page"+functionSuffix+"'>"+(pageNo+i)+"</a>");
}
}
start.append(page);
if(getTotalPageCount()-pageNo>2&&getTotalPageCount()>5){
start.append("<span class='naveEllipses' function='ellipses"+functionSuffix+"'>...</span>");
}
//后5个10的倍数页
for(int i=(pageNo+2)/10+1;i<(pageNo+2)/10+5&&i*10<=getTotalPageCount();i++){
start.append("<a href='javascript:void();' class='navTenPage' pageNo='"+(i*10)+"' function='tenPage"+functionSuffix+"'>"+(i*10)+"</a>");
}
start.append("<a href='javascript:void();' class='navLastPage' pageNo='"+getTotalPageCount()+"' function='lastPage"+functionSuffix+"'>最后一页</a>");
start.append("<span class='navTotal' function='totalPage"+functionSuffix+"'>共"+getTotalPageCount()+"页"+getTotal()+"条</span>");
return start.toString();
}
}
/**
* 导航方式一,通过内置form参数生成jQuery带查询条件的导航代码,不带查询条件方式
* 需要在Action中设置查询表单的formId
* @return
*/
public String getFormNavJS(){
return "<script type=\"text/javascript\">" +
"function navGoto(pageNo){" +
"var iptPageNo=jQuery('#"+formId+"').find(\"input[name='pageNo']\");" +
"if(iptPageNo.size()==0)" +
"jQuery('#"+formId+"').append(\"<input type='hidden' name='pageNo' value='\"+pageNo+\"'/>\");" +
"else " +
"iptPageNo.val(pageNo);" +
"var iptPageSize=jQuery('#"+formId+"').find(\"input[name='pageSize']\");" +
"if(iptPageSize.size()==0)" +
"jQuery('#"+formId+"').append(\"<input type='hidden' name='pageSize' value='"+pageSize+"'/>\");" +
"else " +
"iptPageSize.val("+pageSize+");" +
"jQuery('#"+formId+"').submit();" +
"}\n" +
"jQuery(document).ready(function(){\n" +
"jQuery(\"a[function='firstPage"+functionSuffix+"']\").click(function(){navGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='prevPage"+functionSuffix+"']\").click(function(){navGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='page"+functionSuffix+"']\").click(function(){navGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='tenPage"+functionSuffix+"']\").click(function(){navGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='nextPage"+functionSuffix+"']\").click(function(){navGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='lastPage"+functionSuffix+"']\").click(function(){navGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='gotoPage"+functionSuffix+"']\").click(function(){var pageNo=parseInt(jQuery(jQuery(this).attr('pageNoInputId')).val());if(!isNaN(pageNo))navGoto(pageNo);});\n" +
"})</script>";
}
/**
* 导航方式二,通过拼接url参数导航
*/
public String getUrlNavJS(){
return "<script type=\"text/javascript\">" +
"function navUrlGoto(pageNo){" +
"var url=document.URL;if(url.indexOf(\"?\")==-1)url+=\"?\";"+
"if(url.indexOf(\"pageNo\")>0)url=url.replace(/pageNo=\\d*/,\"pageNo=\"+pageNo);"+
"else url=url+\"&pageNo=\"+pageNo;"+
"if(url.indexOf(\"pageSize\")==-1)url+=\"&pageSize=\"+"+pageSize+";"+
"window.open(url,\"_self\");"+
"}\n" +
"jQuery(document).ready(function(){\n" +
"jQuery(\"a[function='firstPage"+functionSuffix+"']\").click(function(){navUrlGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='prevPage"+functionSuffix+"']\").click(function(){navUrlGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='page"+functionSuffix+"']\").click(function(){navUrlGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='tenPage"+functionSuffix+"']\").click(function(){navUrlGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='nextPage"+functionSuffix+"']\").click(function(){navUrlGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='lastPage"+functionSuffix+"']\").click(function(){navUrlGoto(jQuery(this).attr('pageNo'));});\n" +
"jQuery(\"a[function='gotoPage"+functionSuffix+"']\").click(function(){var pageNo=parseInt(jQuery(jQuery(this).attr('pageNoInputId')).val());if(!isNaN(pageNo))navUrlGoto(pageNo);});\n" +
"})</script>";
}
/**
* 导航的基本css样式
* @return
*/
public String getNavigatorCSS(){
return "<style type=\"text/css\">\n"+
".navFirstPage{margin-right:2px;margin-right:2px;padding:1px;}\n"+
".navPage{margin-right:2px;margin-right:2px;padding:1px;}\n"+
".navTenPage{margin-right:2px;margin-right:2px;padding:1px;}\n" +
".navCurrentPage{margin-right:2px;margin-right:2px;padding:1px;text-decoration:none}\n" +
".navLastPage{margin-right:2px;margin-right:2px;padding:1px;}\n" +
".navTotal{margin-right:2px;margin-right:2px;padding:1px;}\n"+
"</style>";
}
public String getNavigatorCSSNormalStyleOringe(){
return "<style type=\"text/css\">"+
".navFirstPage{border:1px solid #cc9966;font:11pt 宋体;color:#cc6600;text-decoration:none}"+
".navPage{border:1px solid #cc9966;font:11pt 宋体;color:#cc6600;text-decoration:none}"+
".navTenPage{border:1px solid #cc9966;font:11pt 宋体;color:#cc6600;text-decoration:none}"+
".navCurrentPage{font:11pt bold 宋体;color:#cc6600;text-decoration:none}"+
".navLastPage{border:1px solid #cc9966;font:11pt 宋体;color:#cc6600;text-decoration:none}"+
".navTotal{border:1px solid #cc9966;font:11pt 宋体;color:#cc6600;text-decoration:none}"+
"</style>";
}
public String getNavigatorCSSNormalStyleGreen(){
return "<style type=\"text/css\">"+
".navFirstPage{border:1px solid #009933;font:11pt 宋体;color:#009933;text-decoration:none}"+
".navPage{border:1px solid #009933;font:11pt 宋体;color:#009933;text-decoration:none}"+
".navTenPage{border:1px solid #009933;font:11pt 宋体;color:#009933;text-decoration:none}"+
".navCurrentPage{font:11pt bold 宋体;color:#009933;text-decoration:none}"+
".navLastPage{border:1px solid #009933;font:11pt 宋体;color:#009933;text-decoration:none}"+
".navTotal{border:1px solid #009933;font:11pt 宋体;color:#009933;text-decoration:none}"+
"</style>";
}
public String getNavigatorCSSNormalStyleBlue(){
return "<style type=\"text/css\">"+
".navFirstPage{border:1px solid #336699;font:11pt 宋体;color:#336699;text-decoration:none}"+
".navPage{border:1px solid #336699;font:11pt 宋体;color:#336699;text-decoration:none}"+
".navTenPage{border:1px solid #336699;font:11pt 宋体;color:#336699;text-decoration:none}"+
".navCurrentPage{font:11pt bold 宋体;color:#336699;text-decoration:none}"+
".navLastPage{border:1px solid #336699;font:11pt 宋体;color:#336699;text-decoration:none}"+
".navTotal{border:1px solid #336699;font:11pt 宋体;color:#336699;text-decoration:none}"+
"</style>";
}
public String getFieldSortJS(){
return "<script type=\"text/javascript\" language=\"javascript\">"+
" function replaceParam(url, paramName,paramValue){"+
" if(url.indexOf(\"?\")==-1)url+=\"?\";"+
" var start=url.indexOf(paramName);"+
" if(start==-1){"+
" url+=\"&\"+paramName+\"=\"+paramValue;"+
" return url;"+
" }else{"+
" var start=url.indexOf(\"=\",start);"+
" var newUrl=url.substring(0,start+1)+paramValue;"+
" var end=url.indexOf(\"&\",start);"+
" if(end>-1)newUrl+=url.substr(end);"+
" return newUrl;"+
" }"+
" }\n"+
" $(document).ready(function(){" +
" $(\".setOrder\").click(function(){"+
" var url=document.URL;"+
" var field=$(this).attr(\"field\");"+
" var order=$(this).attr(\"order\");"+
" if(order==\"desc\")$(this).attr(\"order\",\"asc\");"+
" else $(this).attr(\"order\",\"desc\");"+
" url=replaceParam(url,\"otherParams.orderByColumn\",field);"+
" url=replaceParam(url,\"otherParams.orderByColumnOrder\",order);"+
" window.open(url,\"_self\");"+
" });"+
" });"+
"</script>";
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public String getOrderByColumn() {
return orderByColumn;
}
public void setOrderByColumn(String orderByColumn) {
this.orderByColumn = orderByColumn;
}
public String getOrderByColumnOrder() {
return orderByColumnOrder;
}
public void setOrderByColumnOrder(String orderByColumnOrder) {
this.orderByColumnOrder = orderByColumnOrder;
}
public String getFormId() {
return formId;
}
/**
* 用于导航方式一
* @param formId
*/
public void setFormId(String formId) {
this.formId = formId;
}
public String getFunctionSuffix() {
return functionSuffix;
}
public void setFunctionSuffix(String functionSuffix) {
this.functionSuffix = functionSuffix;
}
public Boolean isPrevExist(){
return pageNo>1;
}
public Boolean isNextExist(){
return pageNo<getTotalPageCount();
}
public static void main(String[] args) {
DataPage<Integer> page = new DataPage<Integer>(1,5,496,new ArrayList<Integer>());
System.out.println(page.getPageNavigator()+"<br/>");
page.setPageNo(4);
System.out.println(page.getPageNavigator()+"<br/>");
page.setPageNo(5);
System.out.println(page.getPageNavigator()+"<br/>");
page.setPageNo(6);
System.out.println(page.getPageNavigator()+"<br/>");
page.setPageNo(10);
System.out.println(page.getPageNavigator()+"<br/>");
page.setPageNo(94);
System.out.println(page.getPageNavigator()+"<br/>");
page.setPageNo(95);
System.out.println(page.getPageNavigator()+"<br/>");
page.setPageNo(96);
System.out.println(page.getPageNavigator()+"<br/>");
page.setPageNo(97);
System.out.println(page.getPageNavigator()+"<br/>");
page.setPageNo(100);
System.out.println(page.getPageNavigator()+"<br/>");
}
}