存储重命名的JS.Tree节点的结果

时间:2017-08-16 12:08:09

标签: javascript jstree

我有一个可编辑的树结构。用户可以创建,重命名和删除单个节点。现在,当我选择要重命名的节点时,我使用此代码将原始值存储在变量中。

        function rename() {
            var data = null;
        data = $(plBox2).jstree().get_selected(true)[0].text;
        console.log(data);
        }

在上下文菜单中选择“重命名”时,将调用此函数。我想要做的是在重命名节点之后存储该节点的新值,但我无法弄明白。我的最终目标是将旧值和新值都发送到MySQL表。

这是我的树和上下文菜单:

$('#plBox2').jstree({
                "checkbox": {
                "keep_selected_style": false
                },
                "core" : {
                    "themes": {
                        "name": "default",
                        "dots": true,
                        "icons": true,
                    },
                    "check_callback": true,
                },                  
                "plugins" : [ 
                        "massload", "wholerow", "contextmenu", "changed", "sort", "dnd",
                ],

                "contextmenu":{         
                    "items": function($node) {
                        var tree = $("#plBox2").jstree(true);
                        return {
                            "Info": {
                                "separator_before": false,
                                "separator_after": false,
                                "label": "Info",
                                "action": function (obj) { document.getElementById('finInfo').style.display='block';
                                }
                            },
                            "Create": {
                                "separator_before": false,
                                "separator_after": false,
                                "label": "Create",
                                "action": function (data) {
                                    var inst = $.jstree.reference(data.reference),
                                    obj = inst.get_node(data.reference);
                                    inst.create_node(obj, {}, "last", function (new_node) {
                                    new_node.data = {file: true};
                                    setTimeout(function () { inst.edit(new_node); },0);
                                    });
                                }
                            },
                            "Rename": {
                                "separator_before": false,
                                "separator_after": false,
                                "label": "Rename",
                                "action": function (obj) { 
                                    tree.edit($node);
                                    rename();
                            }
                        },                         
                            "Remove": {
                                "separator_before": false,
                                "separator_after": false,
                                "label": "Remove",
                                "action": function (obj) { 
                                    tree.delete_node($node);
                                }
                            }
                        };
                    }
                },

HTML:

<div class="2colWrap">
            <div class="col1">
                <div id="plBox2"></div>

            </div>
            <div class="col2">
                <div id="finishBox"></div>
            </div>
        </div>

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以在下面使用此声明!

                 "Rename": {
                        "separator_before": false,
                        "separator_after": false,
                        "label": "Rename",
                        "action": function (obj) {
                            console.log("waht?");
                            currentNode = $node;
                            //tree.edit($node);
                            tree.edit('j1_1', null, function (node, status) {
                                debugger;
                                console.log("old name: " + node.original.text);
                                console.log("new: " + node.text);
                                console.log(arguments);
                            });

                        }
                    },