【问题标题】:Jstree context menu supress changed event on select_node right clickJstree 上下文菜单抑制 select_node 右键单击​​上的更改事件
【发布时间】:2016-03-02 19:55:15
【问题描述】:

我试图找到一种方法可以在加载动态上下文菜单(右键单击)时抑制 jstree 中的 changedevent。我知道您可以在上下文菜单中suppress the select_node event,但我需要获取我右键单击的节点的节点 ID。 (因此需要使用select_node)。我知道您可以在定期调用select_nodesuppress 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,我如何捕获选择节点 )?

【问题讨论】:

  • 顺便说一句,为什么不能在reportMenu 函数中获取节点ID 为node.id

标签: javascript jquery contextmenu jstree


【解决方案1】:

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

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

【讨论】:

  • 我最初尝试过这种方法,但不幸的是,我需要捕获选中的节点,以便构建上下文菜单。知道如何实现这一点,我将 select_node 设置为 false 吗?
  • 实际上您在上面的评论中是正确的。显然,即使 select_node 为假,我仍然可以捕获 node.id。感谢您的澄清!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
相关资源
最近更新 更多