Skip to content

Commit 8f8fc73

Browse files
authored
Merge pull request #214 from rappasoft/DeltaSystems-feature-autodefine-filters-property
Delta systems feature autodefine filters property
2 parents 52afdf9 + f05b678 commit 8f8fc73

File tree

5 files changed

+116
-27
lines changed

5 files changed

+116
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
88

99
### Added
1010

11-
- `$searchFilterDebounce`, `$searchFilterDefer`, `$searchFilterLazy`, for defining the search input data binding property.
11+
- `$searchFilterDebounce`, `$searchFilterDefer`, `$searchFilterLazy`, for defining the search input data binding property. https://github.com/rappasoft/laravel-livewire-tables/pull/211
12+
- Remove ability to need to define filters if not defining defaults. https://github.com/rappasoft/laravel-livewire-tables/pull/213
1213

1314
## [1.0.3] - 2021-04-18
1415

README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -234,22 +234,7 @@ You will see how to get more out of this base query using filters and search bel
234234

235235
Creating filters is not required, and the filters box will be hidden if none are defined.
236236

237-
Creating filters requires a few easy steps.
238-
239-
#### Adding to the filters array
240-
241-
You must first define a filter key in the **$filters** array, this tells the component to save the filter status in the query string for page reloads, as well as let you set a default.
242-
243-
```php
244-
public array $filters = [
245-
'type' => null,
246-
'active' => null,
247-
];
248-
```
249-
250-
#### Defining the filter UI
251-
252-
After you define the filters for the component, you must specify their options using the **filters()** method.
237+
#### Defining the filters
253238

254239
Right now the only supported filter type is a select dropdown.
255240

