JsTree-based infinite tree JSON data conversion code _ javascript skills

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

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.