156 lines
4.8 KiB
PHP
156 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\Artist;
|
|
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 ArtistTable extends PowerGridComponent
|
|
{
|
|
public string $tableName = 'artist-table';
|
|
|
|
public bool $showFilters = false;
|
|
|
|
public function boot(): void
|
|
{
|
|
config(['livewire-powergrid.filter' => 'outside']);
|
|
}
|
|
|
|
public function setUp(): array
|
|
{
|
|
$this->showCheckBox();
|
|
|
|
return [
|
|
PowerGrid::exportable(fileName: 'artist-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 Artist::query();
|
|
}
|
|
|
|
public function relationSearch(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function fields(): PowerGridFields
|
|
{
|
|
return PowerGrid::fields()
|
|
->add('id')
|
|
->add('category_id')
|
|
->add('name')
|
|
->add('simplified')
|
|
->add('phonetic_abbr')
|
|
->add('pinyin_abbr')
|
|
->add('created_at_formatted', fn (Artist $model) => Carbon::parse($model->created_at)->format('d/m/Y H:i:s'));
|
|
}
|
|
|
|
public function columns(): array
|
|
{
|
|
return [
|
|
Column::make(__('artists.no'), 'id'),
|
|
Column::make(__('artists.category'), 'category_id'),
|
|
Column::make(__('artists.name'), 'name')
|
|
->sortable()
|
|
->searchable(),
|
|
|
|
Column::make(__('artists.name.simplified'), 'simplified')
|
|
->sortable()
|
|
->searchable(),
|
|
|
|
Column::make(__('artists.name.phinetic'), 'phonetic_abbr')
|
|
->sortable()
|
|
->searchable(),
|
|
|
|
Column::make(__('artists.name.pinyin'), 'pinyin_abbr')
|
|
->sortable()
|
|
->searchable(),
|
|
|
|
Column::make('Created at', 'created_at_formatted', 'created_at')
|
|
->sortable(),
|
|
|
|
Column::action(__('artists.actions'))
|
|
];
|
|
}
|
|
|
|
public function filters(): array
|
|
{
|
|
return [
|
|
Filter::inputText('name')->placeholder(__('artists.name')),
|
|
Filter::inputText('phonetic_abbr')->placeholder(__('artists.name.phinetic')),
|
|
Filter::inputText('pinyin_abbr')->placeholder(__('artists.name.pinyin')),
|
|
Filter::datetimepicker('created_at'),
|
|
];
|
|
}
|
|
|
|
#[On('bulkDelete.{tableName}')]
|
|
public function bulkDelete(): void
|
|
{
|
|
$this->js('alert(window.pgBulkActions.get(\'' . $this->tableName . '\'))');
|
|
if($this->checkboxValues){
|
|
Artist::destroy($this->checkboxValues);
|
|
$this->js('window.pgBulkActions.clearAll()'); // clear the count on the interface.
|
|
}
|
|
}
|
|
|
|
public function actions(Artist $row): array
|
|
{
|
|
return [
|
|
|
|
Button::add('edit')
|
|
->slot(__('artists.edit'))
|
|
->icon('solid-pencil-square')
|
|
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
|
|
->dispatchTo('admin.artist-form', 'openEditArtistModal', ['id' => $row->id]),
|
|
Button::add('delete')
|
|
->slot(__('artists.delete'))
|
|
->icon('solid-trash')
|
|
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
|
|
->dispatchTo('admin.artist-form', 'deleteArtist', ['id' => $row->id]),
|
|
];
|
|
}
|
|
|
|
/*
|
|
public function actionRules($row): array
|
|
{
|
|
return [
|
|
// Hide button edit for ID 1
|
|
Rule::button('edit')
|
|
->when(fn($row) => $row->id === 1)
|
|
->hide(),
|
|
];
|
|
}
|
|
*/
|
|
}
|