为什么jsTree open_all()不能为我工作?

sen*_*eyr 21 jquery jstree

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

Firebug没有显示任何错误,这些错误排除了输入错误的方法名称的愚蠢错误.我检查了文档,我认为我正在按照我所读的内容正确地完成所有工作.树正确加载,但在页面加载后没有打开所有节点.

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

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

// 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"]
});
Run Code Online (Sandbox Code Playgroud)

Gid*_*sey 39

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

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

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

在使用初始化树之前,请执行上述操作.jstree({...}).

如果刷新它,然后再次打开所有节点,则必须使用:

    tree.bind("refresh.jstree", function (event, data) {
        tree.jstree("open_all");
    });
Run Code Online (Sandbox Code Playgroud)


Cod*_*uth 20

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

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 */ });
Run Code Online (Sandbox Code Playgroud)

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

  • +1这种方法对我有用,接受的答案没有. (3认同)