ewein
ewein

Reputation: 2735

jQuery Sortable list ordering

I have a jQuery sortable list of unknown size and I need to update the item ordering when a item is re-positioned. I have the items position showing and I have the position updating when the item is moved however when an item is moved the other positions are not updated. For example, if my sortable list has 3 items with ordering 1, 2, 3 and I decide to move item 2 to position 3 the new ordering is 1, 3, 3. I want it to update to 1, 2, 3. Any ideas how I can achieve this? Here is my code for updating the position. Thanks.

    $("#sortable2").sortable({
        stop: function(event, ui) {
            var id = $(ui.item).attr('id');
            $("#" + id).html(ui.item.index() + 1); 
        }
    });

Upvotes: 0

Views: 333

Answers (1)

greener
greener

Reputation: 5069

Try this:

$("#sortable").sortable({
    stop: function(event, ui) {
        $('li').each(function(idx){
            $(this).html(idx+1);
        });
    }
});

Basically, you need to iterate over each element. Also, index is 0 based.

JSFIDDLE

Upvotes: 1

Related Questions