【问题标题】:[jsTree]: why the 'rename' and 'move' events are not fired with new nodes?[jsTree]:为什么新节点不会触发“重命名”和“移动”事件?
【发布时间】:2011-07-29 15:08:23
【问题描述】:

我正在将 jstree 用于网页中的树形视图。

树可以重命名和移动节点。移动或重命名节点会触发 rename_node.jstree 和 rename_node.jstree 事件。

使用新节点(使用rename_node.jstree events 创建),节点仍然可以重命名和移动,但不会触发 move_node.jstree 和 rename_node.jstree 事件。

似乎事件只与初始节点绑定。我没有看到任何将事件与之后创建的节点绑定的“实时”方法。

有没有可能这样做?

这是一个有助于(我希望)理解我的问题的示例:

 $(function(){
    $("#nodes").jstree({ 
        "plugins" : [ "themes", "html_data", "dnd", "ui", "crrm" ]
    }).bind("move_node.jstree", function (event, data) {
        alert('move');
    }).bind("rename_node.jstree", function (event, data) {
        alert('rename');
    }).bind("create_node.jstree", function (event, data) {
        alert('create_node');
    })

    $("#create_button").click(function () { 
        $("#nodes").jstree("create",null,"last",{data:"name"}); 
    });
});

【问题讨论】:

标签: javascript jquery events jstree


【解决方案1】:

我认为命令是create_node,而不是create。详情请见http://www.jstree.com/documentation/core。


仅供参考,您的代码最好写成:

$(function() {
    $("#nodes").jstree({
        "plugins": ["themes", "html_data", "dnd", "ui", "crrm"]
    }).bind("move_node.jstree rename_node.jstree create_node.jstree", function(event, data) {
        var type = event.type;
        alert(type);
        if (type === 'move_node.jstree') {
            //handle move_node.jstree here
        } else if (type === 'rename_node.jstree') {
            //handle rename_node.jstree here
        } else if (type === 'create_node.jstree') {
            //handle create_node.jstree here
        }

    });

    $("#create_button").click(function() {
        $("#nodes").jstree("create", null, "last", {
            data: "name"
        });
    });
});

我知道这是主观的,但要考虑它的价值。

【讨论】:

  • 感谢您的回答和建议。似乎即使使用 create_node,当我重命名新创建的节点时,我也没有收到 rename_node.jstree 事件。也许我的事件回调有问题,但我需要一点时间根据您的建议重构我的代码。
  • 想评论一下为什么您处理事件的方式是“正确”的方式?
【解决方案2】:

似乎事件被正确触发了。问题出在逻辑的某个地方。我也必须设置项目的 id 才能正确处理。

$("#nodes").jstree("create",null,"last",{data:the_name, attr: {id: the_id}});

抱歉这个错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多