@@ -273,10 +258,21 @@ public function filters(): array
273258
}
274259
```
275260

276-
You specify your filters array using the **key** as the filter name supplied in the **$filters** array on the component.
277-
278261
The keys of the options you supply will be validated on select to make sure they match one of the options on the backend, otherwise it will be changed to _null_ for safety.
279262

263+
#### Setting filter defaults
264+
265+
When you define the filters, a class property of $filters is generated for you.
266+
267+
```php
268+
public array $filters = [
269+
'type' => null,
270+
'active' => null,
271+
];
272+
```
273+
274+
You may overwrite this property to set defaults to your filters if you would like.
275+
280276
#### Alternate: Defining a filter view
281277

282278
If you want full control over your filters, you can omit the **filters()** method and instead add a **filtersView()** method that return the string view name, which will be included in the master component on render. This is useful when you have different types of filters than the package offers:

resources/views/tailwind/includes/filters.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class="origin-top-right absolute right-0 mt-2 w-full md:w-56 rounded-md shadow-l
5151
<div class="mt-1 relative rounded-md shadow-sm">
5252
<select
5353
wire:model="filters.{{ $key }}"
54+
wire:key="filter-{{ $key }}"
5455
id="filter-{{ $key }}"
5556
class="rounded-md shadow-sm block w-full pl-3 pr-10 py-2 text-base leading-6 border-gray-300 focus:outline-none focus:border-indigo-300 focus:shadow-outline-indigo sm:text-sm sm:leading-5"
5657
>

src/DataTableComponent.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public function __construct($id = null)
136136
parent::__construct($id);
137137

138138
$theme = config('livewire-tables.theme');
139+
139140
if ($theme === 'bootstrap-4' || $theme === 'bootstrap-5') {
140141
$this->paginationTheme = 'bootstrap';
141142
}

src/Traits/WithFilters.php

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ trait WithFilters
4545
*/
4646
public ?bool $searchFilterLazy = null;
4747

48+
/**
49+
* Prebuild the $filters array
50+
*/
51+
public function mountWithFilters(): void
52+
{
53+
foreach ($this->filters() as $filter => $default) {
54+
if (! isset($this->filters[$filter])) {
55+
$this->filters[$filter] = null;
56+
}
57+
}
58+
}
59+
4860
/**
4961
* Build Livewire model options for the search input
5062
*
@@ -67,65 +79,143 @@ public function getSearchFilterOptionsProperty(): string
6779
return '';
6880
}
6981

82+
/**
83+
* Reset the filters array but keep the value for search
84+
*/
7085
public function resetFilters(): void
7186
{
7287
$search = $this->filters['search'] ?? null;
88+
7389
$this->reset('filters');
90+
7491
$this->filters['search'] = $search;
7592
}
7693

94+
/**
95+
* Reset the page if the filters are updated
96+
*/
7797
public function updatingFilters(): void
7898
{
7999
$this->resetPage();
80100
}
81101

102+
/**
103+
* Reset all the filters
104+
*/
82105
public function resetAll(): void
83106
{
84107
$this->resetFilters();
85108
$this->resetSorts();
86109
$this->resetBulk();
87110
}
88111

112+
/**
113+
* Define the filters array
114+
*
115+
* @return array
116+
*/
89117
public function filters(): array
90118
{
91119
return [];
92120
}
93121

122+
/**
123+
* Cleans $filter property of any values that don't exist
124+
* in the filter() definition.
125+
*/
94126
public function cleanFilters(): void
95127
{
96-
foreach ($this->filters() as $key => $filter) {
97-
if (
98-
$filter->isSelect() &&
99-
$this->hasFilter($key) &&
100-
! in_array($this->getFilter($key), $this->getFilterOptions($key), true)
101-
) {
102-
$this->removeFilter($key);
128+
// Filter $filters values
129+
$this->filters = collect($this->filters)->filter(function ($filterValue, $filterName) {
130+
$filterDefinitions = $this->filters();
131+
132+
// Ignore search
133+
if ($filterName === 'search') {
134+
return true;
103135
}
104-
}
136+
137+
// Filter out any keys that weren't defined as a filter
138+
if (! isset($filterDefinitions[$filterName])) {
139+
return false;
140+
}
141+
142+
// Ignore null values
143+
if (is_null($filterValue)) {
144+
return true;
145+
}
146+
147+
// Handle 'select' filters
148+
if ($filterDefinitions[$filterName]->isSelect()) {
149+
foreach ($this->getFilterOptions($filterName) as $optionValue) {
150+
// If the option is an integer, typecast filter value
151+
if (is_int($optionValue) && $optionValue === (int)$filterValue) {
152+
return true;
153+
}
154+
155+
// Strict check the value
156+
if ($optionValue === $filterValue) {
157+
return true;
158+
}
159+
}
160+
}
161+
162+
return false;
163+
})->toArray();
105164
}
106165

166+
/**
167+
* Define the string location to a view to be included as the filters view
168+
*
169+
* @return string|null
170+
*/
107171
public function filtersView(): ?string
108172
{
109173
return null;
110174
}
111175

176+
/**
177+
* Check if a filter exists and isn't null
178+
*
179+
* @param string $filter
180+
*
181+
* @return bool
182+
*/
112183
public function hasFilter(string $filter): bool
113184
{
114185
return isset($this->filters[$filter]) && $this->filters[$filter] !== null;
115186
}
116187

188+
/**
189+
* Get the value of a given filter
190+
*
191+
* @param string $filter
192+
*
193+
* @return string|null
194+
*/
117195
public function getFilter(string $filter): ?string
118196
{
119197
return $this->filters[$filter] ?? null;
120198
}
121199

200+
/**
201+
* Set a given filter to null
202+
*
203+
* @param $filter
204+
*/
122205
public function removeFilter($filter): void
123206
{
124207
if (isset($this->filters[$filter])) {
125208
$this->filters[$filter] = null;
126209
}
127210
}
128211

212+
/**
213+
* Get the options for a given filter if they exist
214+
*
215+
* @param string $filter
216+
*
217+
* @return array
218+
*/
129219
public function getFilterOptions(string $filter): array
130220
{
131221
return array_filter(array_keys($this->filters()[$filter]->options() ?? []));

0 commit comments

Comments
 (0)