Jstree Home:
http://www.jstree.com/
This provides a way to render the data from the background into a tree:
Copy Code code as follows:
$ ("#mytree"). Tree ({
Data: {
Type: "JSON",
URL: "${ctx}/user/power!list.do"
}
});
The value returned in the URL must be the form of the JSON data it defines:
Copy Code code as follows:
$ ("#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"}
]
}
});
This requires a form of conversion from the background instance collection to its specified JSON data.
Copy Code code as follows:
/** *//**
* Infinite recursion to get jstree JSON string
*
* @param parentid
* Parent Permission ID
* @return
*/
Private String Getjson (Long ParentID)
{
Find the top floor.
list<action> actions = Actionmanager.querybyparentid (ParentID);
for (int i = 0; i < actions.size (); i++)
{
Action a = Actions.get (i);
Has child nodes
if (a.getishaschild () = = 1)
{
str = "{attributes:{id:\" "+ A.getanid ()
+ "\"},state:\ "open\", data:\ "" + a.getanname () + "\";
STR + + "children:[";
Isolate its child nodes.
list<action> list = Actionmanager.querybyparentid (A.getanid ());
Traversal of its child nodes
for int j = 0; J < List.size (); j + +)
{
Action AC = List.get (j);
There are also child nodes (recursive calls)
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;
}
Call:
Copy Code code as follows:
@org. apache.struts2.convention.annotation.Action (results =
{@Result (name = "Success", location = "/main/user/action-list.jsp")})
Public String list ()
{
String str = "[";
Start from Root
str = This.getjson (0);
str = + "]";
This.renderjson (str);
return null;
}
Where action is an entity such as a menu class or permission class.
Effect Chart: