jquery easyui中datagrid数据传递和返回的格式

时间:2022-05-29 15:02:11

一:datagrid数据传递给后台

    datagrid数据传递给后台分为,1:开发者自己需要传递至后台的数据  2:datagrid封装的传递的数据。下面分别介绍:

    1. 开发者自己需要传递至后台的数据

      定义好的datagrid。

      <table id="datagrid"></table>

$('#datagrid').datagrid('options').queryParams = {
'ProductVo.itemId' : itemId,
'ProductVo.productId' : productId,
'ProductVo.listPrice' : listPrice,
'ProductVo.status' : status
};
如果项目中使用的struts的Action,直接在action中定义好ProductVo的属性,并设置getter和setter方法,就可以接受到这些参数了。

  2:datagrid封装的传递的数据

    datagrid使用了分页的话,其框架自身会向后台传递page、rows这个两个属性值。分别表示当前页和当前页显示的记录行数。可以在action中定义好这两个属性,并同样设置getter和setter方法,就可以接受到这些参数了。     
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数据的返回(后台传给前台)

jquery easyui中datagrid数据传递和返回的格式

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

jquery easyui中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;
}

你可能注意到了代码中的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();
}
}
}


注意:

datagrid前台传给后台和后台传给前台都有一个rows属性。但是这两个属性的名称虽然相同,但是值得类型确不一样。前台传给后台的rows为一个数字值,标识当前页的记录数。后台传给前台的rows为一个数组对象,数组中的内容即为数据Vo集合。