javascript jstree 从树中获取新的 json 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27618305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:42:25  来源:igfitidea点击:

jstree get new json data from tree

javascriptjsontreegetjsonjstree

提问by user3820266

I created a tree with the following data. After this process, I made a drag-drop process between menus. And as a result, my menu structure was changed. I want to export new JSON data which has the same structure as my first data. How can I get data from the tree? Please help me.

我用以下数据创建了一棵树。在这个过程之后,我在菜单之间做了一个拖放过程。结果,我的菜单结构发生了变化。我想导出与我的第一个数据具有相同结构的新 JSON 数据。如何从树中获取数据?请帮我。

I tried this code, but this export very complicated JSON. I won't like my first data format.

我试过这段代码,但是这个导出非常复杂的 JSON。我不会喜欢我的第一个数据格式。

var v = $('#data').jstree(true).get_json();
var mytext = JSON.stringify(v);
alert(mytext);

First state of menu:

菜单的第一状态:

enter image description here

在此处输入图片说明

Last state of menu:

菜单的最后状态:

enter image description here

在此处输入图片说明

// html demo
$('#html').jstree();

// inline data demo



    $(function() {
            var arrayCollection = [
                {"id": "animal", "parent": "#", "text": "Animals"},
                {"id": "device", "parent": "#", "text": "Devices"},
                {"id": "dog", "parent": "animal", "text": "Dogs"},
                {"id": "lion", "parent": "animal", "text": "Lions"},
                {"id": "mobile", "parent": "device", "text": "Mobile Phones"},
                {"id": "lappy", "parent": "device", "text": "Laptops"},
                {"id": "daburman", "parent": "dog", "text": "Dabur Man", "icon": "/"},
                {"id": "Dalmation", "parent": "dog", "text": "Dalmatian", "icon": "/"},
                {"id": "african", "parent": "lion", "text": "African Lion", "icon": "/"},
                {"id": "indian", "parent": "lion", "text": "Indian Lion", "icon": "/"},
                {"id": "apple", "parent": "mobile", "text": "Apple IPhone 6", "icon": "/"},
                {"id": "samsung", "parent": "mobile", "text": "Samsung Note II", "icon": "/"},
                {"id": "lenevo", "parent": "lappy", "text": "Lenevo", "icon": "/"},
                {"id": "hp", "parent": "lappy", "text": "HP", "icon": "/"}
            ];
$('#data').jstree({
    'core' : {
        'check_callback' : true,
        'data' :arrayCollection ,

    },


    "plugins" : ["dnd","wholerow"]
});


});//function

回答by user3820266

I found the most simple way to get json from tree;

我找到了从树中获取 json 的最简单方法;

var v = $('#data').jstree(true).get_json('#', {flat:true})
var mytext = JSON.stringify(v);
alert(mytext);