Skip to content

Instantly share code, notes, and snippets.

@ramaID
Forked from tanthammar/ItemList.php
Created October 14, 2020 14:33
Show Gist options
  • Save ramaID/26f655424b326402a809ae41ffbca494 to your computer and use it in GitHub Desktop.
Save ramaID/26f655424b326402a809ae41ffbca494 to your computer and use it in GitHub Desktop.
/**
* In your Livewire blade for the list of items
*/
<ul wire:sortable="saveOrderChange">
@foreach($this->list as $item)
<li
wire:sortable.item="{{ $item->id }}"
wire:key="{{ $item->id }}"
>
Content here
</li>
@endforeach
</ul>
<?php
/**
* In your Livewire model for the list of items
*/
class ItemList extends Component
{
// rest of component
public function saveOrderChange($sortableEvent)
{
// Convert the sortable event into an array of positions for the setNewOrder function
$order = array_map(function ($item) {
return intval($item['value']);
}, $sortableEvent);
// probably a better way to do this, but this was my in 2 mins solution and I haven't gotten back to it yet
$this->list->items()->first()->setNewOrder($order);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment