【问题标题】:Sortable - Excluded items still affect index可排序 - 排除的项目仍会影响索引
【发布时间】:2013-08-15 17:58:23
【问题描述】:

我有一个 sortable 设置如下:

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

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

我想从可排序中忽略具有nosort 类的元素。

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

有什么简单的方法可以避免这种情况吗?


这是一个可排序示例的jsFiddle

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

【问题讨论】:

标签: javascript jquery jquery-ui jquery-ui-sortable


【解决方案1】:

获取.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();
});

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

【讨论】:

  • @MightyPork 进行了更改
猜你喜欢
  • 2013-06-10
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 2014-12-05
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多