views:

196

answers:

1

I am using the jQuery sortable() feature to re-order a list of items. After an item is drug to a new location, I kick off an AJAX form post to the server to save the new order. How can I undo the sort (e.g. return the drug item to its original position in the list) if I receive an error message from the server?

Basically, I only want the re-order to "stick" if the server confirms that the changes were saved.

A: 

I'm pretty sure that sortable doesn't have any undo-last-drop function -- but it's a great idea!

In the mean time, though, I think your best bet is to write some sort of start that stores the ordering, and then on failure call a revert function. I.e. something like this:

$("list-container").sortable({
  start: function () { 
           /* stash current order of sorted elements in an array */
         },
  update: function () {
          /* ajax call; on failure, re-order based on the stashed order */
         }
});

Would love to know if others have a better answer, though.

brahn