其中提供了一種從后臺取數據渲染成樹的形式: 復制代碼 代碼如下: $("#mytree").tree({ data : { type : "json", url : "${ctx}/user/power!list.do" } }); 對于url中返回的 " /> 中文在线资源,国产最新精品,亚洲精品资源美女情侣酒店

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

基于jsTree的無限級樹JSON數據的轉換代碼

jstree 主頁 :
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是菜單類或權限類等的實體。
效果圖:

JavaScript技術基于jsTree的無限級樹JSON數據的轉換代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 南昌县| 方正县| 天津市| 香港 | 泉州市| 凯里市| 陵川县| 万安县| 东乡族自治县| 贺州市| 济南市| 墨脱县| 固原市| 兴安县| 唐海县| 三原县| 鄂托克旗| 蓬安县| 大同县| 韩城市| 沅陵县| 河间市| 和林格尔县| 金塔县| 措美县| 丰台区| 伊通| 景德镇市| 绥芬河市| 兴城市| 吐鲁番市| 乌什县| 江口县| 厦门市| 满洲里市| 沂南县| 大港区| 吴江市| 长寿区| 花莲市| 徐州市|