Skip to content

Instantly share code, notes, and snippets.

@danieljpalmer
Last active October 14, 2020 14:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save danieljpalmer/9662f67cc0dcc75af224931f2019c8fc to your computer and use it in GitHub Desktop.
Save danieljpalmer/9662f67cc0dcc75af224931f2019c8fc 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);
}
}
@danpalmieri
Copy link

danpalmieri commented Jun 25, 2020

I did as it follows:

$order = Arr::pluck($sortableEvent, 'value');

Model::setNewOrder($order);

:-)

@danclaytondev
Copy link

@danieljpalmer Thanks very much for the gist! How did you manage to keep the width of your element when dragging it? (The mirror item) I saw in the preview that yours stays the same but for me it loses the width when its being dragged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment