Sortable icon indicating copy to clipboard operation
Sortable copied to clipboard

[bug] SortableJS + AlpineJS = x-for visual conflicts

Open 10YAR opened this issue 1 year ago • 2 comments

Hello,

I'm currently struggling on using SortableJS in a AlpineJS project. The problem is that when I drag an element, it does change the index values in the elements array, but visually, it stays the same.

This is how I init SortableJS :

document.addEventListener("DOMContentLoaded", () => {
    new Sortable(document.getElementById("sortableContainer"), {
      animation: 200,
      onStart: function (event) {
         Alpine.store("dashboard").saveOldIndex(event.oldDraggableIndex);
      },
      onEnd: function (event) {
        Alpine.store("dashboard").movePersonne(event.newDraggableIndex);
      }
    });
}); 

If you comment the Alpine.store functions, there's no bug anymore, but then the changed array isn't being saved.

Here's a codepen with the bug encountered : https://codepen.io/Aristy/pen/bGJWMxp

Thank you for your help

I tried without using a store (but a data), and it was worse.

10YAR avatar Mar 23 '24 10:03 10YAR

For what it's worth, I'm running into a similar issue. Though, I think the bug is on the Alpine.js side, not the Sortable.js side. If I just bind the Sortable to the DOM, everything works well. But, the moment I try to save the .toArray() of the Sortable back into the Alpine.js array, the DOM ordering seems to be completely random.

Image

In my logging, the two in-memory lists align, and are correct (ie, the result of the drag-n-drop). However, the DOM (on the left) changes to some random state.

bennadel avatar Jun 21 '25 10:06 bennadel

Also, seems like there is a discussion over in Alpine.js issues: https://github.com/alpinejs/alpine/discussions/4157

bennadel avatar Jun 21 '25 13:06 bennadel