NO.85 项目信息分页显示
1.在ProjectDao中添加分页相关方法:
ListfindPageObjects(
@Param("startIndex") int startIndex,
@Param("pageSize") int pageSize);
说明:当dao中的方法参数多余1个时要使用@Param注解定义参数.
int getRowCount();
2.在ProjectMapper.xml文件中添加方法对应的id
<select id="findPageObjects" resultType="cn.xxx.ttms.product.entity.Project">
select * from tms_projects order by createdTime desc limit #{startIndex},#{pageSize}
</select>
<select id="getRowCount" esultType="int">
select count(*) from tms_projects;
</select>
3.在ProjectService接口及实现类中添加分页查询方法
MapfindPageObjects(int pageCurrent){
int pageSize=2;
int startIndex=(pageCurrent-1)*pageSize;
获取当前页数据
List<Project> list = projectDao.findPageObjects(startIndex,pageSize);
获取分页信息(总记录数,总页数,.....)
int rowCount = projectDao.getRowCount();
int pageCount = rowCount / pageSize;
if(rowCount%pageSize != 0){
pageCount++;
}
.......
封装分页信息(构建PageObject对象:VO)
将当前数据以及分页信息封装到map,然后返回.
}
4.编写控制层方法
@RequestMapping("doGetPageObjects")
@ResponseBody
public Map<String,Object> doGetPageObjects(Integer pageCurrent) {
return projectService.findPageObjects(pageCurrent);
}
5.编写客户端的project_list.js,page.js
project_list.js:
$(document).ready(function(){
doGetObjects();
})
function doGetObjects(){
var url="project/doGetPageObjects.do";
var pageCurrent=$("#pageId").data("pageCurrent");
if(!pageCurrent)pageCurrent=1;
var params={"pageCurrent":pageCurrent};
$.getJSON(url,params,function(result) {
setTableBodyRows(result.list);
setPagination(result.pageObject);
})
};
function setTableBodyRows(result){....}
page.js:
$(document).ready(function(){
$("#pageId").on("click",".pre,.next,.first,.last",jumpToPage)
})
function setPagination(pageObject){
$(".pageCount").html("总页数("+pageObject.pageCount+")");
$(".pageCurrent").html("当前页("+pageObject.pageCurrent+")");
$("#pageId").data("pageCount",pageObject.pageCount);
$("#pageId").data("pageCurrent",pageObject.pageCurrent);
}
function jumpToPage(){
var cValue=$(this).attr("class");
var pageCurrent=$("#pageId").data("pageCurrent");
var pageCount=$("#pageId").data("pageCount");
if(cValue=="pre"&&pageCurrent>1){
pageCurrent--;
}
.......
$("#pageId").data("pageCurrent",pageCurrent);
doGetObjects();
}