javascript 如何在完整的 jstree reload 事件上绑定回调函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6451979/
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-10-25 20:43:30  来源:igfitidea点击:

How do I bind a callback function on the complete jstree reload event?

javascriptjqueryjstree

提问by John Doe

I have a button that reloads (resends an AJAX request) the jsTree once is clicked.

我有一个按钮,可以在单击 jsTree 后重新加载(重新发送 AJAX 请求)。

Here is a sample configuration code I have:

这是我拥有的示例配置代码:

treeContainer.bind("loaded.jstree", function () {
    alert("the tree is loaded");
}).jstree(config);

The problem I experience is that I do not have the alert (wrapped in a callback function) displayed once the 'reload' button is clicked the 2nd, 3rd, etc. times. Am I using the wrong jstree status event?

我遇到的问题是,在第二次、第三次等点击“重新加载”按钮后,我没有显示警报(包装在回调函数中)。我是否使用了错误的 jstree 状态事件?

To summarize, I want a jsTree callback function to be executed each time I click the 'reload' button.

总而言之,我希望每次单击“重新加载”按钮时都执行一个 jsTree 回调函数。

I am currently using jsTree 1.0-rc1 (rev. 191).

我目前正在使用jsTree 1.0-rc1 (rev. 191)

回答by dead10ck

Destroying the tree before building the new one works as well.

在构建新树之前摧毁树也有效。

treeContainer.jstree("destroy");
treeContainer.bind("loaded.jstree", function () {
    alert("the tree is loaded");
}).jstree(config);

回答by apfz

add this to the core:

将此添加到核心:

        reopen : function () {
            var _this = this;
            if(this.data.core.to_open.length) {
                $.each(this.data.core.to_open, function (i, val) {
                    _this.open_node(val, false, true); 
                });
            }
            this.__callback({});
            this.reopened();
        },

note that only this.reopened is added to the allready existing reopen-method. now create the reopened-method:

请注意,只有 this.reopened 被添加到已经存在的重新打开方法中。现在创建重新打开的方法:

        reopened : function () {
            this.__callback();
        },

now bind the new reopened-method to your tree-selector

现在将新的重新打开方法绑定到您的树选择器

}).bind("reopened.jstree", function (e,data) {
        alert("i am refreshed...");
    });

be carefull, because this alert-message will also be called when the tree is done loading. It is anyhow better, since you now have a way to have a callback when the tree is refreshed!

小心,因为当树完成加载时也会调​​用此警报消息。无论如何,它更好,因为您现在有一种方法可以在树刷新时进行回调!

hope this helps you all!

希望这对大家有帮助!

回答by András Szepesházi

From the jstree documentation:

jstree 文档

.loaded ()

A dummy function, whose purpose is only to trigger the loaded event. This event is triggered once after the tree's root nodes are loaded, but before any nodes set in initially_open are opened.

.loaded ()

一个虚拟函数,其目的只是触发加载的事件。在加载树的根节点之后,但在打开 initial_open 中设置的任何节点之前,会触发一次此事件。

So you can call this method from the success callback of your ajax call. Something like this:

因此,您可以从 ajax 调用的成功回调中调用此方法。像这样的东西:

$.ajax({
  url: "yourscript-url",
  success: function(){
    $('selector-for-jstree-container').jstree('loaded');
  }
});

See also the "Interacting with the tree" section on the same jstree documentation page.

另请参阅同一 jstree 文档页面上的“与树交互”部分。