java 构造easyui tree 的返回值json
public @ResponseBody List formatTree(HttpServletRequest request,HttpServletResponse response) throws Exception {
List cityList =cityTreeService.getCityTreeList();
Map map = new HashMap();
List treelist = new ArrayList(); //返回json
map.put("id", cityList.get(0).getId()); //父节点
map.put("text", cityList.get(0).getName());
//map.put("parentid", cityList.get(0).getParentid());
List children = new ArrayList();
for (int i=1; i
CityName e = cityList.get(i);
if (cityList != null && cityList.size() > 0){
if(e.getParentid().equals(cityList.get(0).getId())){
Map childmap = new HashMap();
childmap.put("id", e.getId());
childmap.put("text", e.getName());
childmap.put("sort", e.getSort());
childmap.put("slevel", "市");
childmap.put("updatedate",new DateUtil().getDateStrCompact(new DateUtil().addDate(e.getUpdatetime(), 2),"yyyy-MM-dd"));
//childmap.put("parentid", e.getParentid());
List childrens = new ArrayList(); //添加子节点的孩子节点
for (int j=1; j
CityName c = cityList.get(j);
if(c.getParentid().equals(e.getId())){
Map childmaps = new HashMap();
childmaps.put("id", c.getId());
childmaps.put("text", c.getName());
childmaps.put("sort", c.getSort());
childmaps.put("slevel", "区镇");
childmaps.put("updatedate",new DateUtil().getDateStrCompact(new DateUtil().addDate(c.getUpdatetime(), 2),"yyyy-MM-dd"));
//childmaps.put("parentid", c.getParentid());
childrens.add(childmaps);
}
}
if(childrens.size()>0){
childmap.put("state","closed"); //state:节点状态, 'open' 或 'closed',默认是 'open'。
childmap.put("children", childrens);
}
children.add(childmap);
}
}
}
map.put("children", children);
treelist.add(map);
return treelist;
}