在EasyUI的API文档以及官网的demo中都有一些简单的例子,但大多例子都是静态赋值,今天就给大家写一段动态赋值的方法。我用的是Struts与easyui的复用,所以在URL的地方采用的是action,现在让我们来看下代码的实现。
<table id="tt">
<thead>
<tr>
<th data-options="field:'check',checkbox:true"></th>
<th data-options="field:'id',split:true">id</th>
<th data-options="field:'title',split:true">title</th>
<th data-options="field:'startDate',split:true">startDate</th>
</tr>
</thead>
</table>
<script type="text/javascript">
$('#tt').datagrid({
url:'zsgc.action',
pagination : true,
singleSelect:false,
rownumbers : true,
fitColumns : true,
fit : true,
nowrap : true,
multiSort : true,
border : true,
striped : true
});
</script>
在上述代码中data-options中的field中的字段要与后台的字段名要相同,pagination用来设置分页。在使用分页时要注意,分页是有特指的字段名,来标识当前页面、页面大小、总记录数。
//页面大小
private Integer rows;
//当前页面
private Integer page = 1;
//总记录数
private Integer total;
//索引
private Integer offSet;
private Pagination pagination;
//此处省略get、set方法
/*
*pagination中必须包含total字段,用于返回到JSP界面的总记录数
*/
@Override
public String execute() throws Exception {
boxingMethod();
vol = aAAService.getListOfPage(offSet,rows);
pagination = new Pagination();
pagination.setRows(vol);
pagination.setTotal(total);
return SUCCESS;
}
/*
* 计算总记录数,以及当前的索引位置
*/
private void boxingMethod(){
try {
total = aAAService.findAllRows();
} catch (Exception e) {
e.printStackTrace();
}
if(total % rows == 0){
totalPages = total / rows ;
}else {
totalPages = total / rows + 1;
}
for(Integer i = 1;i<=totalPages;i++){
list.add(i);
}
offSet = (page - 1) * rows;
}
配置Struts.xml文件
<package name="ajax" extends="json-default">
<action name="zsgc" class="AAAAction">
<result type="json">
<param name="root">pagination</param>
</result>
</action>
</package>
如果代码有什么问题,欢迎指出大家一起进步。
本文介绍了如何在Struts框架下,结合EasyUI的datagrid组件进行动态赋值和分页显示。通过在JavaScript中设置datagrid的URL、pagination属性,以及在后台Java代码中处理分页参数,实现数据的动态加载和分页展示。关键字段需与后台字段匹配,同时注意pagination对象中total字段的设置,以确保正确返回总记录数。
1万+

被折叠的 条评论
为什么被折叠?



