199 lines
6.6 KiB
PHP
199 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\User;
|
|
use App\Enums\UserGender;
|
|
use App\Enums\UserStatus;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use PowerComponents\LivewirePowerGrid\Button;
|
|
use PowerComponents\LivewirePowerGrid\Column;
|
|
use PowerComponents\LivewirePowerGrid\Facades\Filter;
|
|
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
|
|
use PowerComponents\LivewirePowerGrid\PowerGridFields;
|
|
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
|
|
//use PowerComponents\LivewirePowerGrid\Traits\WithExport;
|
|
use PowerComponents\LivewirePowerGrid\Components\SetUp\Exportable;
|
|
use PowerComponents\LivewirePowerGrid\Facades\Rule;
|
|
use Livewire\Attributes\On;
|
|
|
|
final class UserTable extends PowerGridComponent
|
|
{
|
|
//use WithExport ;
|
|
|
|
public string $tableName = 'user-table';
|
|
|
|
public bool $showFilters = false;
|
|
|
|
public function boot(): void
|
|
{
|
|
config(['livewire-powergrid.filter' => 'outside']);
|
|
}
|
|
|
|
|
|
public function setUp(): array
|
|
{
|
|
$this->showCheckBox();
|
|
|
|
return [
|
|
PowerGrid::exportable(fileName: 'my-export-file')
|
|
->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV),
|
|
PowerGrid::header()
|
|
//->showSoftDeletes()
|
|
->showToggleColumns()
|
|
->showSearchInput(),
|
|
PowerGrid::footer()->showPerPage()->showRecordCount(),
|
|
];
|
|
}
|
|
public function header(): array
|
|
{
|
|
return [
|
|
Button::add('bulk-delete')
|
|
->slot('Bulk delete (<span x-text="window.pgBulkActions.count(\'' . $this->tableName . '\')"></span>)')
|
|
->icon('solid-trash',['id' => 'my-custom-icon-id', 'class' => 'font-bold'])
|
|
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
|
|
->dispatch('bulkDelete.' . $this->tableName, []),
|
|
];
|
|
}
|
|
|
|
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', fn (User $model) => UserGender::from($model->gender)->labels())
|
|
->add('status', fn (User $model) => UserStatus::from($model->status)->labels())
|
|
->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()
|
|
->editOnClick(
|
|
hasPermission: true,
|
|
dataField: 'name',
|
|
fallback: 'N/A',
|
|
saveOnMouseOut: true
|
|
),
|
|
Column::make('Email', 'email')
|
|
->sortable()
|
|
->searchable()
|
|
->editOnClick(
|
|
hasPermission: true,
|
|
dataField: 'email',
|
|
fallback: 'N/A',
|
|
saveOnMouseOut: true
|
|
),
|
|
Column::make(__('users.phone'), 'phone')
|
|
->sortable()
|
|
->searchable()
|
|
->editOnClick(
|
|
hasPermission: true,
|
|
dataField: 'phone',
|
|
fallback: 'N/A',
|
|
saveOnMouseOut: true
|
|
),
|
|
|
|
Column::make(__('users.gender'), 'gender','users.gender'),
|
|
Column::make(__('users.birthday'), 'birthday_formatted')->sortable()->searchable(),
|
|
Column::make(__('users.status'), 'status','users.status'),
|
|
Column::make(__('users.role'), 'roles'),
|
|
Column::make('建立時間', 'created_at_formatted', 'created_at')->sortable(),
|
|
Column::action('操作')
|
|
];
|
|
}
|
|
|
|
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','users.gender')
|
|
->datasource(UserGender::cases())
|
|
->optionLabel('users.gender'),
|
|
Filter::datepicker('birthday'),
|
|
Filter::enumSelect('status', 'users.status')
|
|
->datasource(UserStatus::cases())
|
|
->optionLabel('users.status'),
|
|
Filter::datetimepicker('created_at'),
|
|
];
|
|
}
|
|
|
|
public function actions(User $row): array
|
|
{
|
|
return [
|
|
|
|
Button::add('edit')
|
|
->slot(__('users.edit'))
|
|
->icon('solid-pencil-square')
|
|
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
|
|
->dispatchTo('admin.user-form', 'openEditUserModal', ['id' => $row->id]),
|
|
Button::add('delete')
|
|
->slot(__('users.delete'))
|
|
->icon('solid-trash')
|
|
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
|
|
->dispatchTo('admin.user-form', 'deleteUser', ['id' => $row->id]),
|
|
];
|
|
}
|
|
|
|
public function onUpdatedEditable($id, $field, $value): void
|
|
{
|
|
$updated = User::query()->where('id', $id)->update([
|
|
$field => $value,
|
|
]);
|
|
if ($updated) {
|
|
$this->fillData();
|
|
}
|
|
}
|
|
public function onUpdatedToggleable($id, $field, $value): void
|
|
{
|
|
$updated = User::query()->where('id', $id)->update([
|
|
$field => $value,
|
|
]);
|
|
if ($updated) {
|
|
$this->fillData();
|
|
}
|
|
}
|
|
#[On('bulkDelete.{tableName}')]
|
|
public function bulkDelete(): void
|
|
{
|
|
$this->js('alert(window.pgBulkActions.get(\'' . $this->tableName . '\'))');
|
|
if($this->checkboxValues){
|
|
User::destroy($this->checkboxValues);
|
|
$this->js('window.pgBulkActions.clearAll()'); // clear the count on the interface.
|
|
}
|
|
}
|
|
|
|
|
|
/* public function actionRules($row): array
|
|
{
|
|
return [
|
|
// Hide button edit for ID 1
|
|
Rule::button('edit')
|
|
->when(fn($row) => $row->id === 1)
|
|
->hide(),
|
|
];
|
|
} */
|
|
|
|
}
|