Input Button
This example demonstrates how to use Buttons in Table rows.
ID
|
Name
|
Category
|
Price
|
In Stock
|
Created At
|
Action
|
---|---|---|---|---|---|---|
1
|
Arkansas Possum Pie
|
Pasta
|
279,94 €
|
No
|
03/04/2024
|
|
2
|
Albacore Tuna Melt
|
Soup
|
97,76 €
|
Yes
|
18/05/2024
|
|
3
|
борщ
|
Soup
|
100,19 €
|
Yes
|
30/04/2024
|
|
4
|
Bacalhau com natas
|
Fish
|
257,80 €
|
No
|
21/04/2024
|
|
5
|
Baba Ghanoush
|
Meat
|
234,59 €
|
No
|
01/05/2024
|
|
6
|
Bacon Cheeseburger
|
Fish
|
118,06 €
|
No
|
17/05/2024
|
|
7
|
Baked potato
|
Fish
|
156,36 €
|
No
|
13/04/2024
|
|
8
|
Baklava
|
Garnish
|
174,31 €
|
Yes
|
21/04/2024
|
|
9
|
Bangers and mash
|
Meat
|
193,81 €
|
No
|
03/04/2024
|
|
10
|
Black Pudding
|
Meat
|
108,28 €
|
Yes
|
18/05/2024
|
|
Disclaimer: Table data is randomly generated for
illustrative purposes only. The information here is not a reflection of the actual market and does
not constitute business, financial, or medical advice.
<?php namespace App\Livewire\Examples\InputButtonTable; use App\Models\Dish;use Illuminate\Database\Eloquent\Builder;use Illuminate\Support\Carbon;use Illuminate\Support\Number;use Livewire\Attributes\On;use PowerComponents\LivewirePowerGrid\Button;use PowerComponents\LivewirePowerGrid\Column;use PowerComponents\LivewirePowerGrid\Footer;use PowerComponents\LivewirePowerGrid\Header;use PowerComponents\LivewirePowerGrid\PowerGrid;use PowerComponents\LivewirePowerGrid\PowerGridComponent;use PowerComponents\LivewirePowerGrid\PowerGridFields; class InputButtonTable extends PowerGridComponent{ public function setUp(): array { return [ Header::make() ->showSearchInput(), Footer::make() ->showPerPage() ->showRecordCount(), ]; } public function datasource(): ?Builder { return Dish::with('category'); } public function fields(): PowerGridFields { return PowerGrid::fields() ->add('id') ->add('name') ->add('category_id', fn ($dish) => intval($dish->category_id)) ->add('category_name', fn ($dish) => e($dish->category->name)) ->add('price_in_eur', fn ($dish) => Number::currency($dish->price, in: 'EUR', locale: 'pt_PT')) ->add('in_stock', fn ($dish) => $dish->in_stock ? 'Yes' : 'No') ->add('created_at_formatted', fn ($dish) => Carbon::parse($dish->created_at)->format('d/m/Y')); } public function columns(): array { return [ Column::make('ID', 'id') ->searchable() ->sortable(), Column::make('Name', 'name') ->bodyAttribute('!text-wrap') // <--- Must add "!" to override style ->searchable() ->sortable(), Column::make('Category', 'category_name'), Column::make('Price', 'price_in_eur', 'price') ->searchable() ->sortable(), Column::make('In Stock', 'in_stock'), Column::make('Created At', 'created_at_formatted'), Column::action('Action'), ]; } public function actions(Dish $row): array { return [ Button::add('edit') ->slot("⚡ Edit {$row->name}") ->class('bg-blue-500 text-white font-bold py-2 px-2 rounded') ->dispatch('clickToEdit', ['dishId' => $row->id, 'dishName' => $row->name]), ]; } #[On('clickToEdit')] public function clickToEdit(int $dishId, string $dishName): void { $this->js("alert('Editing #{$dishId} - {$dishName}')"); }}
Code highlighting provided by Torchlight.dev
Here you can find all relevant packages installed on this demo.
Name
|
Version
|
Description
|
---|---|---|
laravel/framework
|
v11
|
The Laravel Framework.
|
livewire/livewire
|
v3.5.0
|
A front-end framework for Laravel.
|
openspout/openspout
|
v4.24.1
|
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
|
power-components/livewire-powergrid
|
5.x-dev
|
PowerGrid generates Advanced Datatables using Laravel Livewire.
|