yongyonglsy 2016-11-07
原文:
正在做的一个Web项目,其中用到了jQuery datatables这个表格控件,说实话挺好用的,顺便自己记录一下
首先,定义一个<table>表格
response.setCharacterEncoding("UTF-8"); PrintWriter out = null; String json = null; // 返回的json数据 try { out = response.getWriter(); } catch (IOException e) { e.printStackTrace(); } String statName = null; String sEcho = "0";// 记录操作的次数 每次加1 String iDisplayStart = "0";// 起始 String iDisplayLength = "10";// size int count = 0; //查询出来的数量 String aoData = request.getParameter("aoData"); //获取jquery datatable当前配置参数 JSONArray jsonArray = JSONArray.fromObject(aoData); for (int i = 0; i < jsonArray.size(); i++) { try { JSONObject jsonObject = (JSONObject)jsonArray.get(i); if (jsonObject.get("name").equals("sEcho")) sEcho = jsonObject.get("value").toString(); else if (jsonObject.get("name").equals("iDisplayStart")) iDisplayStart = jsonObject.get("value").toString(); else if (jsonObject.get("name").equals("iDisplayLength")) iDisplayLength = jsonObject.get("value").toString(); else if (jsonObject.get("name").equals("statId")) statName = jsonObject.get("value").toString(); } catch (Exception e) { break; } } JSONArray jsonArray2 = new JSONArray(); JSONObject jsonObject2 = new JSONObject(); StatCleanService service = new StatCleanService(); //为操作次数加1 int initEcho = Integer.parseInt(sEcho) + 1; count = service.getStatCleanRevampCount(statName);//总记录数 List statFailList = service.getStatCleanRevampList(Integer.parseInt(iDisplayStart), Integer.parseInt(iDisplayLength), statName); for (Object object : statFailList) { StatCleanRevampNoticeTable table = (StatCleanRevampNoticeTable)object; jsonObject2.put("statCleanRevampId", table.getStatCleanRevampId()); jsonObject2.put("statCleanId", table.getStatCleanId()); jsonObject2.put("statId", table.getStatId()); jsonObject2.put("statName", table.getStatName()); jsonObject2.put("revampStatus", table.getRevampStatus()); jsonObject2.put("problemDes", table.getProblemDes()); jsonArray2.add(jsonObject2); } json = "{\"sEcho\":" + initEcho + ",\"iTotalRecords\":" + count + ",\"iTotalDisplayRecords\":" + count + ",\"aaData\":" + jsonArray2.toString() + "}"; //传到页面 out.println(json); out.close(); 其中参数 [java] view plain copy sEcho,iTotalRecords,iTotalDisplayRecords,aaData名称是固定的,不能修改;
iDisplayStart,iDisplayLength为每次查询的起始记录和长度;
加载页面url链接-->load form-->指向分页的action-->json数据返回给页面
效果图如下: