200 lines
6.7 KiB
PHP
200 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\Artist;
|
|
use App\Enums\ArtistCategory;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Blade;
|
|
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 $deferLoading = true;
|
|
//public string $loadingComponent = 'components.power-grid-loading';
|
|
|
|
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()
|
|
->withoutLoading()
|
|
->showToggleColumns()
|
|
//->showSoftDeletes()
|
|
//->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 ')
|
|
->dispat ch('bulkDelete.' . $this->tableName, []),*/
|
|
];
|
|
}
|
|
|
|
public function datasource(): Builder
|
|
{
|
|
return Artist::query();
|
|
}
|
|
|
|
public function relationSearch(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function fields(): PowerGridFields
|
|
{
|
|
$options = $this->categorySelectOptions();
|
|
|
|
return PowerGrid::fields()
|
|
->add('id')
|
|
->add('category_str', function (Artist $model) use ($options){
|
|
return Blade::render(
|
|
'<x-select-category type="occurrence" :options=$options :modelId=$modelId :fieldName=$fieldName :selected=$selected/>',
|
|
[
|
|
'options' => $options,
|
|
'modelId' => intval($model->id),
|
|
'fieldName'=>'category',
|
|
'selected' => $model->category->value
|
|
]);
|
|
} )
|
|
->add('name')
|
|
->add('simplified')
|
|
->add('phonetic_abbr')
|
|
->add('pinyin_abbr')
|
|
->add('strokes_abbr')
|
|
->add('created_at_formatted', fn (Artist $model) => Carbon::parse($model->created_at)->format('Y-m-d H:i:s'));
|
|
}
|
|
|
|
public function columns(): array
|
|
{
|
|
return [
|
|
Column::make(__('artists.no'), 'id'),
|
|
Column::make(__('artists.category'),'category_str', 'artists.category')->searchable(),
|
|
Column::make(__('artists.name'), 'name')
|
|
->sortable()
|
|
->searchable(),
|
|
|
|
Column::make(__('artists.name.simplified'), 'simplified')
|
|
->sortable()
|
|
->searchable()
|
|
->hidden(true, false),
|
|
|
|
Column::make(__('artists.name.phinetic'), 'phonetic_abbr')
|
|
->sortable()
|
|
->searchable(),
|
|
|
|
Column::make(__('artists.name.pinyin'), 'pinyin_abbr')
|
|
->sortable()
|
|
->searchable(),
|
|
Column::make(__('artists.name.strokes'), 'strokes_abbr')
|
|
->sortable()
|
|
->searchable(),
|
|
|
|
Column::make('Created at', 'created_at_formatted', 'created_at')
|
|
->sortable()
|
|
->hidden(true, false),
|
|
|
|
Column::action(__('artists.actions'))
|
|
];
|
|
}
|
|
|
|
public function categorySelectOptions():Collection
|
|
{
|
|
return collect(ArtistCategory::cases())->mapWithKeys(function (ArtistCategory $case) {
|
|
return [$case->value => $case->labels()];
|
|
});
|
|
}
|
|
#[On('categoryChanged')]
|
|
public function categoryChanged($value,$fieldName, $modelId): void
|
|
{
|
|
// dd($value,$fieldName, $modelId);
|
|
if($fieldName=='category'){
|
|
Artist::find($modelId)?->update([$fieldName => $value]);
|
|
}
|
|
}
|
|
public function filters(): array
|
|
{
|
|
return [
|
|
Filter::enumSelect('category_str','artists.category')
|
|
->datasource(ArtistCategory::cases())
|
|
->optionLabel('artists.category'),
|
|
Filter::inputText('name')->placeholder(__('artists.name')),
|
|
Filter::inputText('phonetic_abbr')->placeholder(__('artists.name.phinetic')),
|
|
Filter::inputText('pinyin_abbr')->placeholder(__('artists.name.pinyin')),
|
|
Filter::number('strokes_abbr')->thousands('.')->decimal(','),
|
|
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(),
|
|
];
|
|
}
|
|
*/
|
|
}
|