Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickweh committed Aug 6, 2024
1 parent a9fdea6 commit 697e7f4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
17 changes: 14 additions & 3 deletions resources/views/components/layouts/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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">
<div class="flex gap-1.5">
<div class="flex gap-1.5 flex-wrap">
<div x-html="formatter(leftAppend[col], record)">
</div>
<div class="flex-grow">
<div x-html="formatter(topAppend[col], record)">
</div>
<div {{ $cellAttributes->merge(['x-html' => 'formatter(col, record)']) }}>
<div {{ $cellAttributes->merge(['x-html' => 'formatter(col, record)']) }}>
</div>
<div x-html="formatter(bottomAppend[col], record)">
</div>
Expand All @@ -236,11 +237,21 @@ class="cursor-pointer"
</template>
@if($rowActions ?? false)
<td class="border-b border-slate-200 dark:border-slate-600 whitespace-nowrap px-3 py-4">
<div class="flex gap-1.5">
<div class="flex gap-1.5" @if($allowSoftDeletes) x-bind:class="record.deleted_at ? 'hidden' : ''" @endif>
@foreach($rowActions as $rowAction)
{{ $rowAction }}
@endforeach
</div>
@if($showRestoreButton && $allowSoftDeletes)
<div class="flex gap-1.5" x-show="record.deleted_at">
<x-button
primary
wire:click="restore(record.id)"
>
{{ __('Restore') }}
</x-button>
</div>
@endif
</td>
@endif
{{-- Empty cell for the col selection--}}
Expand Down
1 change: 1 addition & 0 deletions resources/views/components/options.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.') }}
</div>
<x-checkbox x-model="$wire.$parent.withSoftDeletes" x-on:change="$wire.$parent.$call('startSearch')" :label="__('Include deleted')" />
<x-button
wire:target="loadFields"
wire:loading.attr="disabled"
Expand Down
3 changes: 3 additions & 0 deletions resources/views/livewire/data-table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
:model-name="$modelName"
:table-actions="$tableActions"
:headline="$headline"
:allow-soft-deletes="$allowSoftDeletes"
/>
@if($actions ?? false)
<x-dropdown>
Expand All @@ -43,5 +44,7 @@
:use-wire-navigate="$useWireNavigate"
:is-selectable="$isSelectable"
:select-value="$selectValue"
:allow-soft-deletes="$allowSoftDeletes"
:show-restore-button="$showRestoreButton"
/>
</x-tall-datatables::data-table-wrapper>
27 changes: 26 additions & 1 deletion src/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'] : []
));
}

Expand Down Expand Up @@ -846,6 +869,8 @@ protected function getViewData(): array
'colLabels' => $this->colLabels,
'includeBefore' => $this->includeBefore,
'selectValue' => $this->getSelectValue(),
'allowSoftDeletes' => $this->allowSoftDeletes(),
'showRestoreButton' => $this->showRestoreButton(),
];
}

Expand Down
3 changes: 3 additions & 0 deletions src/Livewire/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 697e7f4

Please sign in to comment.