一、创建分页通用类

 package com.dkyw.util;

 import java.util.List;

 public class Page<T> {
private int total;//总页数
private int pageIndex;//当前页码
private int pageSize;//每页大小
private List<T> rows;//当前页的集合对象 public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
} }

二、serivice层封装返回的集合信息为page对象的集合

public Page<Template> getTemplateForPage(int pageIndex,int pageSize) {
int pageStart = (pageIndex - 1) * pageSize;
List<Template> list = templateDao.getForPage(pageStart,pageSize);
for (Template template : list) {
template.setDataSource(dataSourceDao.getById(template
.getDataSourceId()));
} Page<Template> page =new Page<Template>();
page.setPageIndex(pageIndex);
page.setPageSize(pageSize);
page.setRows(list);
int count = templateDao.getCount();
page.setTotal((int)Math.ceil(count*1.0/pageSize));
return page;
}

三、返回至前端后的处理

    /* 获取分页数据 */
function getTemplateInfoForPage(pageIndex,pageSize){
$.ajax({
url : "getTemplateInfoForPage.action",//要请求的服务器url
data : {
"pageIndex" : pageIndex,
"pageSize":pageSize
},
async : true, //是否为异步请求
type : "POST", //请求方式为POST
success : function(result) {
console.log("分页数据");
result = JSON.parse(result);
var _LENGTH = 3;//最大页数3
//拼接分页
if(result[0].total<_LENGTH){
$("#pageInfo").html("");
var start = (pageIndex-1)>0 ? pageIndex-1 : pageIndex;
var end = (pageIndex+1)<=result[0].total ? pageIndex+1 : pageIndex;
$("#pageInfo").append("<li><a onclick='getTemplateInfoForPage("+start +","+pageSize+")' aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>");
for (var int = 1; int < result[0].total+1; int++) {
if(int==pageIndex){
$("#pageInfo").append("<li class='active'><a onclick='getTemplateInfoForPage("+int+","+pageSize+")'>"+int+"</a></li>");
}else{
$("#pageInfo").append("<li><a onclick='getTemplateInfoForPage("+int+","+pageSize+")'>"+int+"</a></li>");
} }
$("#pageInfo").append("<li><a onclick='getTemplateInfoForPage("+end+","+pageSize+")' aria-label='Next'><span aria-hidden='true'>&raquo;</span></a></li>")
}else{
debugger;
$("#pageInfo").html("");
var pageArr=new Array();
//
if(pageIndex+Math.floor(_LENGTH/2) <= result[0].total&&pageIndex-Math.floor(_LENGTH/2) > 0){//当前页没有超过总页数
for(var i = 0 ;i < _LENGTH; i++){
pageArr[i] = pageIndex-Math.floor(_LENGTH/2) + i;
}
}else if(pageIndex<=Math.floor(_LENGTH/2)){
for (var int2 = 1; int2 < _LENGTH+1; int2++) {
pageArr.push(int2);
}
// pageArr = [1,2,3,4,5,6,7,8,9] ;
}else{
for(var i = 0 ;i < _LENGTH; i++){
//
pageArr[i] = result[0].total-(_LENGTH-1)+i;
}
} var start = (pageIndex-1)>0 ? pageIndex-1 : pageIndex;
var end = (pageIndex+1)<=result[0].total ? pageIndex+1 : pageIndex;
$("#pageInfo").append("<li><a onclick='getTemplateInfoForPage("+start +","+pageSize+")' aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>");
for (var int = pageArr[0]; int <= pageArr[pageArr.length-1]; int++) {
if(int==pageIndex){
$("#pageInfo").append("<li class='active'><a onclick='getTemplateInfoForPage("+int+","+pageSize+")'>"+int+"</a></li>");
}else{
$("#pageInfo").append("<li><a onclick='getTemplateInfoForPage("+int+","+pageSize+")'>"+int+"</a></li>");
} }
$("#pageInfo").append("<li><a onclick='getTemplateInfoForPage("+end+","+pageSize+")' aria-label='Next'><span aria-hidden='true'>&raquo;</span></a></li>") } $("#tbody").html("");
$.each(result[0].rows,function(index,item){
console.log(item);
$("#tbody").append("<tr onclick='showTemplate("+item.id
+",\""+item.echartType+"\");'>"
+"<td><input type='checkbox' name='deletecheck'data-id="+item.id+" data-type="+echartTypemap[item.echartType]+"></td>"
+"<td>"+item.id+"</td>"
+"<td>"+item.templateName+"</td>"
+"<td>"+item.dataSource.dataName+"</td>"
+"<td>"+getEchartType(item.echartType)+"</td>"
+"<td>"+getEchartMethod(item.echartMethod)+"</td>"
+"<td>"+item.tableName+"</td>"
+"<td><button type='button' class='btn btn-default' onclick='updateTemplate("+
item.id+","+item.dataSource.id+",\""+item.templateName+"\",\""+item.echartType+"\",\""+item.rowField+"\",\""+item.tableName+"\");'>修改</button></td>"
+"</tr>");
});
}
})
}
05-11 16:02