Skip to content

Commit

Permalink
Add primaryKeyAlias property
Browse files Browse the repository at this point in the history
  • Loading branch information
devsquad-luan-freitas committed Jul 18, 2024
1 parent d9ff65d commit 8eab0ae
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 20 deletions.
6 changes: 3 additions & 3 deletions resources/views/components/editable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -74,9 +74,9 @@
<div x-html="editableInput"></div>
</template>
@if ($showErrorBag)
@error($field . '.' . $row->{$primaryKey})
@error($field . '.' . $row->{$this->realPrimaryKey})
<div class="text-sm text-red-800 p-1 transition-all duration-200">
{{ str($message)->replace($field . '.' . $row->{$primaryKey}, $field) }}
{{ str($message)->replace($field . '.' . $row->{$this->realPrimaryKey}, $field) }}
</div>
@enderror
@endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<x-livewire-powergrid::editable
:tableName="$tableName"
:primaryKey="$primaryKey"
:primaryKey="$this->realPrimaryKey"
:row="$row"
:field="$field"
:theme="$theme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class="{{ data_get($theme, 'tdBodyClass') }}"
>
<div
class="cursor-pointer"
x-on:click.prevent="$wire.toggleDetail('{{ $row->{$primaryKey} }}')"
x-on:click.prevent="$wire.toggleDetail('{{ $row->{$this->realPrimaryKey} }}')"
>
@includeIf(data_get($setUp, 'detail.viewIcon'))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@php
$params = [
'id' => data_get($row, $primaryKey),
'id' => data_get($row, $this->realPrimaryKey),
'isHidden' => !$showToggleable,
'tableName' => $tableName,
'field' => $column->field,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<x-livewire-powergrid::editable
:tableName="$tableName"
:primaryKey="$primaryKey"
:primaryKey="$this->realPrimaryKey"
:row="$row"
:field="$field"
:theme="$theme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class="{{ data_get($theme, 'tdBodyClass') }}"
>
<div
class="cursor-pointer"
x-on:click.prevent="$wire.toggleDetail('{{ $row->{$primaryKey} }}')"
x-on:click.prevent="$wire.toggleDetail('{{ $row->{$this->realPrimaryKey} }}')"
>
@includeIf(data_get($setUp, 'detail.viewIcon'))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions resources/views/components/row.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<div class="pg-actions">
@if(empty(data_get($row, 'actions')) && $column->isAction)
@if (method_exists($this, 'actionsFromView') && $actionsFromView = $this->actionsFromView($row))
<div wire:key="actions-view-{{ data_get($row, $primaryKey) }}">
<div wire:key="actions-view-{{ data_get($row, $this->realPrimaryKey) }}">
{!! $actionsFromView !!}
</div>
@endif
Expand All @@ -80,7 +80,7 @@
@if (filled(data_get($row, 'actions')) && $column->isAction)
@foreach (data_get($row, 'actions') as $key => $action)
@if(filled($action))
<span wire:key="action-{{ data_get($row, $primaryKey) }}-{{ $key }}">
<span wire:key="action-{{ data_get($row, $this->realPrimaryKey) }}-{{ $key }}">
{!! $action !!}
</span>
@endif
Expand All @@ -92,7 +92,7 @@
$hasFieldRules = $actionRulesClass->recoverActionForField($row, $field);
// =============* Edit On Click *=====================
$showEditOnClick = false;
if (data_get($column->editable, 'hasPermission')) {
Expand Down Expand Up @@ -125,7 +125,7 @@
$showEditOnClick = false;
}
@endphp

@if($showEditOnClick === true)
<span @class([$contentClassField, $contentClass])>
@include(data_get($theme, 'editable.view') ?? null, ['editable' => $column->editable])
Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -96,7 +96,7 @@
<livewire:lazy-child
key="{{ $this->getLazyKeys }}"
:child-index="$item"
:$primaryKey
:primary-key="$this->realPrimaryKey"
:$radio
:$radioAttribute
:$checkbox
Expand Down
8 changes: 4 additions & 4 deletions resources/views/components/table/detail.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@

<td colspan="999">
@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'),
]
)
Expand Down
9 changes: 9 additions & 0 deletions src/Concerns/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Concerns/ToggleDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8eab0ae

Please sign in to comment.