johnny / jquery-sortable

A flexible, opinionated sorting plugin for jQuery
http://johnny.github.io/jquery-sortable/
Other
1.52k stars 441 forks source link

Revert/undo a drop #280

Open LarryBarker opened 4 years ago

LarryBarker commented 4 years ago

Have been working to handle an edge case where dropping an item fails. I am processing a request on the server side using onDrop and if the server throws an error, I would like to "undo" the drop so the item does not stick in the destination container, but effectively moves back to the container it was dragged from.

I looked at the only other issue similar to this, which was closed some time ago, but it doesn't appear to work. I have also tried detaching the item from the container it was dropped in, and then calling _super but this doesnt work either.

onDrop: function ($item, container, _super) {
            $.request('onDropItem', {
                error: function(jqXHR, textStatus, errorThrown) {
                    // revert/undo the drop here
                    _super($item, container.rootGroup.itemContainer);
            })
}

I believe container.rootGroup.itemContainer is a reference to the container the item was selected from? I thought calling _super() is what performs the actual drop?

Out of desperation, I thought maybe just using the same code from onDrop when it is successful, but using container.rootGroup.itemContainer would do it, but this hasn't worked either:

var $clonedItem = $("<li/>").css({ height: 0 });
                    $item.before($clonedItem);

                    $clonedItem.animate({ height: $item.height() });

                    $item.animate($clonedItem.position(), function () {
                        $clonedItem.detach();
                        _super($item, container.rootGroup.itemContainer);
                    });

Has anyone implemented this functionality before? Am I correct that container.rootGroup.itemContainer is a reference to the container the item originated from?

Any feedback is appreciated!

OluwoleOyetoke commented 3 years ago

@LarryBarker , I'm encountering a similar issue the canceling drop event. Were you able to find a workaround for this?