简体   繁体   English

jsTree Contextmenu获取选定的节点ID

[英]jsTree Contextmenu get selected node id

Hi I am currently having an issue with the jsTree Context menu. 嗨,我目前在jsTree上下文菜单上遇到问题。 I would like to grab the id of the selected node however I've looked at a number of solutions and none have worked. 我想获取所选节点的ID,但是我查看了许多解决方案,但没有一个起作用。 My code is below. 我的代码如下。

The function I have which uses tree.get_selected($node) does return an object of the selected node but I'm struggling to access the id parameter of the object. 我使用tree.get_selected($node)函数确实返回了所选节点的对象,但是我正在努力访问该对象的id参数。 Below I used tree.get_selected($node).id but it says it's undefined but its clearly not when I look at the console. 在下面,我使用tree.get_selected($node).id但它说它是undefined但是当我看控制台时,它显然不是。

Can anyone help as to where I'm going wrong? 谁能帮我解决我的问题?

$("#tree_1").jstree({
    "core" : {
    "themes" : {
        "responsive": false
    },
    // so that create works
    "check_callback" : true,
    'data' : {
        'url' : function (node) {
                if(node.id=='#') {
                    var id = 0; 
                } else {
                    var id = node.id;   
                }
                return 'organisation/getOrganisationLocations/'+id;
            }
        }
    },
    "types" : {
        "default" : {
            "icon" : "fa fa-folder icon-state-warning icon-lg"
        },
        "file" : {
            "icon" : "fa fa-file icon-state-warning icon-lg"
        }
    },
    "state" : { "key" : "demo3" },
    "plugins" : [ "dnd", "state", "types", "contextmenu" ],
    "contextmenu":{         
    "items": function($node) {
            var tree = $("#tree_1").jstree(true);
            return {
                "Create": {
                    "separator_before": false,
                    "separator_after": false,
                    "label": "Create",
                    "action": function (obj) { 
                        $node = tree.create_node($node);
                        tree.edit($node);

                        console.log(tree.get_selected($node).id);
                    }
                },
                "Rename": {
                    "separator_before": false,
                    "separator_after": false,
                    "label": "Rename",
                    "action": function (obj) { 
                        tree.edit($node);
                    }
                },
                "Remove": {
                    "separator_before": false,
                    "separator_after": false,
                    "label": "Remove",
                    "action": function (obj) { 
                        tree.delete_node($node);
                    }
                }
            };
        }
    }
});

这种方法对我有用:

$(node).attr('id')

Declare your id above the jstree declaration (at line 1, put var id;) 在jstree声明上方声明您的ID(在第1行,输入var id;)

Then, in the data.url function, remove var from before each line that assignes to id. 然后,在data.url函数中,从分配给id的每一行之前删除var。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM