easyui中的树可以从标记中建立,也可以通过指定一个url属性读取数据建立。如果想建立一棵异步树,需要为每个节点指定一个id属性值,这样在加载数据时会自动向后台传递id参数。本文主要介绍解析jquery easyui tree异步加载子节点问题,easyui中的树可以从标记中建立,也可以通过指定一个url属性读取数据建立,有兴趣的可以了解一下。希望能帮助到大家。
登录后复制
编写前台代码:
$('#tt').tree({ url:'/demo2/node/getNodes' // The url will be mapped to NodeController class and getNodes method});
登录后复制
为测试用,建立一个节点的数据模型:
@Table(name="nodes")public class Node extends ActiveRecordBase{ @Id public Integer id; @Column public Integer parentId; @Column public String name; public boolean hasChildren() throws Exception{ long count = count(Node.class, "parentId=?", new Object[]{id}); return count > 0; }}
登录后复制
编写后台的控制器代码:
public class NodeController extends ApplicationController{ /** * get nodes, if the 'id' parameter equals 0 then load the first level nodes, * otherwise load the children nodes * @param id the parent node id value * @return the tree required node json format * @throws Exception */ public View getNodes(int id) throws Exception{ List nodes = null; if (id == 0){ // return the first level nodes nodes = Node.findAll(Node.class, "parentId=0 or parentId is null", null); } else { // return the children nodes nodes = Node.findAll(Node.class, "parentId=?", new Object[]{id}); } List<Map> items = new ArrayList<Map>(); for(Node node: nodes){ Map item = new HashMap(); item.put("id", node.id); item.put("text", node.name); // the node has children, // set the state to 'closed' so the node can asynchronous load children nodes if (node.hasChildren()){ item.put("state", "closed"); } items.add(item); } return new JsonView(items); }}
登录后复制
官网例子地址:http://www.jeasyui.com/tutorial/tree/tree2.php
demo下载:easyui-tree2_jb51.rar
重要的事情说三遍!!!
$('#tt').tree({ method:"POST", url:'/demo2/node/getNodes' // The url will be mapped to NodeController class and getNodes method});
登录后复制
method一定要用POST,GET的话要在URL后面用一个变量来做时间戳处理。
相关推荐:
关于jQuery插件zTree实现删除树节点方法详解
jQuery插件zTree实现的多选树效果实例讲解
jquery ztree实现右键收藏功能实例详解
以上就是jquery easyui tree异步加载子节点详解的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2789599.html