Jstree上下文菜单在select_node上单击右键更改事件

usr*_*260 1 javascript jquery contextmenu jstree

我试图找到一种方法,我可以changed在加载动态上下文菜单(右键单击)时抑制jstree中的事件.我知道您可以在上下文菜单中禁止select_node事件,但我需要获取我右键单击的节点的节点ID.(因此需要使用select_node).我知道你可以在定期打电话时压制这个改变的事件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);
});
Run Code Online (Sandbox Code Playgroud)

我正在寻找可能的替代方案之一:

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

Nik*_*kov 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); }
        }
    };
}
Run Code Online (Sandbox Code Playgroud)