java实现树 tree

$.ajax({
url: “/common/getorgtree”,
type: “GET”,
dataType: “JSON”,
success: function(data){
return $("#orgcode").treeview({
data:data,
color:"#428bca",
levels:2,
onNodeSelected:function(event,node){
console.log(“选中了”);
},
onNodeUnselected:function(event,node){
console.log(“取消了”);
}
});},
error: function(XMLHttpRequest, textStatus, errorThrown){
if (XMLHttpRequest.status == 550) {
var exp = JSON.parse(XMLHttpRequest.responseText);
showMsg(i18n(exp[0].message));
}
}
});

后台封装数据

public static String getOrgcodeTree() {
	// 取得数据
	String sql = "select * from xxx ";
	Query query = JPA.em().createNativeQuery(sql, Orgrecord.class);
	ResponseDto orginfos = null;
	List resultList = query.getResultList();
	List lis = new ArrayList();
	for (Orgrecord list : resultList) {
		OrginTree orginTree = new OrginTree();
		orginTree.text = list.orgname;
		orginTree.parentorgname = list.parentorgname;
		lis.add(orginTree);
	}
	// 一级菜单
	List menuslist = new ArrayList();
	for (OrginTree list : lis) {
		if (list.parentorgname == null || "".equals(list.parentorgname)) {
			menuslist.add(list);
		}
	}
	for (OrginTree orginTree : menuslist) {
		orginTree.nodes = buildTree(lis, orginTree.text);
	}
	List build = build(menuslist);
	return JSONArray.toJSONString(build);
	}

public static List build(List list) {
	List menus2 = new ArrayList();
	for (OrginTree orginTree : list) {
		if (orginTree.nodes != null && orginTree.nodes.size() > 0) {
			build(orginTree.nodes);
			menus2.add(orginTree);
		}
	}
	return menus2;
}

public static List buildTree(List list,
		String parentorgname) {
	List menus = new ArrayList();
	for (OrginTree menu : list) {
		if (menu.parentorgname.equals(parentorgname)) {
			menus.add(menu);
		}
	}
	for (OrginTree orginTree : menus) {
		orginTree.nodes = buildTree(list, orginTree.text);
	}
	if (menus.size() == 0) {
		return null;
	}
	return menus;
}

你可能感兴趣的:(java实现树 tree)