| 1 | --- |
| 2 | includeMockServer: true |
| 3 | title: "Drag to Reorder" |
| 4 | description: Change order of records with drag and drop |
| 5 | icon: "icon-[mdi--reorder-horizontal]" |
| 6 | soon: true |
| 7 | --- |
| 8 | |
| 9 | <script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script> |
| 10 | <script> |
| 11 | if (!window._sortableLoadHandler) { |
| 12 | window._sortableLoadHandler = (e) => { |
| 13 | const sortables = e.detail.elt.querySelectorAll(".sortable"); |
| 14 | for (const sortable of sortables) { |
| 15 | const sortableInstance = new Sortable(sortable, { |
| 16 | animation: 150, |
| 17 | ghostClass: "sortable-ghost", |
| 18 | filter: ".htmx-indicator", |
| 19 | onMove: (evt) => evt.related.className.indexOf("htmx-indicator") === -1, |
| 20 | onEnd: function (evt) { |
| 21 | this.option("disabled", true); |
| 22 | } |
| 23 | }); |
| 24 | sortable.addEventListener("htmx:afterSwap", () => { |
| 25 | sortableInstance.option("disabled", false); |
| 26 | }); |
| 27 | } |
| 28 | }; |
| 29 | document.addEventListener("htmx:load", window._sortableLoadHandler); |
| 30 | } |
| 31 | |
| 32 | // Inject ghost class styles (Sortable applies this class name directly) |
| 33 | var _sortableStyle = document.getElementById('_sortable-ghost-style'); |
| 34 | if (!_sortableStyle) { |
| 35 | _sortableStyle = document.createElement("style"); |
| 36 | _sortableStyle.id = '_sortable-ghost-style'; |
| 37 | _sortableStyle.textContent = `.sortable-ghost { opacity: 0.5 !important; }`; |
| 38 | document.head.appendChild(_sortableStyle); |
| 39 | } |
| 40 | |
| 41 | var _listItems = [1, 2, 3, 4, 5]; |
| 42 | |
| 43 | server.get("/demo", () => ` |
| 44 | <form class="sortable" hx-post="/items" hx-trigger="end"> |
| 45 | ${listContents()} |
| 46 | </form>`); |
| 47 | |
| 48 | server.post("/items", (req) => { |
| 49 | _listItems = req.params.item; |
| 50 | return listContents(); |
| 51 | }); |
| 52 | |
| 53 | function listContents() { |
| 54 | return `<div class="cursor-default bg-transparent border-none px-0 py-1 my-0 w-auto text-xs text-neutral-600 dark:text-neutral-400 italic htmx-indicator">Updating...</div>\n` + |
| 55 | _listItems.map((val) => |
| 56 | `<div class="bg-white dark:bg-neutral-900 border border-neutral-200 dark:border-neutral-700 px-3 py-2.5 my-2 w-52 cursor-grab active:cursor-grabbing rounded-md select-none text-sm text-neutral-700 dark:text-neutral-200 transition-colors hover:bg-neutral-50 dark:hover:bg-neutral-850"><input type="hidden" name="item" value="${val}"/>Item ${val}</div>` |
| 57 | ).join("\n"); |
| 58 | }; |
| 59 | |
| 60 | server.start("/demo"); |
| 61 | </script> |
| 62 | |
| 63 | <div id="demo-content" class="not-prose demo-container flex justify-center"></div> |
| 64 | |
| 65 | ## Basic usage |
| 66 | |
| 67 | This pattern integrates the [Sortable.js](https://sortablejs.github.io/Sortable/) library with htmx to persist drag-and-drop reordering on the server. |
| 68 | |
| 69 | On the client, wrap your items in a form that posts on the Sortable `end` event. |
| 70 | |
| 71 | ```html |
| 72 | <form class="sortable" hx-post="/items" hx-trigger="end"> |
| 73 | <div class="htmx-indicator">Updating...</div> |
| 74 | <div><input type="hidden" name="item" value="1"/>Item 1</div> |
| 75 | <div><input type="hidden" name="item" value="2"/>Item 2</div> |
| 76 | <div><input type="hidden" name="item" value="3"/>Item 3</div> |
| 77 | </form> |
| 78 | ``` |
| 79 | |
| 80 | - [`hx-post`](/reference/attributes/hx-post) sends the new order to `/items`. |
| 81 | - [`hx-trigger`](/reference/attributes/hx-trigger)=`"end"` fires when Sortable.js finishes a drag (the `end` event bubbles up to the form). |
| 82 | - Each item has a hidden input so the server receives the ids in their new order. |
| 83 | |
| 84 | On the server, read the `item` parameter (which arrives as an ordered list) and respond with the updated list HTML. |
| 85 | |
| 86 | ## Notes |
| 87 | |
| 88 | ### Sortable.js initialization |
| 89 | |
| 90 | Initialize Sortable on your containers using the `htmx:load` event so it works after htmx swaps in new content. |
| 91 | |
| 92 | ```js |
| 93 | document.addEventListener("htmx:load", (e) => { |
| 94 | const sortables = e.detail.elt.querySelectorAll(".sortable"); |
| 95 | for (const sortable of sortables) { |
| 96 | const sortableInstance = new Sortable(sortable, { |
| 97 | animation: 150, |
| 98 | ghostClass: "sortable-ghost", |
| 99 | |
| 100 | // Make the `.htmx-indicator` unsortable |
| 101 | filter: ".htmx-indicator", |
| 102 | onMove: (evt) => |
| 103 | evt.related.className.indexOf("htmx-indicator") === -1, |
| 104 | |
| 105 | // Disable sorting until the server responds |
| 106 | onEnd: function (evt) { |
| 107 | this.option("disabled", true); |
| 108 | } |
| 109 | }); |
| 110 | |
| 111 | // Re-enable sorting after the swap completes |
| 112 | sortable.addEventListener("htmx:afterSwap", () => { |
| 113 | sortableInstance.option("disabled", false); |
| 114 | }); |
| 115 | } |
| 116 | }); |
| 117 | ``` |
| 118 | |
| 119 | Note that `onEnd` uses a regular `function` (not an arrow function) because it needs `this` to refer to the Sortable instance. Sorting is disabled during the request and re-enabled on `htmx:afterSwap` to prevent the user from reordering while the server is processing. |