jQuery 为什么 jsTree open_all() 对我不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3585527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:42:42  来源:igfitidea点击:

Why doesn't jsTree open_all() work for me?

jqueryjstree

提问by seneyr

Started playing around with jQuery and the jsTree plugin yesterday, and have it successfully loading the tree with an AJAX call to a servlet. Now, I would like to have the tree open all the nodes after loading so I added a success function to the ajax attribute. However, I cannot seem to get the open_all() method to work properly. I'm very new to working with jQuery so I'm guessing it's something simple that I'm doing wrong.

昨天开始玩弄 jQuery 和 jsTree 插件,并让它通过对 servlet 的 AJAX 调用成功加载树。现在,我想让树在加载后打开所有节点,所以我向 ajax 属性添加了一个成功函数。但是,我似乎无法让 open_all() 方法正常工作。我对使用 jQuery 很陌生,所以我猜这很简单,我做错了。

Firebug isn't showing any errors which rules out the dumb error of mistyped method name. I checked the documentation and I think I'm doing everything correctly according to what I read. The tree is loading correctly, but not opening all the nodes after the page loads.

Firebug 没有显示任何排除错误输入方法名称的愚蠢错误的错误。我检查了文档,我认为根据我阅读的内容,我所做的一切都是正确的。树正确加载,但在页面加载后未打开所有节点。

I'm using jQuery 1.4.2 and jsTree 1.0rc2 on Firefox 3.6.8.

我在 Firefox 3.6.8 上使用 jQuery 1.4.2 和 jsTree 1.0rc2。

Here's the code I'm using to load the tree and attempt to open all the nodes in the tree:

这是我用来加载树并尝试打开树中所有节点的代码:

// Create the tree object
$("td#modelXML").jstree({
    core : { "animation" : 0 },
    //xml_data : {"data" : "" + xml, "xsl" : "nest"},
    xml_data : {"ajax" : 
                    {"url" : "servlet/GetModelHierarchy", 
                    "type" : "post", "data" : { modelId : "" + modelId} }, 
                    "xsl" : "nest",
                    "success" : function(){
                                $(this).open_all(-1);
                                }
    },
    themes : {"theme" : "classic", "dots" : true, "icons" : true},
    types : { 
        "types" : {
            "category" : {
                "valid_children" : ["factor"]
            },
            "factor" : {
                "valid_children" : ["level"]
            },
            "level" : {
                "valid_children" : "none",
                "icon" : {
                    "image" : "${request.contextPath}/jsTree/file.png"
                }
            }
        }
    },
    plugins : ["themes", "types", "xml_data"]
});

回答by GiddyUpHorsey

You have to hook into the events, and then call open_all.

您必须挂钩事件,然后调用open_all.

To have all nodes open on load, use:

要在加载时打开所有节点,请使用:

    var tree = $("#id-or-selector-for-my-tree-element");
    tree.bind("loaded.jstree", function (event, data) {
        tree.jstree("open_all");
    });

Do the above, before you initialize the tree with .jstree({...}).

在使用.jstree({...}).

If you refresh it, then to have all nodes open again, you have to use:

如果刷新它,那么要再次打开所有节点,您必须使用:

    tree.bind("refresh.jstree", function (event, data) {
        tree.jstree("open_all");
    });

回答by Codesleuth

Yes, this is an old question, but with no accepted answer and having the only answer not being useful to me, here's my answer which I now use:

是的,这是一个老问题,但没有被接受的答案,唯一的答案对我没有用,这是我现在使用的答案:

var tree = $("td#modelXML")
    .bind("loaded.jstree", function (e, data) {
        data.inst.open_all(-1); // -1 opens all nodes in the container
    })
    .jstree({ /* your jsTree options as normal */ });

The key point here is that data.instis your jsTree, and is the only reference you will have available because treewon't have a value until .jstree({finishes. Since loaded.jstreeis called within the .jstree({call, the result will not yet exist. See?

这里的关键点data.inst是您的jsTree,并且是您唯一可用的参考,因为tree.jstree({完成之前不会有值。由于loaded.jstree.jstree({调用中调用,结果尚不存在。看?

回答by SlinQ

I was completely unable to get it to work either with tree.jstree('open_all')or data.inst.open_all(-1)- in the end I had to use data.instance.open_all()- notice the change from inst to instance, and open_all(-1) to just open_all() - both of those seem to be required with jQuery 1.11 and jstree 3.0.0. My final code block looks like this:

我完全无法让它与tree.jstree('open_all')data.inst.open_all(-1)- 最后我不得不使用data.instance.open_all()- 注意从 inst 到 instance 的变化,以及 open_all(-1) 到 open_all() - 这两个似乎都是 jQuery 所必需的1.11 和 jstree 3.0.0。我的最终代码块如下所示:

$(document).ready(function() {
    var tree = $('#jstree');
    tree.bind('loaded.jstree', function(event, data) {
        data.instance.open_all();   
    });
    tree.jstree({});
});

回答by guest

Try this!

尝试这个!

$("td#modelXML").jstree("open_all","#nodeID");