可排序 - 排除的项目仍会影响索引

Mig*_*ork 5 javascript jquery jquery-ui jquery-ui-sortable

我有一个像这样的可排序设置:

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

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

我想忽略nosort来自sortable 的类的元素.

这很好用; 但是,我得到的索引似乎包括可排序的所有元素,不仅包括那些可以排序的元素,因此它不能真正用于我需要的东西.

有没有简单的方法来避免这种情况?


这是一个可排序的例子的jsFiddle.

(注意:从索引中减去1不是一个选项,因为排除元素的数量和位置可能会有所不同)

Dom*_*Dom 2

获取子集合.sortable,然后使用 查找索引.index(),基于此...

您可以通过进行以下更改来做到这一点:

$(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();
});
Run Code Online (Sandbox Code Playgroud)

演示: http: //jsfiddle.net/dirtyd77/Yy9hW/3/