Skip to content

Commit 50a73b1

Browse files
committed
Merge branch 'feature/numeric-filter-keys' into develop
2 parents 9dd8cc9 + 025093b commit 50a73b1

File tree

8 files changed

+86
-35
lines changed

8 files changed

+86
-35
lines changed

resources/views/bootstrap-4/includes/filter-pills.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showFilters && count(array_filter($filters)) && !(count(array_filter($filters)) === 1 && isset($filters['search'])))
1+
@if ($showFilters && count($this->getFiltersWithoutSearch()))
22
<div class="mb-3">
33
<small>@lang('Applied Filters'):</small>
44

resources/views/bootstrap-4/includes/filters.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<button type="button" class="btn dropdown-toggle d-block w-100 d-md-inline" data-toggle="dropdown">
44
@lang('Filters')
55

6-
@if (count(array_filter($filters)) && !(count(array_filter($filters)) === 1 && isset($filters['search'])))
6+
@if (count($this->getFiltersWithoutSearch()))
77
<span class="badge badge-info">
8-
{{ isset($filters['search']) ? count(array_filter($filters)) - 1 : count(array_filter($filters)) }}
8+
{{ count($this->getFiltersWithoutSearch()) }}
99
</span>
1010
@endif
1111

@@ -38,7 +38,7 @@ class="form-control"
3838
@endforeach
3939
@endif
4040

41-
@if (count(array_filter($filters)) && !(count(array_filter($filters)) === 1 && isset($filters['search'])))
41+
@if (count($this->getFiltersWithoutSearch()))
4242
<div class="dropdown-divider"></div>
4343

4444
<a

resources/views/bootstrap-5/includes/filter-pills.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showFilters && count(array_filter($filters)) && !(count(array_filter($filters)) === 1 && isset($filters['search'])))
1+
@if ($showFilters && count($this->getFiltersWithoutSearch()))
22
<div class="mb-3">
33
<small>@lang('Applied Filters'):</small>
44

resources/views/bootstrap-5/includes/filters.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<button type="button" class="btn dropdown-toggle d-block w-100 d-md-inline" data-bs-toggle="dropdown">
44
@lang('Filters')
55

6-
@if (count(array_filter($filters)) && !(count(array_filter($filters)) === 1 && isset($filters['search'])))
6+
@if (count($this->getFiltersWithoutSearch()))
77
<span class="badge bg-info">
8-
{{ isset($filters['search']) ? count(array_filter($filters)) - 1 : count(array_filter($filters)) }}
8+
{{ count($this->getFiltersWithoutSearch()) }}
99
</span>
1010
@endif
1111

@@ -38,7 +38,7 @@ class="form-control"
3838
@endforeach
3939
@endif
4040

41-
@if (count(array_filter($filters)) && !(count(array_filter($filters)) === 1 && isset($filters['search'])))
41+
@if (count($this->getFiltersWithoutSearch()))
4242
<div class="dropdown-divider"></div>
4343

4444
<a

resources/views/tailwind/includes/filter-pills.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showFilters && count(array_filter($filters)) && !(count(array_filter($filters)) === 1 && isset($filters['search'])))
1+
@if ($showFilters && count($this->getFiltersWithoutSearch()))
22
<div class="p-6 md:p-0">
33
<small class="text-gray-700">@lang('Applied Filters'):</small>
44

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class="relative block md:inline-block text-left"
1111
class="inline-flex justify-center w-full rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:border-indigo-300 focus:shadow-outline-indigo" id="filters-menu" @click="open = !open" aria-haspopup="true" x-bind:aria-expanded="open" aria-expanded="true">
1212
@lang('Filters')
1313

14-
@if (count(array_filter($filters)) && !(count(array_filter($filters)) === 1 && isset($filters['search'])))
14+
@if (count($this->getFiltersWithoutSearch()))
1515
<span class="ml-1 inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium leading-4 bg-indigo-100 text-indigo-800 capitalize">
16-
{{ isset($filters['search']) ? count(array_filter($filters)) - 1 : count(array_filter($filters)) }}
16+
{{ count($this->getFiltersWithoutSearch()) }}
1717
</span>
1818
@endif
1919

@@ -66,7 +66,7 @@ class="rounded-md shadow-sm block w-full pl-3 pr-10 py-2 text-base leading-6 bor
6666
@endforeach
6767
@endif
6868

69-
@if (count(array_filter($filters)) && !(count(array_filter($filters)) === 1 && isset($filters['search'])))
69+
@if (count($this->getFiltersWithoutSearch()))
7070
<div class="py-1" role="none">
7171
<div class="block px-4 py-2 text-sm text-gray-700" role="menuitem">
7272
<button

