108 lines
3.5 KiB
PHP
108 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Tables;
|
|
|
|
use App\Models\User;
|
|
use App\Enums\UserGender;
|
|
use App\Enums\UserStatus;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use PowerComponents\LivewirePowerGrid\Column;
|
|
use PowerComponents\LivewirePowerGrid\Facades\Filter;
|
|
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
|
|
use PowerComponents\LivewirePowerGrid\PowerGridFields;
|
|
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
|
|
|
|
final class UserTable extends PowerGridComponent
|
|
{
|
|
public string $tableName = 'user-table';
|
|
|
|
public bool $showFilters = false;
|
|
|
|
public function boot(): void
|
|
{
|
|
config(['livewire-powergrid.filter' => 'outside']);
|
|
}
|
|
|
|
|
|
public function setUp(): array
|
|
{
|
|
$actions = [];
|
|
$header = PowerGrid::header()
|
|
->showToggleColumns();
|
|
$header->includeViewOnTop('livewire.headers.user');
|
|
$actions[]=$header;
|
|
$actions[]=PowerGrid::footer()->showPerPage()->showRecordCount();
|
|
return $actions;
|
|
}
|
|
|
|
public function datasource(): Builder
|
|
{
|
|
return User::query();
|
|
}
|
|
|
|
public function relationSearch(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function fields(): PowerGridFields
|
|
{
|
|
return PowerGrid::fields()
|
|
->add('id')
|
|
->add('name')
|
|
->add('email')
|
|
->add('phone')
|
|
->add('birthday_formatted',fn (User $model) => Carbon::parse($model->birthday)->format('Y-m-d'))
|
|
->add('gender_str', function (User $model) {
|
|
return $model->gender->labelPowergridFilter();
|
|
} )
|
|
->add('status_str', function (User $model) {
|
|
return $model->status->labelPowergridFilter();
|
|
} )
|
|
->add('roles' ,fn(User $model)=> $model->roles->pluck('name')->implode(', '))
|
|
->add('created_at_formatted', fn (User $model) => Carbon::parse($model->created_at)->format('Y-m-d H:i:s'));
|
|
}
|
|
|
|
public function columns(): array
|
|
{
|
|
return [
|
|
Column::make('ID', 'id'),
|
|
Column::make(__('users.name'), 'name')
|
|
->sortable()
|
|
->searchable(),
|
|
Column::make('Email', 'email')
|
|
->sortable()
|
|
->searchable(),
|
|
Column::make(__('users.phone'), 'phone')
|
|
->sortable()
|
|
->searchable(),
|
|
|
|
Column::make(__('users.gender'), 'gender_str','users.gender'),
|
|
Column::make(__('users.birthday'), 'birthday_formatted')->sortable()->searchable(),
|
|
Column::make(__('users.status'), 'status_str','users.status'),
|
|
Column::make(__('users.role'), 'roles'),
|
|
Column::make('建立時間', 'created_at_formatted', 'created_at')->sortable(),
|
|
|
|
];
|
|
}
|
|
|
|
public function filters(): array
|
|
{
|
|
return [
|
|
Filter::inputText('name')->placeholder(__('users.name')),
|
|
Filter::inputText('email')->placeholder('Email'),
|
|
Filter::inputText('phone')->placeholder(__('users.phone')),
|
|
Filter::enumSelect('gender_str','users.gender')
|
|
->datasource(UserGender::cases())
|
|
->optionLabel('users.gender'),
|
|
Filter::datepicker('birthday'),
|
|
Filter::enumSelect('status_str', 'users.status')
|
|
->datasource(UserStatus::cases())
|
|
->optionLabel('users.status'),
|
|
Filter::datetimepicker('created_at'),
|
|
];
|
|
}
|
|
|
|
}
|