Easyui-Treegrid使用注意事项-sunziren

  版权声明:本文为sunziren原创文章,博客园首发,转载务必注明出处以及作者名称。


  最近,工作中有一个网页需要用到前端框架easyui的treegrid组件,因此我对这个treegird研究了一段时间,踩了很多坑,你必须要按照它的要求来,不然那个treegrid就出不来。

  1.  首先要注意的是,在treegrid前端定义的时候,一定要注明idField和treeField,前者是节点的id,后者是节点的文本。这两个参数必须要有。

  2.  其次要注意的是,后台发往前台的json格式字符串数据,貌似有两种类型。第一种是每一条数据分开的,父数据和子数据分开,每条数据没有childred项,只有“id”、“text”、“_parentId”这三个必须项和你的其他属性项(注意,“_parentId”不要拼写错误,否则前台无法显示,注意里面那个字母“I”是大写的“i”。);第二种类型是没有“_parentId”但是有“children”属性,由于这种格式我试了很久没试出来,就改用第一种方式了。

  3.  最后要注意的是,后台拼接JSON字符串的时候,如果一条数据是根节点,那么就不要给这条数据拼接“_parentId”属性了,根节点不要这个属性,否则前台无法显示。如果不是根节点,那么必须要指明“_parentId”属性值。也就是父节点id。

  4.  下面我把我的后台拼接的代码贴上来。大家不要觉得我写的太差,你觉得不好请在评论区指出。


  import net.sf.json.JSONArray;
  import net.sf.json.JSONObject;
  import net.sf.json.JsonConfig;


   //
查看系统功能项(获取树形结构) public void showXtgnxList(){ Map<String, Object> params = new HashMap<String, Object>(); StringBuffer sql = new StringBuffer("select gnxbh,gnxmc,sjgnbh test_table where 1=1 "); JsonConfig config=new JsonConfig(); config.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl()); config.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessorImpl()); JSONArray arr = JSONArray.fromObject(baseDao.getByExample(sql.toString(),null,params).getList(),config); JSONObject targetObj = new JSONObject(); JSONObject footer = new JSONObject(); for(int i=0;i<arr.size();i++){ JSONObject json = (JSONObject) arr.get(i); if(json.get("SJGNBH").equals("-1")==false){ if(Utils.isNotEmpty(xtgnmc)){ }else{ json.put("_parentId", json.get("SJGNBH")); json.remove("SJGNBH"); } } } targetObj.put("total", arr.size()); targetObj.put("rows", arr); footer.put("name", "Total Persons:"); footer.put("persons", 7); footer.put("iconCls", "icon-sum"); targetObj.put("footer",footer); this.printToJson(targetObj.toString()); }

  这次就写到这里吧。

原文地址:https://www.cnblogs.com/sunziren/p/10522217.html