Jstree 上下文菜单抑制 select_node 右键单击​​时更改的事件

Jstree context menu supress changed event on select_node right click

我正在尝试找到一种方法可以在加载动态上下文菜单(右键单击)时抑制 jstree 中的 changed 事件。我知道您可以在定期调用 select_nodesuppress the select_node event in the context menu but I need to get the node id of the node that I am right clicking on. (and therefore need to use the select_node). I know that you can suppress that changed event,但我不确定如何在右键单击时执行此操作。我用上下文菜单 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. 如何在上下文菜单调用 select_node 时抑制 changed 事件?
  2. 如何在不调用 select_node 事件的情况下获取我正在右键单击的节点的 ID(,即如果我将上下文菜单设置为 'select_node': false,我如何才能捕获 select 节点)?

最后,我想你可以得到你想要的,稍微改变你的代码。

检查演示 - 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); }
        }
    };
}