论坛首页 Web前端技术论坛

jquery-easyui中创建异步加载树

浏览 18038 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-27   最后修改:2010-01-27

easyui中的树可以从标记中建立,也可以通过指定一个URL属性读取数据建立。如果想建立一棵异步树,需要为每个节点指定一个id属性值,这样在加载数据时会自动向后台传递id参数。

 

<ul id="tt"></ul>

编写前台代码:

$('#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<Node> 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<String,Object>> items = new ArrayList<Map<String,Object>>();
        for(Node node: nodes){
            Map<String,Object> item = new HashMap<String,Object>();
            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://localhost:8080/demo2/

 

 

原文及代码下载地址:http://jquery-easyui.wikidot.com/tutorial:tree2

 

   发表时间:2010-01-27  
原来是这样,貌似tree的api不多哦,通常对树本身也要有curd操作的,还有刷新等,应该还有选择等等操作事件,不知道这个有没有!!!!
0 请登录后投票
   发表时间:2010-01-28   最后修改:2010-01-28
很好的代码,只是能加一些详细的说明吗?
0 请登录后投票
   发表时间:2010-01-31  
http://jquery-easyui.wikidot.com/
我登不上去啊,被墙了吗?
国人开发的吗,有点类似ext啊
0 请登录后投票
   发表时间:2010-01-31  
ActiveRecordBase是什么东东
0 请登录后投票
   发表时间:2010-02-01  
这个是etmvc框架中的ORM,请参见http://code.google.com/p/etmvc/
0 请登录后投票
   发表时间:2010-02-01  
看了下easyui中的tree插件,觉得支持的事件太少,比如常用的展开、搜索、遍历、复选框、双击等。能
否加入这些常用功能呢?
0 请登录后投票
   发表时间:2010-02-02  
树功能是少些,当初的目标只是提供解决树菜单的功能。下个版本再考虑改进。
0 请登录后投票
   发表时间:2010-03-08  
这棵树我是弄出来了。

但是如何在树节点上添加超链接啊?



0 请登录后投票
   发表时间:2010-03-08  
将节点的文本加上超连接就好。
如节点"text":"abc"
改成"text":"<a href='#'>abc</a>"
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics