作者:hwj3747 转载请注明
说明
前端使用的是easyUI 1.3.6+jq 后台使用的是springMVC 初始化调用后台getCourse方法,返回的json数据格式如下:
{"total":3,"rows":[
{"id":1,"typeName":"All Tasks","description":"3/4/2010","sort":"3/20/2010"},
{"id":2,"typeName":"All Tasks","description":"3/4/2010","sort":"3/20/2010","_parentId":1},
{"id":3,"typeName":"All Tasks","description":"3/4/2010","sort":"3/20/2010","_parentId":2}]
}
保存一行数据,用js取得row的数据,再转化为json,调用saveCourseType传到后台。 删除一行,取得行id,调用deleteCourseType删除数据。
前端代码
CheckBox Tree - jQuery EasyUI Demo
可编辑的表格树
//菜单栏
//treegrid主表
//右键菜单选项
JS代码
后台java代码
//获取treegrid列表
@RequestMapping(value = "/getCourseType")
public @ResponseBody Map tree_data(HttpServletRequest request) throws Exception {
String parentId = ConvertUtils.cStr(request.getParameter("parentId"));
Map param = new HashMap();
List courseTypes = new ArrayList();
if(StringUtils.isNotEmpty(parentId)){
courseTypes = courseTypeService.queryTree(null, parentId,null);
} else {
courseTypes = courseTypeService.queryTree(null, null,"1");
}
List> dateList = new ArrayList>();
if(courseTypes.size()>0){
Map map = null;
Map params = new HashMap();
for(CourseType courseType:courseTypes){
map = new HashMap();
map.put("id", courseType.getId());
map.put("typeName", courseType.getTypeName());
map.put("sort", courseType.getSort());
map.put("description", courseType.getDescription());
if(StringUtils.isEmpty(courseType.getParentId())){
map.put("_parentId", null);
}
else{
map.put("_parentId", courseType.getParentId());
}
//根据id判断是否有子节点
params.put("parentId", courseType.getId());
int count = courseTypeService.querySelectRowCount(params);
if(count>0){
map.put("state", "closed");
}
dateList.add(map);
}
}
param.put("rows", dateList);
return param;
}
//保存treegrid的某一列
@RequestMapping(value = "/saveCourseType", method = RequestMethod.POST)
public @ResponseBody int saveDirectory(HttpServletRequest request) throws Exception {
Map params = CommonUtil.getNewSqlMap();
String rows = ConvertUtils.cStr(request.getParameter("rows"));
Map map=JsonParser.parseJsonStrNull(rows);
params.put("id",map.get("id"));
params.put("typeName",map.get("typeName"));
params.put("description", map.get("description"));
params.put("parentId", map.get("_parentId"));
params.put("sort", map.get("sort"));
params.put("delFlag", 0);
String parentIds=map.get("_parentId")+",";
String parentId=map.get("_parentId");
while(true){
parentId=courseTypeService.findParentId(parentId);
if(StringUtils.isEmpty(parentId)){
break;
}
parentIds=parentIds+parentId+",";
}
params.put("parentIds", parentIds);
if(courseTypeService.updateObject(params)==0){
courseTypeService.saveObject(params);
}
return 1;
}
//删除某一行
@RequestMapping(value = "/deleteCourseType")
public @ResponseBody Map deleteDirectory(HttpServletRequest request) throws Exception {
String id=ConvertUtils.cStr(request.getParameter("id"));
Map result = new HashMap();
Map params = CommonUtil.getNewSqlMap();
params.put("id",id);
courseTypeService.deleteObject(params);
return result;
}
注意事项
1.后台传回的json数据的key,必须与前端treegrid的表头name相对应。 2.要使用编辑功能,前端表头必须加入editor:'text'参数,具体类型参见官方文档。 3.后台传回的参数如果包含state参数,state为closed代表该节点具有子节点。 4.后台传回的json数据必须有一个以上是不包含_parentId字段的,也就是必须要有父节点,否则无法显示。