Skip to content

Commit aa7c64a

Browse files
committedMar 15, 2025
refactor filter toggle logic to use Alpine.js for improved interactivity and state management
1 parent f2a1e1a commit aa7c64a

File tree

3 files changed

+5
-10
lines changed

3 files changed

+5
-10
lines changed
 

‎resources/views/datatable.blade.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<script src="{{ $jsAssetsUrl }}" defer></script>
44
@endassets
55

6-
<div class="card datatable" @style(['max-height: ' . $height]) id="{{ $id }}" wire:ignore.self>
6+
<div class="card datatable" @style(['max-height: ' . $height]) id="{{ $id }}" wire:ignore.self x-data="{ filtersOpen: {{ $filtersOpen ? 'true' : 'false' }} }">
77
<div class="card-body d-flex align-items-center gap-1">
88
<div class="w-auto me-auto" wire:ignore>
99
@include('datatables::partials.per-page')
@@ -32,8 +32,8 @@
3232
</div>
3333
</div>
3434

35-
@if ($filterable && $showFilters)
36-
<div class="card-body" wire:ignore>
35+
@if ($filterable)
36+
<div class="card-body" wire:ignore x-show="filtersOpen">
3737
@include('datatables::partials.filters')
3838
</div>
3939
@endif

‎resources/views/partials/filters-toggle.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<button type="button" @class(['btn btn-icon', 'active' => $showFilters]) wire:click="$toggle('showFilters')">
1+
<button type="button" :class="{ 'active': filtersOpen }" class="btn btn-icon" x-on:click="filtersOpen = !filtersOpen">
22
<i class="fas fa-filter"></i>
33
</button>
44

‎src/Datatable.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ abstract class Datatable extends Component
6868
#[Url(as: 'filter')]
6969
public array $filtered = [];
7070

71-
/**
72-
* Toggle filters visibility.
73-
*/
74-
#[Url]
75-
public bool $showFilters = false;
76-
7771
/**
7872
* Set the datatable maximum height.
7973
*/
@@ -327,6 +321,7 @@ public function viewData(): array
327321
'actions' => $actions,
328322

329323
'colspan' => $this->getColspanForColumns($columns, $actions),
324+
'filtersOpen' => count($this->filtered) > 0,
330325

331326
'filterable' => count($filters) > 0,
332327
'searchable' => count(array_filter($columns, fn (Column $column) => $column->searchable)) > 0,

0 commit comments

Comments
 (0)