Skip to content

Commit c13a4d0

Browse files
authored
Added defaultFilters to Livewire Datatables (#527)
Added defaultFilters to Livewire Datatables
1 parent 0d39c09 commit c13a4d0

File tree

2 files changed

+98
-6
lines changed

2 files changed

+98
-6
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,34 @@ class CallbackDemoTable extends LivewireDatatable
413413
}
414414
```
415415

416+
### Default Filters
417+
418+
If you want to have a default filter applied to your table, you can use the `defaultFilters` property. The `defaultFilter` should be an Array of column names and the default filter value to use for. When a persisted filter (`$this->persistFilters` is true and session values are available) is available, it will override the default filters.
419+
420+
In the example below, the table will by default be filtered by rows where the _deleted_at_ column is false. If the user has a persisted filter for the _deleted_at_ column, the default filter will be ignored.
421+
422+
```php
423+
class CallbackDemoTable extends LivewireDatatable
424+
{
425+
public $defaultFilters = [
426+
'deleted_at' => '0',
427+
];
428+
429+
public function builder()
430+
{
431+
return User::query()->withTrashed();
432+
}
433+
434+
public function columns()
435+
{
436+
return [
437+
Column::name('id'),
438+
BooleanColumn::name('deleted_at')->filterable(),
439+
];
440+
}
441+
}
442+
```
443+
416444
### Views
417445
You can specify that a column's output is piped directly into a separate blade view template.
418446
- Template is specified using ususal laravel view helper syntax

src/Http/Livewire/LivewireDatatable.php

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class LivewireDatatable extends Component
3636
public $activeBooleanFilters = [];
3737
public $activeTextFilters = [];
3838
public $activeNumberFilters = [];
39+
public $defaultFilters = [];
3940
public $hideHeader;
4041
public $hidePagination;
4142
public $perPage;
@@ -265,6 +266,7 @@ public function mount(
265266
$this->initialiseSearch();
266267
$this->initialiseSort();
267268
$this->initialiseHiddenColumns();
269+
$this->initialiseDefaultFilters();
268270
$this->initialiseFilters();
269271
$this->initialisePerPage();
270272
$this->initialiseColumnGroups();
@@ -587,6 +589,51 @@ public function initialiseColumnGroups()
587589
}, $this->columns);
588590
}
589591

592+
public function initialiseDefaultFilters()
593+
{
594+
if (! $this->defaultFilters || ! is_array($this->defaultFilters) || count($this->defaultFilters) === 0) {
595+
return;
596+
}
597+
598+
$columns = collect($this->columns);
599+
600+
foreach ($this->defaultFilters as $columnName => $value) {
601+
$columnIndex = $columns->search(function ($column) use ($columnName) {
602+
return $column['name'] === $columnName;
603+
});
604+
605+
if ($columnIndex === false) {
606+
continue;
607+
}
608+
609+
$column = $columns[$columnIndex];
610+
611+
if ($column['type'] === 'string') {
612+
$this->activeTextFilters[$columnIndex] = $value;
613+
}
614+
615+
if ($column['type'] === 'boolean') {
616+
$this->activeBooleanFilters[$columnIndex] = $value;
617+
}
618+
619+
if ($column['type'] === 'select') {
620+
$this->activeSelectFilters[$columnIndex] = $value;
621+
}
622+
623+
if ($column['type'] === 'date') {
624+
$this->activeDateFilters[$columnIndex] = $value;
625+
}
626+
627+
if ($column['type'] === 'time') {
628+
$this->activeTimeFilters[$columnIndex] = $value;
629+
}
630+
631+
if ($column['type'] === 'number') {
632+
$this->activeNumberFilters[$columnIndex] = $value;
633+
}
634+
}
635+
}
636+
590637
public function initialiseFilters()
591638
{
592639
if (! $this->persistFilters) {
@@ -595,12 +642,29 @@ public function initialiseFilters()
595642

596643
$filters = session()->get($this->sessionStorageKey() . '_filter');
597644

598-
$this->activeBooleanFilters = $filters['boolean'] ?? [];
599-
$this->activeSelectFilters = $filters['select'] ?? [];
600-
$this->activeTextFilters = $filters['text'] ?? [];
601-
$this->activeDateFilters = $filters['date'] ?? [];
602-
$this->activeTimeFilters = $filters['time'] ?? [];
603-
$this->activeNumberFilters = $filters['number'] ?? [];
645+
if (! empty($filters['text'])) {
646+
$this->activeTextFilters = $filters['text'];
647+
}
648+
649+
if (! empty($filters['boolean'])) {
650+
$this->activeBooleanFilters = $filters['boolean'];
651+
}
652+
653+
if (! empty($filters['select'])) {
654+
$this->activeSelectFilters = $filters['select'];
655+
}
656+
657+
if (! empty($filters['date'])) {
658+
$this->activeDateFilters = $filters['date'];
659+
}
660+
661+
if (! empty($filters['time'])) {
662+
$this->activeTimeFilters = $filters['time'];
663+
}
664+
665+
if (! empty($filters['number'])) {
666+
$this->activeNumberFilters = $filters['number'];
667+
}
604668

605669
if (isset($filters['search'])) {
606670
$this->search = $filters['search'];

0 commit comments

Comments
 (0)