Thursday, February 01, 2007

How do I persist a TreePanel?

Ext its not out yet but here is the code:



YAHOO.ext.tree.TreePanel.prototype.toJsonString=function(){
var nodes=this.getRootNode().childNodes;
this.arrNodes=[{id:this.getRootNode().id,parent:0}];
for(var i=0;i< nodes.length;i++){
this.arrNodes.push({id:nodes[i].id,parent:this.getRootNode().id});
this.processChildren(nodes[i]);
}
return YAHOO.ext.util.JSON.encode(this.arrNodes);
}
/*
Recursive function
sweeps the tree in all levels
*/
YAHOO.ext.tree.TreePanel.prototype.processChildren=function(node){
child=node.childNodes;
for(var i=0;i<child.length;i++){
this.arrNodes.push({id:child[i].id,parent:node.id});
this.processChildren(child[i]);
}
}




It returns a jsonString with an array of objects with the tree nodes and its parents reflecting the changed tree. When Ext comes out I will build a complete sample persisting that output to a database.





powered by performancing firefox

1 comment:

zahaby said...

How can I re-load it into an EXT.tree ?