Input Select
This example demonstrates how to use a Custom Field to render a select input in a table cell.
You can find the Blade Component source code in SelectCategory.php and in select-category.blade.php.
ID
|
Dish Name
|
Category
|
Price
|
In Stock
|
Created At
|
---|---|---|---|---|---|
1
|
Arkansas Possum Pie
|
|
188,03Β β¬
|
Yes
|
03/11/2024
|
2
|
Albacore Tuna Melt
|
|
145,93Β β¬
|
Yes
|
01/12/2024
|
3
|
Π±ΠΎΡΡ
|
|
100,19Β β¬
|
Yes
|
02/12/2024
|
4
|
Bacalhau com natas
|
|
115,28Β β¬
|
No
|
22/11/2024
|
5
|
Baba Ghanoush
|
|
127,86Β β¬
|
No
|
02/11/2024
|
6
|
Bacon Cheeseburger
|
|
220,97Β β¬
|
Yes
|
04/11/2024
|
7
|
Baked potato
|
|
195,18Β β¬
|
Yes
|
28/10/2024
|
8
|
Baklava
|
|
254,15Β β¬
|
Yes
|
06/12/2024
|
9
|
Bangers and mash
|
|
172,21Β β¬
|
Yes
|
11/12/2024
|
10
|
Black Pudding
|
|
230,45Β β¬
|
Yes
|
18/12/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\InputSelectTable;
use App\Models\Category;
use App\Models\Dish;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Number;
use Livewire\Attributes\On;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
class InputSelectTable extends PowerGridComponent
{
public string $tableName = 'input-select-table';
public function setUp(): array
{
return [
PowerGrid::header()
->showSearchInput(),
PowerGrid::footer()
->showPerPage()
->showRecordCount(),
];
}
public function datasource(): ?Builder
{
return Dish::query()
->join('categories as newCategories', function ($categories) {
$categories->on('dishes.category_id', '=', 'newCategories.id');
})
->select('dishes.*', 'newCategories.name as category_name')
->toBase();
}
public function fields(): PowerGridFields
{
$options = $this->categorySelectOptions();
return PowerGrid::fields()
->add('id')
->add('name')
->add('category_id', fn ($dish) => intval($dish->category_id))
->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'))
->add('category_name', function ($dish) use ($options) {
if (is_null($dish->category_id)) {
dd($dish);
}
return Blade::render('<x-select-category type="occurrence" :options=$options :dishId=$dishId :selected=$selected/>', ['options' => $options, 'dishId' => intval($dish->id), 'selected' => intval($dish->category_id)]);
});
}
public function columns(): array
{
return [
Column::make('ID', 'id'),
Column::make('Dish Name', 'name')
->bodyAttribute('!text-wrap')
->searchable()
->sortable(),
Column::make('Category', 'category_name'),
Column::make('Price', 'price_in_eur', 'price'),
Column::make('In Stock', 'in_stock'),
Column::make('Created At', 'created_at_formatted'),
];
}
public function categorySelectOptions(): Collection
{
return Category::all(['id', 'name'])->mapWithKeys(function ($item) {
return [
$item->id => match (strtolower($item->name)) {
'meat' => 'π ',
'fish' => 'π ',
'pie' => 'π° ',
'garnish' => 'π₯ ',
'pasta' => 'π ',
'dessert' => 'π§ ',
'soup' => 'π² ',
} . $item->name,
];
});
}
#[On('categoryChanged')]
public function categoryChanged($categoryId, $dishId): void
{
dd("category Id: {$categoryId} for Dish id: {$dishId}");
}
}