jsTree加载并单击

时间:2011-11-14 11:01:37

标签: javascript jquery jstree

我有两个关于jsTree的问题:

  1. 我正在使用带有struts2的JSON生成一个jsTree。第一次树正确显示。但是在按钮上单击我想将具有不同数据的树重新加载到同一个div中。在按钮上单击我给出相同的动作名称,这是第一次生成jstree。

  2. 如果我点击树的任何节点,那么我想要该节点的id,如果该节点有任何父节点,那么还需要该节点的父节点id再次父节点具有父节点,那么id为父节点也

  3. 实施例

        Parent 1
           |__Parent 2
                |__Child 1
    

    我点击了Child 1然后我想要孩子1,父母2,父母1的id等等。

2 个答案:

答案 0 :(得分:3)

我不确定你是否真的浏览了这些文档,但这里有一些可以帮到你的东西:

对于您的第一个问题,您可以使用回滚:http://luban.danse.us/jazzclub/javascripts/jquery/jsTree/reference/_examples/5_others.html 刷新也是一种选择:http://luban.danse.us/jazzclub/javascripts/jquery/jsTree/reference/_examples/2_operations.html

对于第二个问题,您可以使用回调:http://luban.danse.us/jazzclub/javascripts/jquery/jsTree/reference/_examples/3_callbacks.html

._get_parent也会对您有所帮助:

  

._ get_parent(node)

     

获取表示传递节点的父元素的LI元素。   失败时返回false。混合节点

     

这可以是指向元素的DOM节点,jQuery节点或选择器   在树中,我们想要的父母。

有关详细信息,请参阅完整文档:http://www.jstree.com/documentation/core

答案 1 :(得分:1)

如果要获取节点的完整路径,可以使用get_path

// The true means it will return the ID's of the parent, therefore ALL NODES need an ID
var parents = $("#your_jstree").jstree("get_path",$("#the_node"),true); 
$.each(parents, function(k, v){
    // Log down the ID's
    console.log(v);
});

完整示例

var ids = $(this).closest(".jstree").jstree("get_path", $(this), true);
var path = "";
$.each(ids, function (k, v) {
    if (k == 0) return;

    if (path != "")
        path += "/";

    path += clone.html();
});

将返回/父1 /父2 /子1