jstree 凯发k8网页登录主页 :
http://www.jstree.com/
其中提供了一种从后台取数据渲染成树的形式:
$("#mytree").tree({
data : {
type : "json",
url : "${ctx}/user/power!list.do"
}
});
对于url中返回的值必须是它定义的json数据形式:
$("#demo2").tree({
data : {
type : "json",
json : [
{ attributes: { id : "pjson_1" }, state: "open", data: "root node 1", children : [
{ attributes: { id : "pjson_2" }, data: { title : "custom icon", icon : "../media/images/ok.png" } },
{ attributes: { id : "pjson_3" }, data: "child node 2" },
{ attributes: { id : "pjson_4" }, data: "some other child node" }
]},
{ attributes: { id : "pjson_5" }, data: "root node 2" }
]
}
});
这里需要一个从后台实例集合转换为它规定的json数据的形式.
/** *//**
* 无限递归获得jstree的json字串
*
* @param parentid
* 父权限id
* @return
*/
private string getjson(long parentid)
{
// 把顶层的查出来
list<action> actions = actionmanager.querybyparentid(parentid);
for (int i = 0; i < actions.size(); i)
{
action a = actions.get(i);
// 有子节点
if (a.getishaschild() == 1)
{
str = "{attributes:{id:\"" a.getanid()
"\"},state:\"open\",data:\"" a.getanname() "\" ,";
str = "children:[";
// 查出它的子节点
list<action> list = actionmanager.querybyparentid(a.getanid());
// 遍历它的子节点
for (int j = 0; j < list.size(); j)
{
action ac = list.get(j);
//还有子节点(递归调用)
if (ac.getishaschild() == 1)
{
this.getjson(ac.getparentid());
}
else
{
str = "{attributes:{id:\"" ac.getanid()
"\"},state:\"open\",data:\"" ac.getanname()
"\" " " }";
if (j < list.size() - 1)
{
str = ",";
}
}
}
str = "]";
str = " }";
if (i < actions.size() - 1)
{
str = ",";
}
}
}
return str;
}
调用:
@org.apache.struts2.convention.annotation.action(results =
{ @result(name = "success", location = "/main/user/action-list.jsp") })
public string list()
{
string str = "[";
// 从根开始
str = this.getjson(0);
str = "]";
this.renderjson(str);
return null;
}
其中action是菜单类或权限类等的实体。
效果图: