Skip to content

Commit f016b74

Browse files
authored
Merge pull request #105 from elfeffe/defaultvalue
Added Default Value for filters
2 parents d7a59fc + b1d52a1 commit f016b74

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Filters/BaseFilter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class BaseFilter
99
{
1010
protected $title;
11+
public $defaultValue;
1112
public $id;
1213

1314
public function __construct()

src/Views/DataView.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,27 @@ public function mount(QueryStringData $queryStringData)
5454
{
5555
$this->filtersViews = $this->filters();
5656
$this->search = $queryStringData->getSearchValue($this->search);
57+
5758
$this->filters = $queryStringData->getFilterValues($this->filters);
59+
60+
$this->applyDefaultFilters();
61+
5862
$this->sortBy = $queryStringData->getValue('sortBy', $this->sortBy);
5963
$this->sortOrder = $queryStringData->getValue('sortOrder', $this->sortOrder);
6064
}
6165

66+
/**
67+
* Check if each of the filters has a default value and it's not already set
68+
*/
69+
public function applyDefaultFilters()
70+
{
71+
foreach ($this->filters() as $filter) {
72+
if (empty($this->filters[$filter->id]) && $filter->defaultValue) {
73+
$this->filters[$filter->id] = $filter->defaultValue;
74+
}
75+
}
76+
}
77+
6278
/**
6379
* Collects all data to be passed to the view, this includes the items searched on the database
6480
* through the filters, this data will be passed to livewire render method
@@ -80,6 +96,7 @@ public function getItems(Searchable $searchable, Filterable $filterable, Sortabl
8096
$query = $this->repository();
8197

8298
$query = $searchable->searchItems($query, $this->searchBy, $this->search);
99+
83100
$query = $filterable->applyFilters($query, $this->filters(), $this->filters);
84101
$query = $sortable->sortItems($query, $this->sortBy, $this->sortOrder);
85102

0 commit comments

Comments
 (0)