Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sortable - Excluded items still affect index

I have a sortable set up like this:

$('.sortable').sortable({
    items: '> *:not(.nosort)',
    axis: 'y',

    stop: function (event, ui) {
        var index = ui.item.index();
        // do something with the index
    }
});

I want to ignore elements with the nosort class from the sortable.

This works good; however, the index I get seems to include all elements in the sortable, not only those that can be sorted, so it can't really be used for what I need.

Is there any easy way to avoid this?


Here's a jsFiddle of an example sortable.

(note: subtracting 1 from the index is not an option, because the number and position of excluded elements can vary)

like image 752
MightyPork Avatar asked Nov 02 '22 16:11

MightyPork


1 Answers

Get a collection of .sortable children and then find the index, using .index(), based on that...

You can do that by making the following changes:

$(document).ready(function () {
    $('.sortable').sortable({
        items: '> *:not(.nosort)',
        axis: 'y',

        stop: function (event, ui) {

            // obtain index of the moved item
            var index = $(this).children(':not(.nosort)').index(ui.item);

            $('#index').text(index);

        }
    }).disableSelection();
});

DEMO: http://jsfiddle.net/dirtyd77/Yy9hW/3/

like image 197
Dom Avatar answered Nov 09 '22 16:11

Dom