在 jsTree 的上下文菜单中创建功能不起作用

Create Functionality in Context Menu of jsTree not working

定义 'types' 插件时不会创建新节点。

请看这个fiddle。 我无法在树中创建新节点。 http://jsfiddle.net/z8L5r9w3/1/

$('#jstree').jstree({
"core" : {
    "check_callback" : true,
    "data" : [
        { "text" : "Branch 1", "type" : "branch", "children" : [
            { "text" : "leaf 1.1", "type" : "leaf" },
            { "text" : "leaf 1.2", "type" : "leaf" },
            { "text" : "leaf 1.3", "type" : "leaf" }
           ]
        },
        { "text" : "Branch 2", "type" : "branch", "children" : [
            { "text" : "leaf 2.1", "type" : "leaf" },
            { "text" : "leaf 2.2", "type" : "leaf" },
            { "text" : "leaf 2.3", "type" : "leaf" }
           ]
        }
    ]
},
        "types" : {
            "#" : {
                "valid_children" : ["branch"]
            },
            "branch" : {
                "valid_children" : ["leaf"]
            },
            "leaf" : {
                "valid_children" : []
            }
        },
"plugins" : ["types", "dnd", "contextmenu"]});

您对 "types" 有疑问。 "contextmenu" 中的 "Create" 操作不知道 "branch" 和 "leaf" 的类型,并使用 "type" 创建新节点:"default"。 你可以看到这个:

        "types" : {
            "#" : {
                "valid_children" : ["branch", "default"]
            },
            "branch" : {
                "valid_children" : ["leaf", "default"]
            },
            "leaf" : {
                "valid_children" : []
            }
        },

此外,您可以覆盖 "contextmenu"

    "contextmenu":{
                "items": function () {
                    return {
                        "Create": {
                            "label": "Create",
                            "action": function (data) {
                                var ref = $.jstree.reference(data.reference);
                                sel = ref.get_selected();
                                if(!sel.length) { return false; }
                                sel = sel[0];
                                type = ref.get_type(sel);
                                if (type == "#")
                                    type = "branch";
                                else if (type == "branch")
                                    type = "leaf";
                                else if (type == "leaf")
                                    type = "";
                                sel = ref.create_node(sel, {text: "new "+type, type: type});
                                if(sel) {
                                    ref.edit(sel);
                                }

                            }
                        },
                        "Rename": {
                            "label": "Rename",
                            "action": function (data) {
                                var inst = $.jstree.reference(data.reference);
                                obj = inst.get_node(data.reference);
                                inst.edit(obj);
                            }
                        },
                        "Delete": {
                            "label": "Delete",
                            "action": function (data) {
                                var ref = $.jstree.reference(data.reference),
                                sel = ref.get_selected();
                                if(!sel.length) { return false; }
                                ref.delete_node(sel);

                            }
                        }
                    };
                }
            },