datagrid以表格花样显示数据,并为选择、排序、分组和编辑数据提供了厚实的支持。数据网格(datagrid)的设计目的是为了削减开发时间,且不要求开发人员具备指定的知识。它是轻量级的,然则功效厚实。它的特征包罗单米格合并,多列页眉,冻结列和页脚,等等。
atagrid数据通报给后台分为,1:开发者自己需要通报至后台的数据 2:datagrid封装的通报的数据。下面划分先容:
界说好的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方式,就可以接受到这些参数了。
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数据的花样如下:
若是上面这些属性的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(); } } }
1.阿里云: 本站现在使用的是阿里云主机,平安/可靠/稳固。点击领取2000米代金券、领会最新阿里云产物的种种优惠流动点击进入
2.腾讯云: 提供云服务器、云数据库、云存储、视频与CDN、域名等服务。腾讯云各种产物的最新流动,优惠券领取点击进入
3.广告同盟: 整理了现在主流的广告同盟平台,若是你有流量,可以作为参考选择适合你的平台点击进入
链接: http://www.fly63.com/article/detial/742