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是菜单类或权限类等的实体。
效果图:
分享到:
相关推荐
基于jsTree的无限级树JSON数据的转换代码.docx
基于jsTree的无限级树JSON数据的转换代码,需要的朋友可以参考下。
js无限级树形菜单js无限级树形菜单js无限级树形菜单
JS无限级树形菜单3种
将js对象转换成json格式的数据的字符串,方便后台进行转换处理
基于内存多叉树的Ext JS无限级树形菜单实现方案,一种构建多级JSON的方法;一次性构造多级JSON字符串,一次性生成树形菜单。
摘要:脚本资源,Ajax/JavaScript,Tree,树形菜单,无限级树 一个基于jQuery的无限级树代码,MythTree v1.0的代码及实例,涉及到的函数讲解(JQuery方式): $(自定义标签).mythTree(); 默认可不传值。插件会根据自己...
例程可正常使用,非常的简单,基本上学过几天代码的人都能够正常使用。采用纯js编写,不复杂
AienTree-艾恩JS无限级菜单树,典型树形菜单例子,很好的
一个用于将JSON数据转换为Table的JavaScript库
使用JQuery实现从JSON对象转换为form提交数据
可以直接将JSON数据生成表格 生成的表格可以是HTML字符串 也可以是OBJECT 操作简单,三步即可实现数据转换成表格。
主要为大家详细介绍了基于jstree使用JSON数据组装成树,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
struts2 jstree json_data实现异步加载 定位
js树形菜单组件,主要解决大数据加载节点卡死问题。
原生JS写的无限级树形菜单(包含文件:otree.js,otree.css,demo.html,demo1.html,imgs/ ),兼容IE6/7/8/9+、Chrome、Firefox、Opera、Safari等主流浏览器,以及360/搜狗等其他各浏览器。 可动态添加/删除节点,启用/...
json转换json schema
在js中通过递归的方法将List与Tree(树形数据)互相转换