From 3a609e495f0d231b84bb6035e8abd81dcba52b1f Mon Sep 17 00:00:00 2001 From: "allen.yan" Date: Fri, 2 May 2025 14:38:49 +0800 Subject: [PATCH] =?UTF-8?q?table=20=E5=8F=AF=E4=BB=A5=E4=BF=AE=E6=94=B9=20?= =?UTF-8?q?=E6=AD=8C=E6=89=8B=E9=A1=9E=E5=88=A5=EF=BC=8C=E6=AD=8C=E6=9B=B2?= =?UTF-8?q?=E8=AA=9E=E5=88=A5=EF=BC=8C=E6=AD=8C=E6=9B=B2=E6=83=85=E5=A2=83?= =?UTF-8?q?=2020250502?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Livewire/Admin/ArtistTable.php | 29 ++++++++++- app/Livewire/Admin/SongTable.php | 50 +++++++++++++++++-- app/View/Components/SelectCategory.php | 27 ++++++++++ .../components/select-category.blade.php | 16 ++++++ 4 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 app/View/Components/SelectCategory.php create mode 100644 resources/views/components/select-category.blade.php diff --git a/app/Livewire/Admin/ArtistTable.php b/app/Livewire/Admin/ArtistTable.php index 3fd8925..3b5f604 100644 --- a/app/Livewire/Admin/ArtistTable.php +++ b/app/Livewire/Admin/ArtistTable.php @@ -5,6 +5,8 @@ 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; @@ -72,9 +74,20 @@ final class ArtistTable extends PowerGridComponent public function fields(): PowerGridFields { + $options = $this->categorySelectOptions(); + return PowerGrid::fields() ->add('id') - ->add('category_str', fn (Artist $model) => ArtistCategory::from($model->category->value)->labels()) + ->add('category_str', function (Artist $model) use ($options){ + return Blade::render( + '', + [ + 'options' => $options, + 'modelId' => intval($model->id), + 'fieldName'=>'category', + 'selected' => $model->category->value + ]); + } ) ->add('name') ->add('simplified') ->add('phonetic_abbr') @@ -116,6 +129,20 @@ final class ArtistTable extends PowerGridComponent ]; } + 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 [ diff --git a/app/Livewire/Admin/SongTable.php b/app/Livewire/Admin/SongTable.php index 38e4d2d..fb44c14 100644 --- a/app/Livewire/Admin/SongTable.php +++ b/app/Livewire/Admin/SongTable.php @@ -6,6 +6,8 @@ use App\Models\Song; use App\Enums\SongLanguageType; use App\Enums\SongSituation; 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; @@ -86,11 +88,31 @@ final class SongTable extends PowerGridComponent ->add('filename') ->add('adddate_formatted', fn (Song $model) => Carbon::parse($model->adddate)->format('Y-m-d')) ->add('song_artists' ,fn(Song $model)=> $model->str_artists()) - ->add('language_type_str', fn (Song $model) => SongLanguageType::from($model->language_type->value)->labels()) + //->add('language_type_str', fn (Song $model) => SongLanguageType::from($model->language_type->value)->labels()) + ->add('language_type_str', function (Song $model) { + return Blade::render( + '', + [ + 'options' => $this->languageTypeSelectOptions(), + 'modelId' => intval($model->id), + 'fieldName'=>'language_type', + 'selected' => $model->language_type->value + ]); + } ) ->add('song_categories', fn(Song $model) => $model->str_categories()) ->add('db_change') ->add('vocal') - ->add('situation_str', fn (Song $model) => SongSituation::from($model->situation->value)->labels()) + //->add('situation_str', fn (Song $model) => SongSituation::from($model->situation->value)->labels()) + ->add('situation_str', function (Song $model){ + return Blade::render( + '', + [ + 'options' => $this->situationSelectOptions(), + 'modelId' => intval($model->id), + 'fieldName'=>'situation', + 'selected' => $model->situation->value + ]); + } ) ->add('copyright01') ->add('copyright02') ->add('note01') @@ -160,7 +182,8 @@ final class SongTable extends PowerGridComponent Column::make(__('songs.note02'), 'note02') ->sortable() ->searchable() - ->editOnClick(hasPermission: true, dataField: 'note02', fallback: 'N/A', saveOnMouseOut: true), + ->editOnClick(hasPermission: true, dataField: 'note02', fallback: 'N/A', saveOnMouseOut: true) + ->hidden(true, false), Column::make(__('songs.note03'), 'note03') ->sortable() @@ -208,7 +231,26 @@ final class SongTable extends PowerGridComponent Filter::datetimepicker('created_at'), ]; } - + public function languageTypeSelectOptions():Collection + { + return collect(SongLanguageType::cases())->mapWithKeys(function (SongLanguageType $case) { + return [$case->value => $case->labels()]; + }); + } + public function situationSelectOptions():Collection + { + return collect(SongSituation::cases())->mapWithKeys(function (SongSituation $case) { + return [$case->value => $case->labels()]; + }); + } + #[On('categoryChanged')] + public function categoryChanged($value,$fieldName, $modelId): void + { + // dd($value,$fieldName, $modelId); + if (in_array($fieldName, ['language_type', 'situation'])) { + Artist::find($modelId)?->update([$fieldName => $value]); + } + } #[On('bulkDelete.{tableName}')] public function bulkDelete(): void { diff --git a/app/View/Components/SelectCategory.php b/app/View/Components/SelectCategory.php new file mode 100644 index 0000000..b2837aa --- /dev/null +++ b/app/View/Components/SelectCategory.php @@ -0,0 +1,27 @@ + + + \ No newline at end of file