EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: LdyMox on June 04, 2012, 06:49:30 AM



Title: Tree and JSON Data
Post by: LdyMox on June 04, 2012, 06:49:30 AM
I can not seem to figure our how to get back the JSON data for the current places of the Tree UI. I am trying to use the JSON data and hope to pass the values via a form so I can save the data for later use.


Title: Re: Tree and JSON Data
Post by: LdyMox on June 05, 2012, 08:01:07 AM
I need to get the data back somehow, anyways so I can save the data. Loading is no problem, but can't figure how to access the current state of the tree so I can use it.


Title: Re: Tree and JSON Data
Post by: stworthy on June 05, 2012, 06:22:41 PM
Call 'getData' method to get a node data, get the root node data and store it in somewhere. To restore the tree, call 'loadData' method and pass the original root node data as its parameter. The code looks like this:
Code:
var root = $('#tt').tree('getRoot');  // get root node
var data = $('#tt').tree('getData', root.target);  // get the root node data
// now restore the tree
$('#tt').tree('loadData', [data]);

The 'getData' method is implemented as below:
Code:
$.fn.tree.methods.getData = function(jq, target){
function retrieveChildData(aa, ul){
ul.children('li').each(function(){
var node = $(this).children('div.tree-node');
var nodedata = jq.tree('getNode', node[0]);
var sub = $(this).children('ul');
if (sub.length){
nodedata.children = [];
retrieveChildData(nodedata.children, sub);
}
aa.push(nodedata);
});
}
var nodedata = jq.tree('getNode', target);
nodedata.children = [];
retrieveChildData(nodedata.children, $(target).next());
return nodedata;
};


Title: Re: Tree and JSON Data
Post by: LdyMox on June 06, 2012, 05:51:51 AM
Ahh thank you! I knew there had to be something I was missing!