jstree选择多个节点触发一个事件

时间:2014-10-20 12:27:39

标签: jstree

我有一个文件树,我正在尝试为其创建一个编辑器。选择一个节点时,将为该特定节点加载面板。如果选择了多个节点,则为所有选定节点加载更通用的面板。

问题是,当选择多个节点时,会为每个节点触发“select_node.jstree”。

一段相关代码...

$("#tree).on("select_node.jstree", function(event, node) {
    var selected = node.instance.get_selected();
    if(selected.length === 1) {
        $("#editor").load(url);
    } else if(selected.length > 1) {
        $.post(url, {
            data: selected
        }, function(res) {      
            $("#editor").html(res);
        });
    }
});

所以...如果我选择了5项我正在做1 GET和4 POSTS。

我要找的是1 GET(选择的第一个节点)和1 POST(所选节点的集合)......

这只是暂停吗?我觉得我错过了一些明显的东西。我不是一个优秀的程序员,所以任何方向都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

所以,我能够使用doTimeout库来管理它。它有效,不确定它是否是最佳的。

https://github.com/cowboy/jquery-dotimeout

$('#tree').on("select_node.jstree", function(event, node) {
    $.doTimeout('select', 500, function () {
        var selected = node.instance.get_selected();
        if(selected.length === 1) {
            $('#editor').load(url);
        } else if(selected.length > 1) {
           $.post(url, {
                data: selected
            }, function(res) {      
                $('#editor').html(res);
            });
        }
    });
});