From 697e7f4bd023904893110c6c6187d4965d7694a8 Mon Sep 17 00:00:00 2001 From: Patrick Weh <40495041+patrickweh@users.noreply.github.com> Date: Tue, 6 Aug 2024 12:03:03 +0200 Subject: [PATCH] wip --- .../views/components/layouts/table.blade.php | 17 +++++++++--- resources/views/components/options.blade.php | 1 + resources/views/livewire/data-table.blade.php | 3 +++ src/DataTable.php | 27 ++++++++++++++++++- src/Livewire/Options.php | 3 +++ 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/resources/views/components/layouts/table.blade.php b/resources/views/components/layouts/table.blade.php index 1903b15..ef4df9d 100644 --- a/resources/views/components/layouts/table.blade.php +++ b/resources/views/components/layouts/table.blade.php @@ -194,6 +194,7 @@ class="h-24 w-24 m-auto" x-bind:data-id="record.id" x-bind:key="record.id" x-on:click="$dispatch('data-table-row-clicked', record)" + x-bind:class="record.deleted_at ? 'opacity-50' : ''" {{ $rowAttributes->merge(['class' => 'hover:bg-gray-100 dark:hover:bg-secondary-900']) }} > @if($isSelectable) @@ -218,13 +219,13 @@ class="h-24 w-24 m-auto" x-bind:style="stickyCols.includes(col) && 'z-index: 2'" class="cursor-pointer" x-bind:href="record?.href ?? false"> -
+
-
merge(['x-html' => 'formatter(col, record)']) }}> +
merge(['x-html' => 'formatter(col, record)']) }}>
@@ -236,11 +237,21 @@ class="cursor-pointer" @if($rowActions ?? false) -
+
@foreach($rowActions as $rowAction) {{ $rowAction }} @endforeach
+ @if($showRestoreButton && $allowSoftDeletes) +
+ + {{ __('Restore') }} + +
+ @endif @endif {{-- Empty cell for the col selection--}} diff --git a/resources/views/components/options.blade.php b/resources/views/components/options.blade.php index 6510d1b..83d2309 100644 --- a/resources/views/components/options.blade.php +++ b/resources/views/components/options.blade.php @@ -367,6 +367,7 @@ class="text-xs text-slate-400 break-long-words max-w-md" > {{ __('When using the like or not like filter, you can use the % sign as a placeholder. Examples: "test%" for values that start with "test", "%test" for values that end with "test", and "%test%" for values that contain "test" anywhere.') }}
+ @if($actions ?? false) @@ -43,5 +44,7 @@ :use-wire-navigate="$useWireNavigate" :is-selectable="$isSelectable" :select-value="$selectValue" + :allow-soft-deletes="$allowSoftDeletes" + :show-restore-button="$showRestoreButton" /> diff --git a/src/DataTable.php b/src/DataTable.php index 6886d85..16b6100 100755 --- a/src/DataTable.php +++ b/src/DataTable.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\QueryException; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Arr; @@ -54,6 +55,8 @@ class DataTable extends Component #[Locked] public array $filters = []; + public bool $withSoftDeletes = false; + /** * These are the columns that will be available to the user. */ @@ -208,6 +211,21 @@ protected function getEnabledCols(): array return $this->enabledCols; } + protected function allowSoftDeletes(): bool + { + return in_array( + SoftDeletes::class, + class_uses_recursive( + $this->getModel() + ) + ); + } + + protected function showRestoreButton(): bool + { + return method_exists(static::class, 'restore'); + } + #[Renderless] public function getAvailableCols(): array { @@ -462,6 +480,10 @@ protected function buildSearch(): Builder $query = $model::query(); } + if ($this->withSoftDeletes && $this->allowSoftDeletes()) { + $query->withTrashed(); + } + if ($this->userOrderBy) { $orderBy = $this->userOrderBy; $orderAsc = $this->userOrderAsc; @@ -776,7 +798,8 @@ protected function getReturnKeys(): array { return array_filter(array_merge( $this->enabledCols, - [$this->modelKeyName, 'href'] + [$this->modelKeyName, 'href'], + $this->withSoftDeletes ? ['deleted_at'] : [] )); } @@ -846,6 +869,8 @@ protected function getViewData(): array 'colLabels' => $this->colLabels, 'includeBefore' => $this->includeBefore, 'selectValue' => $this->getSelectValue(), + 'allowSoftDeletes' => $this->allowSoftDeletes(), + 'showRestoreButton' => $this->showRestoreButton(), ]; } diff --git a/src/Livewire/Options.php b/src/Livewire/Options.php index eb969ac..7ba48f9 100644 --- a/src/Livewire/Options.php +++ b/src/Livewire/Options.php @@ -17,6 +17,9 @@ class Options extends Component #[Locked] public ?array $aggregatable = null; + #[Locked] + public ?bool $allowSoftDeletes = null; + public function render(): View { return view('tall-datatables::components.options');