JQuery UI可排序:根据某些条件恢复位置

我调用sortable.stop()来进行ajax调用,以便在拖放操作后存储一些数据。

当ajax调用返回错误(应用程序逻辑错误或网络问题)时,我想将拖动的元素移动到其原始/开始位置,我该如何实现它?

场景应该是

  1. 用户拖动A到B.
  2. 调用sortable.stop()事件,它会触发ajax调用
  3. ajax调用返回错误
  4. stop()事件中,我们得到了ajax错误
  5. 将A移动到其原始位置
  6. 用户再次将A移动到B.
  7. 一切都好
  8. A仍然处于B的新位置

显示步骤6-8以阐明可以进行连续拖动并且必须忘记先前的错误。

看看.sortable('cancel') ; 像这样的东西:

 var list = $('#some-list') list.sortable( { // Some options. stop: function() { $.post('some-url', someData, function() { if (somethingWentWrong) { list.sortable('cancel'); } }); } }); 

感谢dafi – 这适合我:

 $('#some-list').sortable({ // other options here, receive: function(event, ui) { // ajax or function here if(somethingWentWrong) { $(this).sortable('cancel'); $(ui.sender).sortable('cancel'); } } }); 

我不确定你是否需要$(this).sortable('cancel'); ,但我认为最好取消源目标可排序列表。 这样,列表项立即恢复。 其他可排序选项在这里。

感谢liveat60fps,它是你需要的两个声明中的第二个声明。

 $('#some-list').sortable({ // other options here, receive: function(event, ui) { // ajax or function here if(somethingWentWrong) { $(this).sortable('cancel'); $(ui.sender).sortable('cancel'); } } }); 

这种方式在某种情况下,您可以重新回到原始状态。