如何在完整的 jstree 重新加载事件上绑定回调函数?

发布于 2024-11-16 22:43:02 字数 372 浏览 6 评论 0原文

我有一个按钮,一旦单击就会重新加载(重新发送 AJAX 请求)jsTree。

这是我的示例配置代码:

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

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

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

我目前正在使用jsTree 1.0-rc1(修订版191)

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

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?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伴我老 2024-11-23 22:43:02

在建造新树之前摧毁这棵树也是可行的。

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

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);
一指流沙 2024-11-23 22:43:02

将其添加到核心:

        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();
        },

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

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

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

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

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

希望这对大家有帮助!

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:

        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!

无畏 2024-11-23 22:43:02

我知道这是一个老问题,但我在 jsTree 3.3.4 中遇到了同样的问题。有效的解决方案是:

treeContainer.bind("refresh.jstree", function (event, data) {
    alert("the tree has been refreshed");
    $(this).jstree("open_all");  //Example: open all nodes after refreshing
})    

I know it's an old question, but I ran into the same problem with jsTree 3.3.4. Solution that works is:

treeContainer.bind("refresh.jstree", function (event, data) {
    alert("the tree has been refreshed");
    $(this).jstree("open_all");  //Example: open all nodes after refreshing
})    
甜心小果奶 2024-11-23 22:43:02

来自 jstree 文档

.loaded()

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

因此,您可以从 ajax 调用的成功回调中调用此方法。类似这样的内容:

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

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

From the jstree documentation:

.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.

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

$.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文