Skip to content

Commit 4d3a161

Browse files
committed
add optgroup support
1 parent 7897869 commit 4d3a161

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

docs/examples/advanced-example.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ class UsersTable extends DataTableComponent
171171
$builder->where('active', false);
172172
}
173173
}),
174+
SelectFilter::make('Address Group')
175+
->options([
176+
'' => 'All',
177+
AddressGroup::query()
178+
->orderBy('type')
179+
->get()
180+
->groupBy('type')
181+
->map(fn ($addressGroup) => $addressGroup->pluck('type', 'id')->filter())
182+
->toArray(),
183+
])
184+
->filter(function(Builder $builder, string $value) {
185+
$builder->where('address_groups.type', $value);
186+
}),
174187
DateFilter::make('Verified From')
175188
->config([
176189
'min' => '2020-01-01',

docs/filters/creating-filters.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ public function filters(): array
3838

3939
You should supply the first option as the default value. I.e. nothing selected, so the filter is not applied. This value should be an empty string. When this value is selected, the filter will be removed from the query and the query string.
4040

41+
### Option Groups
42+
43+
To use `<optgroup>` elements, pass a nested array of options to the select filter.
44+
45+
```php
46+
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
47+
48+
public function filters(): array
49+
{
50+
return [
51+
SelectFilter::make('Active')
52+
->options([
53+
'' => 'All',
54+
'Open' => [
55+
1 => 'Type A',
56+
2 => 'Type B',
57+
3 => 'Type C',
58+
],
59+
'Closed' => [
60+
24 => 'Type X',
61+
25 => 'Type Y',
62+
26 => 'Type Z',
63+
],
64+
]),
65+
];
66+
}
67+
```
68+
4169
## Multi-select Filters
4270

4371
Multi-select filters are a list of checkboxes. The user can select multiple options from the list. There is also an 'All' option that will select all values.

resources/views/components/tools/filters/select.blade.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
class="block w-full border-gray-300 rounded-md shadow-sm transition duration-150 ease-in-out focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-gray-800 dark:text-white dark:border-gray-600"
1212
>
1313
@foreach($filter->getOptions() as $key => $value)
14-
<option value="{{ $key }}">{{ $value }}</option>
14+
@if (is_iterable($value))
15+
<optgroup label="{{ $key }}">
16+
@foreach ($value as $optionKey => $optionValue)
17+
<option value="{{ $optionKey }}">{{ $optionValue }}</option>
18+
@endforeach
19+
</optgroup>
20+
@else
21+
<option value="{{ $key }}">{{ $value }}</option>
22+
@endif
1523
@endforeach
1624
</select>
1725
</div>
@@ -23,7 +31,15 @@ class="block w-full border-gray-300 rounded-md shadow-sm transition duration-150
2331
class="form-control"
2432
>
2533
@foreach($filter->getOptions() as $key => $value)
26-
<option value="{{ $key }}">{{ $value }}</option>
34+
@if (is_iterable($value))
35+
<optgroup label="{{ $key }}">
36+
@foreach ($value as $optionKey => $optionValue)
37+
<option value="{{ $optionKey }}">{{ $optionValue }}</option>
38+
@endforeach
39+
</optgroup>
40+
@else
41+
<option value="{{ $key }}">{{ $value }}</option>
42+
@endif
2743
@endforeach
2844
</select>
2945
@endif

src/Views/Filters/SelectFilter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public function getOptions(): array
2424
public function getKeys(): array
2525
{
2626
return collect($this->getOptions())
27-
->keys()
27+
->map(fn ($value) => is_iterable($value) ? collect($value)->keys() : $value)
28+
->flatten()
2829
->map(fn ($value) => (string)$value)
2930
->filter(fn ($value) => strlen($value) > 0)
3031
->values()

tests/Views/Traits/Helpers/FilterHelpersTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ public function can_get_filter_keys(): void
5858
$this->assertSame(['foo'], $filter->getKeys());
5959
}
6060

61+
/** @test */
62+
public function can_get_nested_filter_keys(): void
63+
{
64+
$filter = SelectFilter::make('Active');
65+
66+
$this->assertSame([], $filter->getKeys());
67+
68+
$filter->options(['foo' => ['bar' => 'baz']]);
69+
70+
$this->assertSame(['bar'], $filter->getKeys());
71+
72+
$filter->options(['foo' => collect(['bar' => 'baz'])]);
73+
74+
$this->assertSame(['bar'], $filter->getKeys());
75+
}
76+
6177
/** @test */
6278
public function can_get_filter_default_value(): void
6379
{

0 commit comments

Comments
 (0)