This part is duplicating built-in functionality. Read up on  
Sortable.serialize (with a z) in the Wiki. It's very powerful and  

var foo = Sortable.serialize('chunksList',{}) //no need to tell it  
tag:li -- it's the default

...time passes...

var fooOrder = foo.serialize();

The result will be an array where the index equals the list position,  
and the value equals the numerical id of the element. Elements must be  
ID'd in the following manner:



one underscore per ID, followed by a number -- which does not need to  
be incremental, that's just an artifact of my example (but it must be  
locally unique, naturally).

If you sorted this into 2,3,1 order, you would get back [2,3,1] from  
the serialize method, post that to your server as 'order', and on your  
server side, you would use something like this:

foreach($_POST['order'] as $k=>$v){
        if($item = ActiveRecord::FindById('items',$v){
                $v->position = $k+1;

and you're done.


On Feb 23, 2009, at 8:12 AM, vanq69 wrote:

> function serialise(listID)
> {
>       var length = document.getElementById(listID).childNodes.length;
>       var serialised = "";
>       for (i = 0; i < length; i++)
>       {
>               var li = document.getElementById(listID).childNodes[i];
>               var id = li.getAttribute("id");
>               // add number to serialised array
>               serialised += encodeURIComponent(id) + "_";
>       }
>       // returns array (with last "_" removed
>       return serialised.substring(0, serialised.length - 1);
> }

You received this message because you are subscribed to the Google Groups 
"Prototype &" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

Reply via email to