javascript - Jstree context menu supress changed event on select_node right click - Stack Overflow

Im trying to find a way I can suppress the changedevent in jstree when loading a dynamic context menu (

Im trying to find a way I can suppress the changedevent in jstree when loading a dynamic context menu (right click). I'm aware that you can suppress 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 when calling select_node regularly, but I'm not sure how to do it when right clicking. I tried the following with the context menu select_node, but it did not work:

$(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);
});

I'm looking for one of the possible alternatives:

  1. How can I suppress the changedevent when the context menu calls select_node?
  2. How can I get the id of the node I am right clicking on without calling the select_node event (i.e. If I set my contextmenu to 'select_node': false, how can I capture the select node)?

Im trying to find a way I can suppress the changedevent in jstree when loading a dynamic context menu (right click). I'm aware that you can suppress 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 when calling select_node regularly, but I'm not sure how to do it when right clicking. I tried the following with the context menu select_node, but it did not work:

$(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);
});

I'm looking for one of the possible alternatives:

  1. How can I suppress the changedevent when the context menu calls select_node?
  2. How can I get the id of the node I am right clicking on without calling the select_node event (i.e. If I set my contextmenu to 'select_node': false, how can I capture the select node)?
Share edited Mar 2, 2016 at 21:05 usr4896260 asked Mar 2, 2016 at 19:55 usr4896260usr4896260 1,5073 gold badges28 silver badges57 bronze badges 1
  • 1 By the way, why can't you get the node id as node.id inside your reportMenu function ? – Nikolay Ermakov Commented Mar 2, 2016 at 22:49
Add a ment  | 

1 Answer 1

Reset to default 6

Finally, I think you can get what you want changing your code a little.

Check demo - 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); }
        }
    };
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744808696a4594934.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信