Edit File: index.blade.php
<?php use App\Models\User; use Illuminate\Support\Collection; use Livewire\Volt\Component; use Mary\Traits\Toast; new class extends Component { use Toast; public string $search = ''; public bool $drawer = false; public array $sortBy = ['column' => 'name', 'direction' => 'asc']; // Clear filters public function clear(): void { $this->reset(); $this->success('Filters cleared.', position: 'toast-bottom'); } // Delete action public function delete($id): void { $this->warning("Will delete #$id", 'It is fake.', position: 'toast-bottom'); } // Table headers public function headers(): array { return [ ['key' => 'id', 'label' => '#', 'class' => 'w-1'], ['key' => 'name', 'label' => 'Name', 'class' => 'w-64'], ['key' => 'age', 'label' => 'Age', 'class' => 'w-20'], ['key' => 'email', 'label' => 'E-mail', 'sortable' => false], ]; } /** * For demo purpose, this is a static collection. * * On real projects you do it with Eloquent collections. * Please, refer to maryUI docs to see the eloquent examples. */ public function users(): Collection { return collect([ ['id' => 1, 'name' => 'Mary', 'email' => 'mary@mary-ui.com', 'age' => 23], ['id' => 2, 'name' => 'Giovanna', 'email' => 'giovanna@mary-ui.com', 'age' => 7], ['id' => 3, 'name' => 'Marina', 'email' => 'marina@mary-ui.com', 'age' => 5], ]) ->sortBy([[...array_values($this->sortBy)]]) ->when($this->search, function (Collection $collection) { return $collection->filter(fn(array $item) => str($item['name'])->contains($this->search, true)); }); } public function with(): array { return [ 'users' => $this->users(), 'headers' => $this->headers() ]; } }; ?> <div> <!-- HEADER --> <x-header title="Hello" separator progress-indicator> <x-slot:middle class="!justify-end"> <x-input placeholder="Search..." wire:model.live.debounce="search" clearable icon="o-magnifying-glass" /> </x-slot:middle> <x-slot:actions> <x-button label="Filters" @click="$wire.drawer = true" responsive icon="o-funnel" /> </x-slot:actions> </x-header> <!-- TABLE --> <x-card> <x-table :headers="$headers" :rows="$users" :sort-by="$sortBy"> @scope('actions', $user) <x-button icon="o-trash" wire:click="delete({{ $user['id'] }})" wire:confirm="Are you sure?" spinner class="btn-ghost btn-sm text-red-500" /> @endscope </x-table> </x-card> <!-- FILTER DRAWER --> <x-drawer wire:model="drawer" title="Filters" right separator with-close-button class="lg:w-1/3"> <x-input placeholder="Search..." wire:model.live.debounce="search" icon="o-magnifying-glass" @keydown.enter="$wire.drawer = false" /> <x-slot:actions> <x-button label="Reset" icon="o-x-mark" wire:click="clear" spinner /> <x-button label="Done" icon="o-check" class="btn-primary" @click="$wire.drawer = false" /> </x-slot:actions> </x-drawer> </div>
Back