Rails - how to serialize a tree not in a form
I started with the standard scriptaculous drag and drop tree, and that all works fine.
Then started implementing this: http://www.开发者_如何学JAVAartweb-design.de/2008/5/30/scriptaculous-sortabletree which gives a good drag and drop tree
Where I am stuck is how to get serialize the tree (unordered list)? It's not in a form, and I can't find a way to serialize it to move onto setting up the AJAX update.
Well, here's the answer I developed, in case anyone else has to deal with this. Sure there's a better way to do this etc.
function walk_tree(node, parent_stem, at_root ) {
if ( node == null || node == undefined || $(node) == undefined )
return ;
var i = 0 ;
var serialized_tree = '' ;
var stem = '' ;
// loop through siblings
do {
// build up the serialized statement for this node
stem = parent_stem + '[' + i + ']' ;
serialized_tree += (at_root ? '' : '&' ) + stem + '[id]=' + $(node).identify().gsub('page_','')
// if node has children, then recurse...
if ( $(node).down('li') != undefined )
serialized_tree += walk_tree($(node).down('li'), stem, false) ;
// carry on finding siblings
node = $(node).next('li') ;
i++ ;
} while ( $(node) != undefined ) ;
return(serialized_tree) ;
} ;
精彩评论