问题描述
当调整数据个数时
不能实现分页 下面就是实现分页的效果
action代码
private int page; // 第几页
private int rows; // 行数
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
//查询全部用户 fenye
public String select(){
List<UserBean> listUser = userService.Select(page,rows);
//得到总页数
int all = userService.all();
if(listUser!=null) {
Map<String, Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("total",all);// total键 存放总记录数,必须的
jsonMap.put("rows", listUser);// rows键 存放每页记录 list
//格式化分页数据*** 必须的
result = JSONObject.fromObject(jsonMap);
}
return SUCCESS;
}
daoImpl代码
@Override
public List<UserBean> Select(int page,int rows) {
int num = (page - 1) * rows;
Object excute = hibernate.execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery("from UserBean");
query.setFirstResult(num);
query.setMaxResults(rows);
List list = query.list();
return list;
}
});
return (List<UserBean>) excute;
}
js代码
$(function() {
$("#tt").datagrid({
pageSize : 2,//每页显示的记录条数,默认为5
pageList : [ 2,5,10, 15, 20 ],//可以设置每页记录条数的列表
beforePageText: '第',//页数文本框前显示的汉字
afterPageText: '页 共 {pages} 页',
displayMsg: '第{from}到{to}条,共{total}条',
columns: [[
{
title: '序号', field: 'u_id',halign: 'center',align: 'center',width:100,id:'typ',
},
{
title: '用户名称', field: 'u_name',halign: 'center',align: 'center',width:100,
},
{
title: '用户密码', field: 'u_pwd',halign: 'center',align: 'center',width:100,
},
{
title: '性别', field: 'u_sex',halign: 'center',align: 'center',width:100,
},
{
title: '邮箱', field: 'u_mail',halign: 'center',align: 'center',width:100,
},
{
title: '自我介绍', field: 'u_describe',halign: 'center',align: 'center',width:100,
},
{
title: '操作',field: 'action',halign: 'center',align: 'center',width: 200,
formatter: function (value,row,index) {
if (row.editing == true) {
return "<a href='#' οnclick='saveRow(" + index + ");'>确定</a> <a href='#' οnclick='reDo(" + index + ");'>取消</a>";
}
else {
return "<a href='#' οnclick='return deleteRow();'>删除</a>";
}
}
}
]],
});
});
body代码
<body>
<center>
<h2>用户列表</h2>
<table id="tt" class="easyui-datagrid"
style="width: 835px; height: 250px" title="用户列表"
data-options="rownumbers:true,singleSelect:true,pagination:true,url:'selectUser',method:'get'">
<!--要想显示分页控件,pagination属性必须为true -->
</table>
</center>
</body>
struts2代码
<!-- 查询全部用户信息 -->
<action name="selectUser" class="useraction" method="select">
<result type="json">
<param name="root">result</param>
</result>
</action>
实现的效果图为