Sortable icon indicating copy to clipboard operation
Sortable copied to clipboard

Wrong sort position when use draggable class

Open rottmann opened this issue 8 years ago • 8 comments

jsbin not work, so i post it quickly here:

<tr id="list">
  <th class="fixed">a</th>
  <th class="draggable">b</th>
  <th class="draggable">c</th>
  <th class="draggable">d</th>
  <th class="draggable">e</th>
  <th class="draggable">f</th>
</tr>

var el = document.getElementById('list');
var sortable = Sortable.create(el, {
    draggable: '.draggable',
    preventOnFilter: false
);

When drag e after f onSort show me: newIndex 4 oldIndex 3

and the result is:

<tr>
  <th class="fixed">a</th>
  <th class="draggable">b</th>
  <th class="draggable">c</th>
  <th class="draggable">e</th> <<--- wrong
  <th class="draggable">d</th>
  <th class="draggable">f</th>
</tr>

rottmann avatar May 30 '17 13:05 rottmann

I am also with this trouble. Has someone a workaround for it?

gulazaro94 avatar Jun 07 '17 13:06 gulazaro94

@rottmann, my friend, I found a workaround, use the option filter instead of draggable and pass the string .fixed instead of .draggable

gulazaro94 avatar Jun 07 '17 16:06 gulazaro94

@gulazaro94 thank you, i will try it!

rottmann avatar Jun 08 '17 07:06 rottmann

Also having the same issue.

shawnwallace avatar Jun 29 '17 18:06 shawnwallace

The above work-around did not work for me, I had to rely on a little jquery / DOM inspection to get the correct order when an element was dropped into a list:

in the onAdd function:

var el = $(event.item); // the dropped element
event.newIndex; // incorrect index returned
el.parent().children().toArray().indexOf(el[0]); // correct index returned

drewlarsen avatar Dec 14 '17 20:12 drewlarsen

Could you provide a JSBin with this issue? I cannot reproduce.

owen-m1 avatar Dec 26 '18 21:12 owen-m1

I am also having this issue. I have a child with class of .not-sortable which is filtered out using filter for the sortablejs options.

newIndex includes the filtered item. I'll reproduce for you in a JSBin

rathboma avatar Aug 29 '22 16:08 rathboma

Ok my issue was having an empty state list item that I hid once you'd dragged/dropped anything into that list. This empty state div was being used in calculating the position, even when hidden.

I made positions consistent with this in the onMove function:

    // prevent it from being added BEFORE the 'empty' state thing.
    if (event.related.classList.contains("not-sortable")) {
      return 1
    }

rathboma avatar Aug 29 '22 16:08 rathboma