johnny / johnny/jquery-sortable

Revert/undo a drop

Open
#280 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
1.5k
Forks
429
PR merge metrics
No merged PRs in 30d

Description

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 = $("

  • ").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!

  • Contributor guide

    No contributing guide indexed for this repository

    Assessment

    This issue has not been assessed yet.

    Get new issues in your inbox

    A short digest of beginner-friendly GitHub issues.