页面:
$(function(){
$('#myTree').tree({
url:"rest/organizationRestFul/getJson",
loadFilter: function(data){
return data;
},
onLoadSuccess: function (node, data) {
// var node = $('#myTree').tree('find',1);
//$("#myTree").tree("collapseAll", node.target);
var roots = $("#myTree").tree("getRoots");
if (roots.length > 0) {
$.each(roots, function (i, item) {
var node = $('#myTree').tree('find', item.id);
$("#myTree").tree("collapseAll", node.target);//树加载默认时折叠状态
});
}
}
});
});
java后台:
新增实体类:
package com.gwm.pur.cdata.organization.entity;
import java.util.List;
import java.util.Map;
public class easyuiTree {
// 菜单id
private String id;
// 要显示的节点文本
private String text;
// 节点状态,'open' 或 'closed',默认是 'open'。当设置为 'closed' 时,该节点有子节点,并且将从远程站点加载它们。
private String state;
// 指示节点是否被选中。
private String checked;
// 给一个节点添加的自定义属性
private Mapattributes;
// 子菜单
private Listchildren;
//父节点
private String parentId;
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getChecked() {
return checked;
}
public void setChecked(String checked) {
this.checked = checked;
}
public MapgetAttributes() {
return attributes;
}
public void setAttributes(Mapattributes) {
this.attributes = attributes;
}
public ListgetChildren() {
return children;
}
public void setChildren(Listchildren) {
this.children = children;
}
}
restful层
@RequestMapping(value = "/getJson", method = RequestMethod.POST,produces = "text/html;charset=utf-8")
public @ResponseBody String getJson(HttpServletRequest request){
try{
//接收树上传来的ID参数(异步加载时会返回id)
String id = request.getParameter("id");
OrganizationEntity query = new OrganizationEntity();
if(id!=null&&!id.equals("")){
query.setId(Long.parseLong(id));
}
ListorganizationList = organizationService.findList(query,null);
ListmenuList = new ArrayList(); // 树递归
ListtotalList = new ArrayList(); // 树递归
// 先找到所有的一级菜单
for (int i = 0; i < organizationList.size(); i++) {
easyuiTree menu = new easyuiTree();
menu.setId(organizationList.get(i).getId().toString());
menu.setText(organizationList.get(i).getName());
menu.setParentId(organizationList.get(i).getParentId().toString());
//menu.setState("closed");
totalList.add(menu);
// 一级菜单父ID为0
if (organizationList.get(i).getParentId()==0) {
menuList.add(menu);
}
}
// 为一级菜单设置子菜单,getChild是递归调用的
for (easyuiTree menu : menuList) {
menu.setChildren(getChild(menu.getId(), totalList));
}
/// MapjsonMap = new HashMap();
// jsonMap.put("menu", menuList);
// System.out.println(JSON.toJSONString(jsonMap));
String list = JsonHelper.encodeObject2Json(menuList, "yyyy-MM-dd HH:mm:ss").toString();
return list;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* 子菜单递归
* @param id
* @param rootMenu
* @return
*/
private static ListgetChild(String id, ListrootMenu) {
// 子菜单
ListchildList = new ArrayList();
for (easyuiTree menu : rootMenu) {
// 遍历所有节点,将父菜单id与传过来的id比较
if (menu.getParentId().equals(id)) {
childList.add(menu);
}
}
// 把子菜单的子菜单再循环一遍
for (easyuiTree menu : childList) {
menu.setChildren(getChild(menu.getId(), rootMenu));// 递归
}
// 判断递归结束
if (childList.size() == 0) {
return null;
}
return childList;
}