Supercharge your data tables with wire:sortable in Laravel Livewire v3

Managing sortable tables efficiently is essential for providing an interactive and user-friendly experience. Laravel Livewire v3 introduces the wire:sortable directive, allowing you to easily implement sortable tables. This feature enables users to sort columns effortlessly, enhancing the usability of your data tables.

Understanding wire:sortable

The wire:sortable directive helps you make table rows draggable and sortable, providing a straightforward way to manage and update the order of items in a table. This is particularly useful for applications where users need to rearrange items, such as task management systems or admin dashboards.

Basic Usage

Here’s a basic example to illustrate how wire:sortable works:

<table wire:sortable="updateOrder">
    <thead>
        <tr>
            <th>Name</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        @foreach($users as $user)
            <tr wire:sortable.item="{{ $user->id }}" wire:key="user-{{ $user->id }}">
                <td wire:sortable.handle>{{ $user->name }}</td>
                <td>{{ $user->email }}</td>
            </tr>
        @endforeach
    </tbody>
</table>

In this example, the table rows become draggable, allowing users to reorder them. The updateOrder method will be triggered whenever the order is changed.

Real-Life Example

Consider a scenario where you have a list of tasks, and you want users to be able to reorder these tasks to prioritize them. Here’s how you can implement this using wire:sortable:

use Livewire\Component;

class TaskList extends Component
{
    public $tasks;

    public function mount()
    {
        $this->tasks = Task::orderBy('order')->get();
    }

    public function updateOrder($orderedIds)
    {
        foreach ($orderedIds as $index => $id) {
            Task::where('id', $id)->update(['order' => $index]);
        }

        $this->tasks = Task::orderBy('order')->get();
    }

    public function render()
    {
        return view('livewire.task-list');
    }
}
<!-- Blade Template (livewire/task-list.blade.php) -->
<table wire:sortable="updateOrder">
    <thead>
        <tr>
            <th>Task</th>
            <th>Priority</th>
        </tr>
    </thead>
    <tbody>
        @foreach($tasks as $task)
            <tr wire:sortable.item="{{ $task->id }}" wire:key="task-{{ $task->id }}">
                <td wire:sortable.handle>{{ $task->name }}</td>
                <td>{{ $task->priority }}</td>
            </tr>
        @endforeach
    </tbody>
</table>

In this example, users can drag and drop tasks to reorder them, and the updateOrder method updates the order in the database accordingly.

Conclusion

The wire:sortable directive in Laravel Livewire v3 is a powerful tool for enhancing the interactivity of your data tables. By leveraging this feature, you can provide users with a seamless and intuitive way to manage and reorder items. Give wire:sortable a try in your next Laravel Livewire project and see how it can improve your application's usability.

Subscribe to Harris Raftopoulos

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe