Jstree上下文菜单supress已更改select_node上的事件右键单击

时间:2016-03-02 19:55:15

标签: javascript jquery contextmenu jstree

我试图找到一种方法可以在加载动态上下文菜单(右键单击)时抑制jstree中的changed事件。我知道您可以在上下文菜单中suppress the select_node event,但我需要获取我右键单击的节点的节点ID。 (因此需要使用select_node)。我知道您可以suppress that changed event定期致电select_node,但我不确定在点击右键时该怎么做。我使用上下文菜单select_node尝试了以下操作,但它不起作用:

$(function () {
    $('#myTree').jstree({
        "core": {
            "themes": {
                "variant": "small",
                "icons": false
            }
        },
        "contextmenu": {
            "items": reportMenu(node),      //builds context menu based on selected node
        },
        "plugins": ["contextmenu", "changed"]
    });
});
$("#myTree").bind('select_node.jstree', function (event, data) {
// Does not work, changed event still fires.
    $("#myTree").jstree().select_node(data.node.id, true);
});

我正在寻找其中一种可能的选择:

  1. 当上下文菜单调用changed
  2. 时,如何取消select_node事件
  3. 如何在不调用select_node事件的情况下获取我正确点击的节点的ID(即如果我将我的上下文菜单设置为'select_node': false,如何捕获选择节点)?

1 个答案:

答案 0 :(得分:5)

最后,我认为你可以得到你想要改变代码的东西。

检查演示 - codepen

$('#myTree')
    .jstree({
        'core': {
            'data': ...
        },
        'plugins': ["contextmenu"],
        'contextmenu': {
            'select_node': false,
            'items': reportMenu
        }
    });

function reportMenu(node) {
    // access node as: node.id);
    // build your menu depending on node id
    return {
        createItem: {
            "label": "Create New Branch",
            "action": function(obj) {
                this.create(obj);
                alert(obj.text())
            },
            "_class": "class"
        },
        renameItem: {
            "label": "Rename Branch",
            "action": function(obj) { this.rename(obj); }
        },
        deleteItem: {
            "label": "Remove Branch",
            "action": function(obj) { this.remove(obj); }
        }
    };
}
相关问题