2014-12-23 45 views
10

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

我试过这段代码,但是这个导出非常复杂的json。我想要我的第一个数据格式。

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

菜单的第一状态:

enter image description here

菜单的最后状态:

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 

回答

29

我发现最简单的方式从树上得到JSON;

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