jquery easyui中datagrid数据传递和返回的格式
来源:互联网 发布:wow7.0装备数据库 编辑:程序博客网 时间:2024/05/17 22:55
一:datagrid数据传递给后台
datagrid数据传递给后台分为,1:开发者自己需要传递至后台的数据 2:datagrid封装的传递的数据。下面分别介绍:
1. 开发者自己需要传递至后台的数据
定义好的datagrid。
<table id="datagrid"></table>
$('#datagrid').datagrid('options').url ='******/*****.action';
$('#datagrid').datagrid('options').queryParams = {'ProductVo.itemId' : itemId,'ProductVo.productId' : productId,'ProductVo.listPrice' : listPrice,'ProductVo.status' : status};
$('#datagrid').datagrid('reload');
如果项目中使用的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数据的返回(后台传给前台)
后台传给前台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();}}}
注意:
datagrid前台传给后台和后台传给前台都有一个rows属性。但是这两个属性的名称虽然相同,但是值得类型确不一样。前台传给后台的rows为一个数字值,标识当前页的记录数。后台传给前台的rows为一个数组对象,数组中的内容即为数据Vo集合。
0 0
- jquery easyui中datagrid数据传递和返回的格式
- 返回easyui/extjs中datagrid使用的json格式
- jquery easyUI 中动态 改变 Datagrid中数据的方法
- jquery easyUI 中动态 改变 Datagrid中数据的方法
- jquery easyui datagrid加载数据不符合datagrid能够解析的格式处理方法
- SpringMVC与JQuery EasyUI DataGrid传递JSON数据
- jquery easyui datagrid 中清空数据的方法
- jQuery easyui datagrid 的数据加载
- jQuery easyui datagrid 的数据加载
- Jquery easyui 批量处理datagrid的数据
- jquery easyui datagrid 数据样式的设置
- jquery easyui datagrid 对数据格式化(日期格式)
- jquery easyUI 中datagrid单元格的合并
- jquery中easyui的datagrid初始化
- easyui datagrid 数据组装格式
- jQuery EasyUI DataGrid 数据表格
- jQuery EasyUI DataGrid 数据表格
- jquery easyui datagrid 判断数据
- 怎么有效杀蚊子,长效杀蚊,灭绝
- NSURLSession详解
- TVS管原理、特性和选型及失效简析
- 《java并发》--volatile修饰符
- MATLAB报错:未定义函数或变量
- jquery easyui中datagrid数据传递和返回的格式
- nginx+php+mysql 搭建完整web服务器
- ajax学习笔记
- POJ3614_Sunscreen_贪心 && stl的优先级队列
- 1067
- Devexpress MVVM OpenFileDialogService&&SaveFileDialogService
- 进程同步与互斥笔记
- 黑客学习网,攻击技术学习,PDF,视频
- TIANKENG’s restaurant 【区间覆盖】