2014-12-23 44 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);