src/Traits/WithFilters.php

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ trait WithFilters
4040
*/
4141
public function mountWithFilters(): void
4242
{
43-
foreach ($this->filters() as $filter => $_default) {
44-
if (! isset($this->filters[$filter])) {
45-
$this->filters[$filter] = null;
46-
}
47-
}
43+
$this->checkFilters();
4844
}
4945

5046
/**
@@ -60,10 +56,19 @@ public function resetFilters(): void
6056
}
6157

6258
/**
63-
* Reset the page if the filters are updated
59+
* Runs when any filter is changed
6460
*/
65-
public function updatingFilters(): void
61+
public function updatedFilters(): void
6662
{
63+
// Remove the search filter when it's empty
64+
if (isset($this->filters['search']) && $this->filters['search'] === '') {
65+
$this->resetSearch();
66+
}
67+
68+
// Remove any url params that are empty
69+
$this->checkFilters();
70+
71+
// Reset the page when filters are changed
6772
$this->resetPage();
6873
}
6974

@@ -77,6 +82,18 @@ public function filters(): array
7782
return [];
7883
}
7984

85+
/**
86+
* Removes any filters that are empty
87+
*/
88+
public function checkFilters(): void
89+
{
90+
foreach ($this->filters() as $filter => $_default) {
91+
if (! isset($this->filters[$filter]) || $this->filters[$filter] === '') {
92+
$this->filters[$filter] = null;
93+
}
94+
}
95+
}
96+
8097
/**
8198
* Cleans $filter property of any values that don't exist
8299
* in the filter() definition.
@@ -140,19 +157,47 @@ public function filtersView(): ?string
140157
*/
141158
public function hasFilter(string $filter): bool
142159
{
143-
return isset($this->filters[$filter]) && $this->filters[$filter] !== null;
160+
return isset($this->filters[$filter]) && $this->filters[$filter] !== null && $this->filters[$filter] !== '';
144161
}
145162

146163
/**
147164
* Get the value of a given filter
148165
*
149166
* @param string $filter
150167
*
151-
* @return string|null
168+
* @return int|string|null
152169
*/
153-
public function getFilter(string $filter): ?string
170+
public function getFilter(string $filter)
154171
{
155-
return $this->filters[$filter] ?? null;
172+
if ($this->hasFilter($filter)) {
173+
if (in_array($filter, collect($this->filters())->keys()->toArray(), true) && $this->filters()[$filter]->isSelect()) {
174+
return $this->hasIntegerKeys($filter) ? (int)$this->filters[$filter] : trim($this->filters[$filter]);
175+
}
176+
177+
return trim($this->filters[$filter]);
178+
}
179+
180+
return null;
181+
}
182+
183+
/**
184+
* @return array
185+
*/
186+
public function getFilters(): array
187+
{
188+
return collect($this->filters)
189+
->reject(fn ($value) => $value === null || $value === '')
190+
->toArray();
191+
}
192+
193+
/**
194+
* @return array
195+
*/
196+
public function getFiltersWithoutSearch(): array
197+
{
198+
return collect($this->getFilters())
199+
->reject(fn ($_value, $key) => $key === 'search')
200+
->toArray();
156201
}
157202

158203
/**
@@ -176,7 +221,23 @@ public function removeFilter($filter): void
176221
*/
177222
public function getFilterOptions(string $filter): array
178223
{
179-
return array_filter(array_keys($this->filters()[$filter]->options()));
224+
return collect($this->filters()[$filter]->options())
225+
->keys()
226+
->reject(fn ($item) => $item === '' || $item === null)
227+
->values()
228+
->toArray();
229+
}
230+
231+
/**
232+
* Check whether the filter has numeric keys or not
233+
*
234+
* @param string $filter
235+
*
236+
* @return bool
237+
*/
238+
public function hasIntegerKeys(string $filter): bool
239+
{
240+
return is_int($this->getFilterOptions($filter)[0] ?? null);
180241
}
181242

182243
/**
@@ -200,7 +261,7 @@ public function applySearchFilter(Builder $query): Builder
200261
$searchableColumns = $this->getSearchableColumns();
201262

202263
if ($this->hasFilter('search') && count($searchableColumns)) {
203-
$search = trim($this->getFilter('search'));
264+
$search = $this->getFilter('search');
204265

205266
// Group search conditions together
206267
$query->where(function (Builder $subQuery) use ($search, $query, $searchableColumns) {

src/Traits/WithSearch.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ trait WithSearch
2929
*/
3030
public ?bool $searchFilterLazy = null;
3131

32-
/**
33-
* Remove the search filter when it's empty
34-
*/
35-
public function updatedFilters(): void
36-
{
37-
if (isset($this->filters['search']) && $this->filters['search'] === '') {
38-
$this->resetSearch();
39-
}
40-
}
41-
4232
/**
4333
* Clear the search filter specifically
4434
*/

0 commit comments

Comments
 (0)