Drag and Drop Sortable Demo
This demo demonstrates how to integrate htmx with the Sortable.js library to create a reorderable list. When an item is dropped into a new position, htmx automatically sends the new order to the server using a POST request.
Sortable List
Drag the handles to reorder items.
Updating...
How it works
- Sortable.js: Handles the drag-and-drop UI and DOM manipulation.
- Hyperscript: Initializes Sortable on the list element when it loads.
It also listens for Sortable's
onEndcallback and dispatches a standard DOMendevent. - htmx: Listens for the
endevent on the form. When triggered, it collects allitemIdsinputs (thanks to Sortable moving the DOM elements) and posts them to the server. It also includes the ASP.NET Core Anti-Forgery Token in the request headers via a global configuration. - ASP.NET Core: The
OnPostReorderhandler receives the array of IDs and updates the sequence in the backend.