要求:在easyui-datagrid中完成paginaton的分页功能。
1.easyui-datagrig的配置
<table id="dg" rownumbers=true fitColumns="true" singleSelect="true"
data-options="pagination:true,fit:true,toolbar:'#tt'">
<thead>
<tr>
<th field="bNo" align="center" width="120px">柜员号</th>
<th field="bType" align="center" width="150px">柜员类型</th>
<th field="jGNo" align="center" width="120px">机构号</th>
<th field="pZCount" align="center" width="120px">凭证数</th>
<th field="zJcount" align="center" width="120px">主件数</th>
<th field="fJcount" align="center" width="120px">附件数</th>
<th field="sBPass" align="center" width="150px">识别凭证</th>
<th field="sBSuccess" align="center" width="150px">识别成功</th>
<th field="sBRoute" align="center" width="120px">识别率</th>
<th field="yWDate" align="center" width="170px">业务日期</th>
</tr>
</thead>
</table>
pagination="true"数据表格会自动将分页栏置于表下方;toolbar="#tt"表示为数据表格上方加入工具栏,具体样式是id="#tt"的模块决定的。在看下js文件对datagrid的其他配置:
$('#dg').datagrid({
url:'user/queryList.action',
pageList: [5,10,20,50,100],
pageSize:5
});
url为页面刷新datagrid自动的请求,每次请求会向后台传入两个参数:1)page,当前第几页;2)rows,每页显示几条数据。因此在后台需要就收这两条重要信息。
2.struts2的Action配置
public String queryList(){
List<User> list =
userService.queryList(page,rows) ;
pag1 = new Pagination<User>();
pag1.setTotal(userService.getCount());
pag1.setRows(list);
return SUCCESS ;
}
<package name="work" namespace="/user" extends="json-default,struts-default">
<action name="queryList" class="userAction" method="queryList">
<result type="json">
<param name="root">pag1</param>
</result>
</action>
</package>
pag1为DTO数据传输对象,有total和rows两属性(datagrid要求的json格式。total是信息总条数,rows是信息实体,键值对)
3.hibernate层service分页方法
public List<User> queryList(int page, int pageSize) {
// TODO Auto-generated method stub
String hql = "from User";
Query query = userDao.getSession().createQuery(hql);
int beginNum = (page-1)*pageSize;
query.setMaxResults(pageSize);
query.setFirstResult(beginNum);
return query.list();
}
public int getCount() {
// TODO Auto-generated method stub
String hql = "select count(1) from User";
Query query = userDao.getSession().createQuery(hql);
Long cc = (Long) query.uniqueResult();
int count = cc.intValue() ;
return count;
}
query.setFirstResult(xxx)里面是起始第几条,query.setMaxResults()里面是从起始条开始搜索的条数,即每页的数量。
4.Action的queryList()将从数据库获取的List集合置于DTO对象pag1的rows属性中,List的大小赋值给pag1的total属性,struts2将pag1以json的数据格式返回页面,datagrid自动实现分页,并且每次页数或每页大小被修改时会访问之前的url,并传给后台page和rows两个参数。
(注意:datagrid数据表格发送和接受的中rows表示不同,前者是每页显示数量大小,后者是json数据的数据体)