From 8eab0aef3ab841d7bf0abfd10b4782ac40496790 Mon Sep 17 00:00:00 2001 From: devsquad-luan-freitas Date: Thu, 18 Jul 2024 10:12:41 -0300 Subject: [PATCH] Add primaryKeyAlias property --- resources/views/components/editable.blade.php | 6 +++--- .../components/frameworks/bootstrap5/editable.blade.php | 2 +- .../frameworks/bootstrap5/toggle-detail.blade.php | 2 +- .../frameworks/bootstrap5/toggleable.blade.php | 2 +- .../components/frameworks/tailwind/editable.blade.php | 2 +- .../frameworks/tailwind/toggle-detail.blade.php | 2 +- .../components/frameworks/tailwind/toggleable.blade.php | 2 +- resources/views/components/row.blade.php | 8 ++++---- resources/views/components/table.blade.php | 4 ++-- resources/views/components/table/detail.blade.php | 8 ++++---- src/Concerns/Base.php | 9 +++++++++ src/Concerns/ToggleDetail.php | 4 +++- 12 files changed, 31 insertions(+), 20 deletions(-) diff --git a/resources/views/components/editable.blade.php b/resources/views/components/editable.blade.php index f03e250d2..f28a3e7b8 100644 --- a/resources/views/components/editable.blade.php +++ b/resources/views/components/editable.blade.php @@ -38,7 +38,7 @@ $params = [ 'theme' => data_get($theme, 'name'), 'tableName' => $tableName, - 'id' => data_get($row, $primaryKey), + 'id' => data_get($row, $this->realPrimaryKey), 'dataField' => $field, 'content' => $content, 'fallback' => $fallback, @@ -74,9 +74,9 @@
@if ($showErrorBag) - @error($field . '.' . $row->{$primaryKey}) + @error($field . '.' . $row->{$this->realPrimaryKey})
- {{ str($message)->replace($field . '.' . $row->{$primaryKey}, $field) }} + {{ str($message)->replace($field . '.' . $row->{$this->realPrimaryKey}, $field) }}
@enderror @endif diff --git a/resources/views/components/frameworks/bootstrap5/editable.blade.php b/resources/views/components/frameworks/bootstrap5/editable.blade.php index 4f9bec841..cb10b876a 100644 --- a/resources/views/components/frameworks/bootstrap5/editable.blade.php +++ b/resources/views/components/frameworks/bootstrap5/editable.blade.php @@ -1,6 +1,6 @@
@includeIf(data_get($setUp, 'detail.viewIcon')) diff --git a/resources/views/components/frameworks/bootstrap5/toggleable.blade.php b/resources/views/components/frameworks/bootstrap5/toggleable.blade.php index c7b74726c..91a5085df 100644 --- a/resources/views/components/frameworks/bootstrap5/toggleable.blade.php +++ b/resources/views/components/frameworks/bootstrap5/toggleable.blade.php @@ -1,6 +1,6 @@ @php $params = [ - 'id' => data_get($row, $primaryKey), + 'id' => data_get($row, $this->realPrimaryKey), 'isHidden' => !$showToggleable, 'tableName' => $tableName, 'field' => $column->field, diff --git a/resources/views/components/frameworks/tailwind/editable.blade.php b/resources/views/components/frameworks/tailwind/editable.blade.php index 67e0bc55c..24ffb43a9 100644 --- a/resources/views/components/frameworks/tailwind/editable.blade.php +++ b/resources/views/components/frameworks/tailwind/editable.blade.php @@ -1,6 +1,6 @@
@includeIf(data_get($setUp, 'detail.viewIcon')) diff --git a/resources/views/components/frameworks/tailwind/toggleable.blade.php b/resources/views/components/frameworks/tailwind/toggleable.blade.php index 450d767a4..9d7103a99 100644 --- a/resources/views/components/frameworks/tailwind/toggleable.blade.php +++ b/resources/views/components/frameworks/tailwind/toggleable.blade.php @@ -5,7 +5,7 @@ $falseValue = $column->toggleable['default'][1]; $params = [ - 'id' => data_get($row, $primaryKey), + 'id' => data_get($row, $this->realPrimaryKey), 'isHidden' => !$showToggleable, 'tableName' => $tableName, 'field' => $column->field, diff --git a/resources/views/components/row.blade.php b/resources/views/components/row.blade.php index 76bb85cd7..f3eb4e84c 100644 --- a/resources/views/components/row.blade.php +++ b/resources/views/components/row.blade.php @@ -71,7 +71,7 @@
@if(empty(data_get($row, 'actions')) && $column->isAction) @if (method_exists($this, 'actionsFromView') && $actionsFromView = $this->actionsFromView($row)) -
+
{!! $actionsFromView !!}
@endif @@ -80,7 +80,7 @@ @if (filled(data_get($row, 'actions')) && $column->isAction) @foreach (data_get($row, 'actions') as $key => $action) @if(filled($action)) - + {!! $action !!} @endif @@ -92,7 +92,7 @@ $hasFieldRules = $actionRulesClass->recoverActionForField($row, $field); // =============* Edit On Click *===================== - + $showEditOnClick = false; if (data_get($column->editable, 'hasPermission')) { @@ -125,7 +125,7 @@ $showEditOnClick = false; } @endphp - + @if($showEditOnClick === true) @include(data_get($theme, 'editable.view') ?? null, ['editable' => $column->editable]) diff --git a/resources/views/components/table.blade.php b/resources/views/components/table.blade.php index d0d5b15fe..ed09b71c4 100644 --- a/resources/views/components/table.blade.php +++ b/resources/views/components/table.blade.php @@ -29,7 +29,7 @@ @php throw new Exception('To use checkboxes, you must define a unique key attribute in your data source.') @endphp @endif @php - $rowId = data_get($row, $primaryKey); + $rowId = data_get($row, $this->realPrimaryKey); $class = data_get($theme, 'table.trBodyClass'); @@ -96,7 +96,7 @@ @if (filled($rulesValues['detailView'])) - @includeWhen(data_get($setUp, 'detail.state.' . $row->{$primaryKey}), + @includeWhen(data_get($setUp, 'detail.state.' . $row->{$this->realPrimaryKey}), $rulesValues['detailView'][0]['detailView'], [ - 'id' => data_get($row, $primaryKey), + 'id' => data_get($row, $this->realPrimaryKey), 'options' => array_merge( data_get($setUp, 'detail.options'), $rulesValues['detailView']['0']['options']), ] ) @else - @includeWhen(data_get($setUp, 'detail.state.' . $row->{$primaryKey}), + @includeWhen(data_get($setUp, 'detail.state.' . $row->{$this->realPrimaryKey}), data_get($setUp, 'detail.view'), [ - 'id' => data_get($row, $primaryKey), + 'id' => data_get($row, $this->realPrimaryKey), 'options' => data_get($setUp, 'detail.options'), ] ) diff --git a/src/Concerns/Base.php b/src/Concerns/Base.php index c31ea2be8..5513c28fc 100644 --- a/src/Concerns/Base.php +++ b/src/Concerns/Base.php @@ -2,12 +2,15 @@ namespace PowerComponents\LivewirePowerGrid\Concerns; +use Livewire\Attributes\Computed; use PowerComponents\LivewirePowerGrid\ProcessDataSource; trait Base { public string $primaryKey = 'id'; + public ?string $primaryKeyAlias = null; + public bool $ignoreTablePrefix = true; public string $tableName = 'default'; @@ -44,6 +47,12 @@ trait Base public int $totalCurrentPage = 0; + #[Computed] + public function realPrimaryKey(): string + { + return $this->primaryKeyAlias ?? $this->primaryKey; + } + public function template(): ?string { return null; diff --git a/src/Concerns/ToggleDetail.php b/src/Concerns/ToggleDetail.php index f6e086050..f670bcdd9 100644 --- a/src/Concerns/ToggleDetail.php +++ b/src/Concerns/ToggleDetail.php @@ -29,7 +29,9 @@ private function resolveDetailRow(mixed $results): void $collection = !is_array($results) && method_exists($results, 'items') ? collect($results->items()) : collect($results); } - $collection->pluck($this->primaryKey) + $primaryKey = $this->primaryKeyAlias ?? $this->primaryKey; + + $collection->pluck($primaryKey) ->map(fn ($id) => strval($id)) ->each(function ($id) { $state = data_get($this->setUp, "detail.state.$id", false);