Skip to content

Commit 3cb7426

Browse files
yphojuliomotol-hdmd
authored andcommitted
Added multiselect filter
1 parent 3c12c47 commit 3cb7426

File tree

7 files changed

+88
-7
lines changed

7 files changed

+88
-7
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@
44
<small>@lang('Applied Filters'):</small>
55

66
@foreach($filters as $key => $value)
7-
@if ($key !== 'search' && strlen($value))
7+
@if ($key !== 'search' && (is_array($value) || strlen($value)))
88
<span
99
wire:key="filter-pill-{{ $key }}"
1010
class="badge badge-pill badge-info d-inline-flex align-items-center"
1111
>
1212
{{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}:
13+
1314
@if(isset($customFilters[$key]) && method_exists($customFilters[$key], 'options'))
14-
{{ $customFilters[$key]->options()[$value] ?? $value }}
15+
@if(is_array($value))
16+
@foreach($value as $selectedValue)
17+
{{ $customFilters[$key]->options()[$selectedValue] ?? $selectedValue }}@if(!$loop->last), @endif
18+
@endforeach
19+
@else
20+
{{ $customFilters[$key]->options()[$value] ?? $value }}
21+
@endif
22+
@elseif(is_array($value))
23+
{{ implode(', ', $value) }}
1524
@else
1625
{{ ucwords(strtr($value, ['_' => ' ', '-' => ' '])) }}
1726
@endif

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@
44
<small>@lang('Applied Filters'):</small>
55

66
@foreach($filters as $key => $value)
7-
@if ($key !== 'search' && strlen($value))
7+
@if ($key !== 'search' && (is_array($value) || strlen($value)))
88
<span
99
wire:key="filter-pill-{{ $key }}"
1010
class="badge rounded-pill bg-info d-inline-flex align-items-center"
1111
>
1212
{{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}:
13+
1314
@if(isset($customFilters[$key]) && method_exists($customFilters[$key], 'options'))
14-
{{ $customFilters[$key]->options()[$value] ?? $value }}
15+
@if(is_array($value))
16+
@foreach($value as $selectedValue)
17+
{{ $customFilters[$key]->options()[$selectedValue] ?? $selectedValue }}@if(!$loop->last), @endif
18+
@endforeach
19+
@else
20+
{{ $customFilters[$key]->options()[$value] ?? $value }}
21+
@endif
22+
@elseif(is_array($value))
23+
{{ implode(', ', $value) }}
1524
@else
1625
{{ ucwords(strtr($value, ['_' => ' ', '-' => ' '])) }}
1726
@endif

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@
44
<small class="text-gray-700 dark:text-white">@lang('Applied Filters'):</small>
55

66
@foreach($filters as $key => $value)
7-
@if ($key !== 'search' && strlen($value))
7+
@if ($key !== 'search' && (is_array($value) || strlen($value)))
88
<span
99
wire:key="filter-pill-{{ $key }}"
1010
class="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 dark:bg-indigo-200 dark:text-indigo-900"
1111
>
1212
{{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}:
13+
1314
@if(isset($customFilters[$key]) && method_exists($customFilters[$key], 'options'))
14-
{{ $customFilters[$key]->options()[$value] ?? $value }}
15+
@if(is_array($value))
16+
@foreach($value as $selectedValue)
17+
{{ $customFilters[$key]->options()[$selectedValue] ?? $selectedValue }}@if(!$loop->last), @endif
18+
@endforeach
19+
@else
20+
{{ $customFilters[$key]->options()[$value] ?? $value }}
21+
@endif
22+
@elseif(is_array($value))
23+
{{ implode(', ', $value) }}
1524
@else
1625
{{ ucwords(strtr($value, ['_' => ' ', '-' => ' '])) }}
1726
@endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div class="mt-1 relative rounded-md shadow-sm">
2+
<select
3+
wire:model.stop="filters.{{ $key }}"
4+
wire:key="filter-{{ $key }}"
5+
id="filter-{{ $key }}"
6+
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"
7+
multiple="multiple"
8+
>
9+
@foreach($filter->options() as $key => $value)
10+
<option value="{{ $key }}">{{ $value }}</option>
11+
@endforeach
12+
</select>
13+
</div>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class="block text-sm font-medium leading-5 text-gray-700 dark:text-white">
5858

5959
@if ($filter->isSelect())
6060
@include('livewire-tables::tailwind.includes.filter-type-select')
61+
@elseif($filter->isMultiSelect())
62+
@include('livewire-tables::tailwind.includes.filter-type-multiselect')
6163
@elseif($filter->isDate())
6264
@include('livewire-tables::tailwind.includes.filter-type-date')
6365
@endif

src/Traits/WithFilters.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ public function cleanFilters(): void
161161
}
162162
}
163163

164+
// Handle 'multiselect' filters
165+
if ($filterDefinitions[$filterName]->isMultiSelect() && is_array($filterValue)) {
166+
foreach ($filterValue as $selectedValue) {
167+
if (!in_array($selectedValue, $this->getFilterOptions($filterName))) {
168+
return false;
169+
}
170+
}
171+
172+
return true;
173+
}
174+
164175
if ($filterDefinitions[$filterName]->isDate()) {
165176
// array_sum trick is a terse way of ensuring that PHP
166177
// did not do "month shifting"
@@ -210,7 +221,11 @@ public function getFilter(string $filter)
210221
return $this->hasIntegerKeys($filter) ? (int)$this->filters[$filter] : trim($this->filters[$filter]);
211222
}
212223

213-
return trim($this->filters[$filter]);
224+
if(is_string($this->filters[$filter])) {
225+
return trim($this->filters[$filter]);
226+
}
227+
228+
return $this->filters[$filter];
214229
}
215230

216231
return null;

src/Views/Filter.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Filter
1111

1212
public const TYPE_SELECT = 'select';
1313

14+
public const TYPE_MULTISELECT = 'multiselect';
15+
1416
/**
1517
* @var string
1618
*/
@@ -60,6 +62,20 @@ public function select(array $options = []): Filter
6062
return $this;
6163
}
6264

65+
/**
66+
* @param array $options
67+
*
68+
* @return $this
69+
*/
70+
public function multiSelect(array $options = []): Filter
71+
{
72+
$this->type = self::TYPE_MULTISELECT;
73+
74+
$this->options = $options;
75+
76+
return $this;
77+
}
78+
6379
/**
6480
* @param array $options
6581
*
@@ -98,6 +114,14 @@ public function isSelect(): bool
98114
return $this->type === self::TYPE_SELECT;
99115
}
100116

117+
/**
118+
* @return bool
119+
*/
120+
public function isMultiSelect(): bool
121+
{
122+
return $this->type === self::TYPE_MULTISELECT;
123+
}
124+
101125
/**
102126
* @return bool
103127
*/

0 commit comments

Comments
 (0)