easyui datagrid数据网格的使用,以及数据传递和返回的格式
easyui datagrid作用
datagrid以表格格式显示数据,并为选择、排序、分组和编辑数据提供了丰富的支持。数据网格(datagrid)的设计目的是为了减少开发时间,且不要求开发人员具备指定的知识。它是轻量级的,但是功能丰富。它的特性包括单元格合并,多列页眉,冻结列和页脚,等等。
datagrid数据传递给后台
atagrid数据传递给后台分为,1:开发者自己需要传递至后台的数据 2:datagrid封装的传递的数据。下面分别介绍:
1. 开发者自己需要传递至后台的数据
定义好的datagrid。
<table id="datagrid"></table>javascript
$('#datagrid').datagrid('options').url ='******/*****.action';java
$('#datagrid').datagrid('options').queryParams = {
'ProductVo.itemId' : itemId,
'ProductVo.productId' : productId,
'ProductVo.listPrice' : listPrice,
'ProductVo.status' : status
};javascript
$('#datagrid').datagrid('reload');如果项目中使用的struts的Action,直接在action中定义好ProductVo的属性,并设置getter和setter方法,就可以接受到这些参数了。
2:datagrid封装的传递的数据
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;
}datagrid数据的返回(后台传给前台)

后台传给前台datagrid数据的格式如下:

如果上面这些属性的Vo类为ProductVo。后台查询数据库返回List<ProductVo> obj;则通过通过下面代码将数据传给前台datagrid:
int totalcount = obj.size();JSONObject resultObj = toGridJson(totalcount ,obj);具体的toGridJson方法如下:
private JSONObject toGridJson(int totalCount, Object obj) {
// 如果数据集对象为null做个特殊处理
if(null == obj) {
JSONObject jsonResult = new JSONObject();
jsonResult.put("total", totalCount);
jsonResult.put("rows", new JSONArray());
return jsonResult;
}
if(!Collection.class.isAssignableFrom(obj.getClass())) {
JSONObject jsonResult = new JSONObject();
jsonResult.put("total", totalCount);
jsonResult.put("rows", new JSONArray());
return jsonResult;
}
JSONArray jsonArray = JSONArray.fromObject(obj);
JSONObject jsonResult = new JSONObject();
jsonResult.put("total", totalCount);
jsonResult.put("rows", jsonArray);
return jsonResult;
}在使用JSONAray的转化对象时,存在Integer、Long等类型的参数如果为空会有默认赋值问题,导致datagrid显示的值就有问题,可使用下面方法解决:
/**
* 生成datagrid的需要格式 可以杜绝Long、Integer类型的默认赋值问题
*
* @param totalCount
* @param obj
* @return
*/
public static JSONObject toJsonString(int totalCount, Object obj) {
if(null == obj) {
JSONObject jsonResult = new JSONObject();
jsonResult.put("total", totalCount);
jsonResult.put("rows", new JSONArray());
return jsonResult;
}
if(!Collection.class.isAssignableFrom(obj.getClass())) {
JSONObject jsonResult = new JSONObject();
jsonResult.put("total", totalCount);
jsonResult.put("rows", new JSONArray());
return jsonResult;
}
String json = toJsonString(obj);
JSONObject jsonResult = new JSONObject();
jsonResult.put("total", totalCount);
jsonResult.put("rows", json);
return jsonResult;
}public static String toJsonString(Object obj) {
String json = null;
try {
ObjectMapper mapper = new ObjectMapper();
json = mapper.writeValueAsString(obj);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return json;
}上面的ObjectMapper来自:

你可能注意到了代码中的total、rows,datagrid接收数据只认识这两个属性。然后将封装好的resultObj写给页面就可以了:
public void ajaxPrintPage(Object resultObj) {
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
PrintWriter writer = null;
try {
try {
writer = response.getWriter();
if (null == obj) {
writer.print("");
} else {
writer.print(obj.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
} finally {
if (writer != null) {
writer.flush();
writer.close();
}
}
}注意:
本文内容仅供个人学习/研究/参考使用,不构成任何决策建议或专业指导。分享/转载时请标明原文来源,同时请勿将内容用于商业售卖、虚假宣传等非学习用途哦~感谢您的理解与支持!