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)
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/
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