The jsTree-based infinite tree JSON data conversion code. For more information, see. Jstree homepage:
Http://www.jstree.com/
It provides a form of rendering data from the background into a tree:
The Code is as follows:
$ ("# Mytree"). tree ({
Data :{
Type: "json ",
Url: "$ {ctx}/user/power! List. do"
}
});
The value returned by the url must be in the json data format defined by the url:
The Code is 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 "}
]
}
});
Here we need to convert a collection of backend instances to the json data format specified by it.
The Code is as follows:
/***//**
* Infinite recursion to obtain jsTree json strings
*
* @ Param parentId
* Parent permission id
* @ Return
*/
Private String getJson (long parentId)
{
// Check the top layer
List actions = actionManager. queryByParentId (parentId );
For (int I = 0; I <actions. size (); I ++)
{
Action a = actions. get (I );
// Subnodes exist.
If (a. getIshaschild () = 1)
{
Str + = "{attributes: {id: \" "+ a. getAnid ()
+ "\"}, State: \ "open \", data: \ "" + a. getAnname () + "\",";
Str + = "children :[";
// Find its subnode
List list = actionManager. queryByParentId (a. getAnid ());
// Traverse its subnodes
For (int j = 0; j <list. size (); j ++)
{
Action ac = list. get (j );
// There are also child nodes (recursive call)
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:
The Code is as follows:
@ Org. apache. struts2.convention. annotation. Action (results =
{@ Result (name = "success", location = "/main/user/action-list.jsp ")})
Public String list ()
{
String str = "[";
// Start with the root
Str + = this. getJson (0 );
Str + = "]";
This. renderJson (str );
Return null;
}
Action is an object of the menu or permission class.
: