The wire:sortable directive enables drag-and-drop reordering for a flat list of items.
Requirements
- The container element must have the
wire:sortable attribute. - Each draggable item must have the
wire:sortable.item attribute. The value of this attribute is passed back to your Livewire component. - (Optional) If you want to restrict dragging to a specific handle, add an element with the
wire:sortable.handle attribute inside your items. - (Optional) To prevent specific elements from triggering drag events, add the
wire:sortable.ignore attribute.
Behavior
When a sort operation completes (sortable:stop), the directive automatically calls the Livewire method specified in your directive call (e.g., wire:sortable="updateOrder"). It passes an array of objects to that method. Each object contains:
order: The new 1-based index of the item.value: The string value provided in the wire:sortable.item attribute.
Example call: wire:sortable="updateOrder"
<!-- HTML Example -->
<div wire:sortable="updateOrder">
<div wire:sortable.item="item-1">Item 1</div>
<div wire:sortable.item="item-2">Item 2</div>
<div wire:sortable.item="item-3">
<span wire:sortable.handle>Drag Me</span> Item 3
</div>
</div>