I use the following code to reorder items on my website:
$(document).ready(function () {
    $("#my-list").sortable({
        stop: function (event, ui) {
            var id = $(ui.item).attr('id');
            var url = '/en/account/changeposition?id=idreplace&position=posReplace';
            url = url.replace("idreplace", id);
            url = url.replace("posReplace", ui.item.index());
            window.location.href = url;
        },
        distance: 15
    });
});
However, even if I don't change element position and place element at the same place, Jquery UI will call 'stop'-function.
Is it possible to detect if element index (position) was changed without using custom data attributes?
// I would like to avoid adding custom data attributes like this:
<li id="id100" data-original-position="1"></li>
<li id="id101" data-original-position="2"></li>
                Have a look at this answer.
It worked for me as follows:
$(selector).sortable({
    start: function(event, ui) {
        ui.item.data('start_pos', ui.item.index());
    },
    stop: function(event, ui) {
        var start_pos = ui.item.data('start_pos');
        if (start_pos != ui.item.index()) {
            // the item got moved
        } else {
            // the item was returned to the same position
        }
    },
    update: function(event, ui) {
        $.post('/reorder', $(selector).sortable('serialize'))
            .done(function() {
                alert('Updated')
            });
        }
});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With