Skip to content

Commit

Permalink
Added defaultFilters to Livewire Datatables (#527)
Browse files Browse the repository at this point in the history
Added defaultFilters to Livewire Datatables
  • Loading branch information
Cosnavel authored Nov 27, 2022
1 parent 0d39c09 commit c13a4d0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 6 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,34 @@ class CallbackDemoTable extends LivewireDatatable
}
```

### Default Filters

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.

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.

```php
class CallbackDemoTable extends LivewireDatatable
{
public $defaultFilters = [
'deleted_at' => '0',
];

public function builder()
{
return User::query()->withTrashed();
}

public function columns()
{
return [
Column::name('id'),
BooleanColumn::name('deleted_at')->filterable(),
];
}
}
```

### Views
You can specify that a column's output is piped directly into a separate blade view template.
- Template is specified using ususal laravel view helper syntax
Expand Down
76 changes: 70 additions & 6 deletions src/Http/Livewire/LivewireDatatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class LivewireDatatable extends Component
public $activeBooleanFilters = [];
public $activeTextFilters = [];
public $activeNumberFilters = [];
public $defaultFilters = [];
public $hideHeader;
public $hidePagination;
public $perPage;
Expand Down Expand Up @@ -265,6 +266,7 @@ public function mount(
$this->initialiseSearch();
$this->initialiseSort();
$this->initialiseHiddenColumns();
$this->initialiseDefaultFilters();
$this->initialiseFilters();
$this->initialisePerPage();
$this->initialiseColumnGroups();
Expand Down Expand Up @@ -587,6 +589,51 @@ public function initialiseColumnGroups()
}, $this->columns);
}

public function initialiseDefaultFilters()
{
if (! $this->defaultFilters || ! is_array($this->defaultFilters) || count($this->defaultFilters) === 0) {
return;
}

$columns = collect($this->columns);

foreach ($this->defaultFilters as $columnName => $value) {
$columnIndex = $columns->search(function ($column) use ($columnName) {
return $column['name'] === $columnName;
});

if ($columnIndex === false) {
continue;
}

$column = $columns[$columnIndex];

if ($column['type'] === 'string') {
$this->activeTextFilters[$columnIndex] = $value;
}

if ($column['type'] === 'boolean') {
$this->activeBooleanFilters[$columnIndex] = $value;
}

if ($column['type'] === 'select') {
$this->activeSelectFilters[$columnIndex] = $value;
}

if ($column['type'] === 'date') {
$this->activeDateFilters[$columnIndex] = $value;
}

if ($column['type'] === 'time') {
$this->activeTimeFilters[$columnIndex] = $value;
}

if ($column['type'] === 'number') {
$this->activeNumberFilters[$columnIndex] = $value;
}
}
}

public function initialiseFilters()
{
if (! $this->persistFilters) {
Expand All @@ -595,12 +642,29 @@ public function initialiseFilters()

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

$this->activeBooleanFilters = $filters['boolean'] ?? [];
$this->activeSelectFilters = $filters['select'] ?? [];
$this->activeTextFilters = $filters['text'] ?? [];
$this->activeDateFilters = $filters['date'] ?? [];
$this->activeTimeFilters = $filters['time'] ?? [];
$this->activeNumberFilters = $filters['number'] ?? [];
if (! empty($filters['text'])) {
$this->activeTextFilters = $filters['text'];
}

if (! empty($filters['boolean'])) {
$this->activeBooleanFilters = $filters['boolean'];
}

if (! empty($filters['select'])) {
$this->activeSelectFilters = $filters['select'];
}

if (! empty($filters['date'])) {
$this->activeDateFilters = $filters['date'];
}

if (! empty($filters['time'])) {
$this->activeTimeFilters = $filters['time'];
}

if (! empty($filters['number'])) {
$this->activeNumberFilters = $filters['number'];
}

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

0 comments on commit c13a4d0

Please sign in to comment.