jQuery UI Sortable: Revert changes if update callback makes an AJAX call that fails?

后端 未结 3 953
情书的邮戳
情书的邮戳 2021-02-04 00:58

I am using the sortable widget to re-order a list of items. After an item is dragged to a new location, I kick off an AJAX form post to the server to save the new order. How can

3条回答
  •  孤城傲影
    2021-02-04 01:30

    I just encountered this same issue, and for the sake of a complete answer, I wanted to share my solution to this problem:

    $('.list').sortable({
      items:'.list:not(.loading)',
      start: function(event,ui) { 
        var element = $(ui.item[0]);
        element.data('lastParent', element.parent());
      },
      update: function(event,ui) {
        var element = $(ui.item[0]);
        if (element.hasClass('loading')) return;
        element.addClass('loading');
        $.ajax({
          url:'/ajax',
          context:element,
          complete:function(xhr,status) {
            $(this).removeClass('loading');
            if (xhr.status != 200) {
              $($(this).data('lastParent')).append(this);
            }
          },
        });
      }
    });
    

    You'll need to modify it to suit your codebase, but this is a completely multithread safe solution that works very well for me.

提交回复
热议问题