135 lines
4.1 KiB
PHP
135 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\User;
|
|
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('created_at_formatted', fn (User $model) => Carbon::parse($model->created_at)->format('d/m/Y H:i:s'));
|
|
}
|
|
|
|
public function columns(): array
|
|
{
|
|
return [
|
|
Column::make('ID', 'id'),
|
|
Column::make('名稱', 'name')->sortable()->searchable(),
|
|
Column::make('Email', 'email')->sortable()->searchable(),
|
|
Column::make('建立時間', 'created_at_formatted', 'created_at')->sortable(),
|
|
Column::action('操作')
|
|
];
|
|
}
|
|
|
|
public function filters(): array
|
|
{
|
|
return [
|
|
Filter::inputText('name')->placeholder('Dish Name'),
|
|
Filter::inputText('email')->placeholder('Dish Email'),
|
|
Filter::datetimepicker('created_at'),
|
|
];
|
|
}
|
|
|
|
public function actions(User $row): array
|
|
{
|
|
return [
|
|
|
|
Button::add('edit')
|
|
->slot('編輯')
|
|
->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('刪除')
|
|
->icon('solid-trash')
|
|
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
|
|
->dispatchTo('admin.user-form', 'deleteUser', ['id' => $row->id]),
|
|
];
|
|
}
|
|
#[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(),
|
|
];
|
|
} */
|
|
|
|
}
|