Sortable
Drag things into the order you want — tasks, menu items, table rows, dashboard cards. One attribute on the list. It works with a mouse, a finger (press and hold, or grab a handle) and the keyboard, and tells your app the new order so you can save it.
- Write the product brief
- Review the new checkout
- Order packaging
- Plan the holiday sale
- Reply to wholesale leads
Drag a row by its handle — or Tab to a handle, Space, arrows, Space.
<ul class="list-group" data-moey-sortable>
<li class="list-group-item" data-id="t1"><span class="sort-handle"></span>Write the product brief</li>
<li class="list-group-item" data-id="t2"><span class="sort-handle"></span>Review the new checkout</li>
…
</ul>
Add it, step by step
- Add
data-moey-sortableto the list — aul, anol, a.list-group, a grid of cards, or a table'stbody. Its children become draggable. - Give each item a
data-idso you know which is which when you save. - Decide how it is grabbed. Put an empty
<span class="sort-handle"></span>(six dots) in each item and only the handle drags — best when the item has links or buttons, and on touch it starts at once. Without handles the whole item drags; on touch, press and hold first (a plain swipe still scrolls). - Save the order in
moey:sort— readMoey.sortable(list).value()(the ids, in order) and send it to your server. Say no withe.preventDefault()and the item goes back.
list.addEventListener('moey:sort', (e) => {
const { id, fromIndex, index } = e.detail; // 't3', 2, 0
saveOrder(Moey.sortable(list).value()); // ['t3', 't1', 't2', 't4', 't5']
});
A grid of cards
Rows and grids work the same — here without handles, so the whole card drags (press and hold on touch). Arrange your dashboard:
<div class="grid grid-cols-2 md:grid-cols-3 gap-3" data-moey-sortable>
<div class="card" data-id="revenue">…</div>
<div class="card" data-id="orders">…</div>
…
</div>
Table rows
Put data-moey-sortable on the tbody and a handle in the first cell.
| Move | Product | Price | Stock |
|---|---|---|---|
| Linen shirt | $49.00 | 124 | |
| Canvas tote | $29.00 | 312 | |
| Wool beanie | $24.00 | 88 | |
| Leather wallet | $59.00 | 41 |
<table class="table">
<thead><tr><th><span class="visually-hidden">Move</span></th><th>Product</th>…</tr></thead>
<tbody data-moey-sortable>
<tr data-id="p1"><td><span class="sort-handle"></span></td><td>Linen shirt</td>…</tr>
…
</tbody>
</table>
Between lists
Give several lists the same name — data-moey-sortable="tasks" — and items move between them. e.detail.from and e.detail.to are the lists. (For a full board with columns, counts and limits, see Kanban.)
- Pack 12 orders
- Call the courier
- Photograph new bags
- Update the FAQ
- Try a new supplier
<ul class="list-group" data-moey-sortable="tasks" data-id="today">…</ul>
<ul class="list-group" data-moey-sortable="tasks" data-id="later">…</ul>
<script>
document.addEventListener('moey:sort', (e) => {
moveTask(e.detail.id, e.detail.to.dataset.id, e.detail.index); // 'b', 'later', 0
});
</script>
On a phone
- AKAna Kim
- MRMarco Rossi
- LFLena Fischer
- SOSam Okafor
- PNPriya Nair
- Inbox
- Orders
- Customers
- Reports
Keyboard
Tab to an item (or its handle), Space picks it up, ↑ ↓ move it (← → in a row of cards), Home / End to the top or bottom, Space drops and Esc puts it back. Each step is read out ("position 2 of 5").
Reference
| Class / attribute | What it does |
|---|---|
data-moey-sortable | Its children can be dragged into a new order |
data-moey-sortable="name" | Lists with the same name share items |
data-id on items | What value() and the event report |
.sort-handle | The grip (empty: six dots); with handles only they drag, and touch starts at once |
.sort-label | The text read out when the item moves (else the item's title or text) |
data-moey-no-sort · data-moey-no-drag | On an item: it stays put · inside an item: this part doesn't start a drag |
Event moey:sort | On the item, cancelable: detail: { item, id, from, to, fromIndex, index } |
Moey.sortable(list) | { value() → ids, move(item | id, index) } |