jquery 在Jstree中创建新节点

vlju58qv  于 2023-06-05  发布在  jQuery
关注(0)|答案(2)|浏览(155)

我一直在尝试创建一个简单的页面,上面有一个jstree,允许创建新的节点。我实现了创建树,我可以在页面中看到它,但是当我尝试创建一个新节点时,我只是得到了一个“false”作为创建节点的指令的结果。
我在网上看过一些例子,但我找不到问题所在。
有人能帮帮我吗
这里是整个代码

<!DOCTYPE html>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-cale=1.0">
        <link rel="stylesheet" href="content/style.css" />
        <script src="script/jquery-1.11.3.min.js" type="text/javascript"></script>
        <script src="script/jstree.min.js" type="text/javascript"></script>
    </head>
    <body>
        <div id="jstree-div"></div>
    </body>

    <script type="text/javascript">
            $('#jstree-div').jstree({
                'core': {
                    'data': [
                       'Simple root node',
                       {
                           'text': 'Root node 2',
                           'state': {
                               'opened': true,
                               'selected': true
                           },
                           'children': [
                                { 'text': 'Child 1' },
                                'Child 2'
                            ]
                       }
                    ]
                },
                'plugins': ['contextmenu'],
                'contextmenu': {
                    'items': function($node) {
                        var tree = $("#jstree-div").jstree(true);
                        return {
                            "Create": {
                                'label': 'Crear',
                                "action": function (data) {
                                    var ref = $.jstree.reference(data.reference);
                                    sel = ref.get_selected();
                                    if (!sel.length) { return false; }
                                    sel = sel[0];
                                    sel = ref.create_node(sel, { "text": "New node" }, 'last');
                                    if (sel) {
                                        ref.edit(sel);
                                    }
                                }
                            }
                        }
                    }
                }
            });
    </script>
</html>
wljmcqd8

wljmcqd81#

问题是您不允许修改配置中的结构,请添加check_callback选项:

'core': {
    'check_callback' : true,
    'data': [
nue99wik

nue99wik2#

你的方法和引用似乎与我的有点不同,我的节点创建是嵌入 AJAX 调用中的。但是,我已经将适用于我的内容缩减为我认为应该适用于您的设置的内容:

contextmenu : {
    items : function (node) {
        var tmp = $.jstree.defaults.contextmenu.items();
        tmp.create.action = function (data) {
            var inst = $.jstree.reference(data.reference),
            obj = inst.get_node(data.reference);
            inst.create_node(obj, {type: "item", text: "some text"}, "last", function (new_node) {
                new_node.state = {
                    is_draggable: true,
                    opened: true,
                    disabled: false,
                    loaded: true
                };
            });
        }
        return tmp;
    }
}

相关问题