Conversion code _javascript technique of infinite-level tree JSON data based on Jstree

Source: Internet
Author: User
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.