Skip to content

Instantly share code, notes, and snippets.

@tanthammar
Forked from danieljpalmer/ItemList.php
Created June 23, 2020 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tanthammar/186b205b86fa606ba71ae7a31cfb0702 to your computer and use it in GitHub Desktop.
Save tanthammar/186b205b86fa606ba71ae7a31cfb0702 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