一、创建分页通用类
1 package com.dkyw.util; 2 3 import java.util.List; 4 5 public class Page<T> { 6 private int total;//总页数 7 private int pageIndex;//当前页码 8 private int pageSize;//每页大小 9 private List<T> rows;//当前页的集合对象 10 11 public int getTotal() { 12 return total; 13 } 14 public void setTotal(int total) { 15 this.total = total; 16 } 17 public int getPageIndex() { 18 return pageIndex; 19 } 20 public void setPageIndex(int pageIndex) { 21 this.pageIndex = pageIndex; 22 } 23 public int getPageSize() { 24 return pageSize; 25 } 26 public void setPageSize(int pageSize) { 27 this.pageSize = pageSize; 28 } 29 public List<T> getRows() { 30 return rows; 31 } 32 public void setRows(List<T> rows) { 33 this.rows = rows; 34 } 35 36 }
二、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'>«</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'>»</span></a></li>") }else{ debugger; $("#pageInfo").html(""); var pageArr=new Array(); //4 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++){ //8 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'>«</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'>»</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>"); }); } }) }