Sortable icon indicating copy to clipboard operation
Sortable copied to clipboard

wrong index for old/newIndex in react mixin

Open chrisivens opened this issue 11 years ago • 8 comments

For some reason, when sorting items I have 1-based indexes which messes things up when it comes to run items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]);

I've had to set this thus:

oldIndex = evt.oldIndex - 1,
newIndex = evt.newIndex - 1

Once I do that, things run fine. Could this be caused by having an element which is not sortable within the same div?

chrisivens avatar Jul 27 '15 14:07 chrisivens

https://github.com/RubaXa/Sortable/blob/master/CONTRIBUTING.md#issue

RubaXa avatar Jul 28 '15 07:07 RubaXa

The same issue for me, 1.7.0v start indexing from 0, 1.8.3v starts indexing from 1. I was using ^1.7.0 in my package.json so after build 1.8.3v was downloaded, this is how I noticed.

cmoutafidis avatar Feb 13 '19 18:02 cmoutafidis

@cmoutafidis This is not true, but the indexing is more lenient to include non-draggable items. You will have to compensate for any elements in the element you passed into Sortable that are not draggable.

owen-m1 avatar Feb 13 '19 18:02 owen-m1

@cmoutafidis This is not true, but the indexing is more lenient to include non-draggable items. You will have to compensate for any elements in the element you passed into Sortable that are not draggable.

So, Sortable will include non-draggable items when returning indexes. Should this be documented somewhere?

Backoo avatar Apr 06 '19 21:04 Backoo

@Backoo It is never documented that it only includes draggable items

owen-m1 avatar Apr 06 '19 22:04 owen-m1

In my previous comment I wrote about the documentation because the fact Sortable will include non-draggable items can lead to subtle confusion and unexpected results when using Sortable, as happened to me.

BTW Is the draggable option really needed if all the direct children items will be draggable?

Backoo avatar Apr 06 '19 23:04 Backoo

@Backoo No, if all the direct children are draggable then the option will include them by default, unless it is a ul or ol then it will only include direct li descendants. In general, I would try to avoid having non-draggable items in the Sortable element, as I have not tested it so there could be even more unexpected behavior.

owen-m1 avatar Apr 07 '19 00:04 owen-m1

@cmoutafidis @Backoo You can now use event.newDraggableIndex to get the index of the element only counting draggable elements.

owen-m1 avatar Apr 08 '19 20:04 owen-m1