Just wondering would a regular old GROUP BY followed by ORDER BY be better. Letting the database work for you.
But are you saying they aren't being sorted correctly in livewire?
How is it possible to detect sorting across groups if the only values we get are the $item and $position?
from the docs
use Livewire\Component;
use Livewire\Attributes\Computed;
new class extends Component {
public User $user;
public function sortItem($item, $position)
{
$item = $this->todo->items()->findOrFail($item);
// Update the item's position
}
};
<div>
@foreach ($user->todoLists as $todo)
<ul wire:sort="sortItem" wire:sort:group="todos">
@foreach ($todo->items as $item)
<li wire:sort:item="{{ $item->id }}">
{{ $item->title }}
</li>
@endforeach
</ul>
@endforeach
</div>
Hi @swaz, yes, I believe so. In my context, I have a form that has sections and each section has questions. I made each section its own livewire component (see simplified version below), but wired them all up to the same wire:sort:group. When I move a question, the moveQuestion() method is only triggered on the receiving section. I have the $this->section parent info available on that component, so I can tell which section should now have the question.
Hopefully this code below feels straightforward?
<?php
use App\Models\FormSection;
use Livewire\Component;
new class extends Component
{
public FormSection $section;
public function moveQuestion(string $item, $position)
{
dd($this->section->id);
// Insert DB reordering logic here (as we now have $item, $position, and parent ($section))
}
};
?>
<div wire:sort="moveQuestion" wire:sort:group="questions">
@foreach($section->questions as $question)
<div class="m-4" wire:sort:item="{{ $question->id }}">
{{ $question->question }}
</div>
@endforeach
</div>
Please or to participate in this conversation.