Skip to content

Commit b78c381

Browse files
authored
Merge pull request #764 from rappasoft/inmanturbo-develop
Column Filters
2 parents 08810f3 + 8b6946a commit b78c381

File tree

16 files changed

+521
-170
lines changed

16 files changed

+521
-170
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ phpunit.xml
99
psalm.xml
1010
testbench.yaml
1111
vendor
12-
output.txt
12+
output.txt
13+
.vscode

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
44

55
## [Unreleased]
66

7+
## [dev-develop] - 2022-05-05
8+
9+
### Added
10+
11+
- Added functionality to hide individual filters from popover and slide down views
12+
- Added functionality to hide individual filters from filter pills
13+
- Added functionality to hide individual filters from the active filter count
14+
- Added functionality to say which filters get reset by the clear button
15+
- Added functionality to set filters as secondaryHeader or footer of columns
16+
717
## [2.6.0] - 2022-05-05
818

919
### Added

docs/columns/footer.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,22 @@ Column::make('Price')
1717

1818
The footer row is enabled when ever any column calls `footer`.
1919

20-
See also [footer component configuration](../footer/available-methods).
20+
See also [footer component configuration](../footer/available-methods).
21+
22+
## Using a filter as a footer
23+
24+
As of version 2.7, you can use a filter as a footer.
25+
26+
```php
27+
// Example filter
28+
SelectFilter::make('Active')
29+
->hiddenFromAll(), // Optional, hides the filter from the menus, pills, count.
30+
31+
// You can pass a filter directly
32+
Column::make('Active')
33+
->footer($this->getFilterByKey('active')),
34+
35+
// Or use the shorthand method
36+
Column::make('Active')
37+
->footerFilter('active'), // Takes the key from the filter, which you can find in the query string when the filter is applied.
38+
```

docs/columns/secondary-header.md

Lines changed: 11 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -19,97 +19,20 @@ The secondary header row is enabled when ever any column calls `secondaryHeader`
1919

2020
See also [secondary header component configuration](../secondary-header/available-methods).
2121

22-
## Example: Adding a column search
22+
## Using a filter as a secondary header
2323

24-
Column search is not built in by default, but is easily achievable using this feature.
25-
26-
Here are the steps you need to take:
27-
28-
1. Add state to the component to track the input values
29-
2. Add the inputs to the columns using the secondaryHeader method
30-
3. Add the clause to the query based on the new state
31-
32-
### 1. Add state to the component to track the input values
33-
34-
This can be called whatever you want, but you need an array to house the values of the column search fields:
35-
36-
```php
37-
public $columnSearch = [
38-
'name' => null,
39-
];
40-
```
41-
42-
### 2. Add the inputs to the columns using the secondaryHeader method
43-
44-
You can do this inline, but I'll break it out into a partial for clarity:
24+
As of version 2.7, you can use a filter as a header.
4525

4626
```php
47-
Column::make('Name')
48-
->sortable()
49-
->searchable()
50-
->asHtml()
51-
->secondaryHeader(
52-
fn() => view('tables.cells.input-search', ['field' => 'name']);
53-
),
54-
```
55-
56-
**input-search.blade.php**
57-
58-
```html
59-
<input type="text" wire:model.debounce="columnSearch.{{ $field }}" placeholder="Search {{ ucfirst($field) }}" class="block w-full border-gray-300 rounded-md shadow-sm transition duration-150 ease-in-out sm:text-sm sm:leading-5 dark:bg-gray-700 dark:text-white dark:border-gray-600 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md" />
60-
```
61-
62-
### 3. Add the clause to the query based on the new state
63-
64-
Now you need to tell the query what to do when something is typed into the search:
65-
66-
```php
67-
public function query()
68-
{
69-
return User::query()
70-
->when(
71-
$this->columnSearch['name'] ?? null,
72-
fn ($query, $name) => $query->where('name', 'like', '%' . $name . '%')
73-
);
74-
}
75-
```
76-
77-
### Extra: Add a clear button to the inputs
78-
79-
If you want the input to have a clear button when it has a value like the default search does:
80-
81-
1. Send the state to the partial
82-
83-
```php
84-
Column::make('Name')
85-
->sortable()
86-
->searchable()
87-
->asHtml()
88-
->secondaryHeader(
89-
fn() => view('tables.cells.input-search', [
90-
'field' => 'name',
91-
'columnSearch' => $this->columnSearch
92-
])
93-
),
94-
```
95-
96-
2. Copy the search field and modify:
27+
// Example filter
28+
SelectFilter::make('Active')
29+
->hiddenFromAll(), // Optional, hides the filter from the menus, pills, count.
9730

98-
```html
99-
<div class="flex rounded-md shadow-sm">
100-
<input
101-
wire:model.debounce="columnSearch.{{ $field }}"
102-
placeholder="Search {{ ucfirst($field) }}"
103-
type="text"
104-
class="block w-full border-gray-300 rounded-md shadow-sm transition duration-150 ease-in-out sm:text-sm sm:leading-5 dark:bg-gray-700 dark:text-white dark:border-gray-600 @if (isset($columnSearch[$field]) && strlen($columnSearch[$field])) rounded-none rounded-l-md focus:ring-0 focus:border-gray-300 @else focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md @endif"
105-
/>
31+
// You can pass a filter directly
32+
Column::make('Active')
33+
->secondaryHeader($this->getFilterByKey('active')),
10634

107-
@if (isset($columnSearch[$field]) && strlen($columnSearch[$field]))
108-
<span wire:click="$set('columnSearch.{{ $field }}', null)" class="inline-flex items-center px-3 text-gray-500 bg-gray-50 rounded-r-md border border-l-0 border-gray-300 cursor-pointer sm:text-sm dark:bg-gray-700 dark:text-white dark:border-gray-600 dark:hover:bg-gray-600">
109-
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
110-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
111-
</svg>
112-
</span>
113-
@endif
114-
</div>
35+
// Or use the shorthand method
36+
Column::make('Active')
37+
->secondaryHeaderFilter('active'), // Takes the key from the filter, which you can find in the query string when the filter is applied.
11538
```

docs/filters/available-methods.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,51 @@ SelectFilter::make('Active')
195195

196196
Now instead of `Active: Yes` it will say `User Status: Active`
197197

198+
### hiddenFromMenus
199+
200+
Hide the filter from both the filter popover and the filter slide down.
201+
202+
```php
203+
SelectFilter::make('Active')
204+
->hiddenFromMenus()
205+
```
206+
207+
### hiddenFromPills
208+
209+
Hide the filter from the filter pills when applied.
210+
211+
```php
212+
SelectFilter::make('Active')
213+
->hiddenFromPills()
214+
```
215+
216+
### hiddenFromFilterCount
217+
218+
Hide the filter from the filter count when applied.
219+
220+
```php
221+
SelectFilter::make('Active')
222+
->hiddenFromFilterCount()
223+
```
224+
225+
### hiddenFromAll
226+
227+
Hide the filter from the menus, pills, and count.
228+
229+
```php
230+
SelectFilter::make('Active')
231+
->hiddenFromAll()
232+
```
233+
234+
### notResetByClearButton
235+
236+
By default the `clear` button will reset all filters to their defaults. You can prevent this on a specific filter by using this method.
237+
238+
```php
239+
SelectFilter::make('Active')
240+
->notResetByClearButton()
241+
```
242+
198243
### Config
199244

200245
If the filter takes any config options, you can set them with the `config` method:
@@ -205,4 +250,4 @@ If the filter takes any config options, you can set them with the `config` metho
205250
'min' => '2020-01-01',
206251
'max' => '2021-12-31',
207252
])
208-
```
253+
```

resources/views/components/tools/filter-pills.blade.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@if ($theme === 'tailwind')
88
<div>
9-
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedFiltersWithValues())
9+
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedVisibleFiltersForPills())
1010
<div class="mb-4 px-4 md:p-0">
1111
<small class="text-gray-700 dark:text-white">@lang('Applied Filters'):</small>
1212

@@ -16,6 +16,7 @@
1616
@endphp
1717

1818
@continue(is_null($filter))
19+
@continue($filter->isHiddenFromPills())
1920

2021
<span
2122
wire:key="{{ $component->getTableName() }}-filter-pill-{{ $filter->getKey() }}"
@@ -49,7 +50,7 @@ class="focus:outline-none active:outline-none"
4950
</div>
5051
@elseif ($theme === 'bootstrap-4')
5152
<div>
52-
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedFiltersWithValues())
53+
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedVisibleFiltersForPills())
5354
<div class="mb-3">
5455
<small>@lang('Applied Filters'):</small>
5556

@@ -59,6 +60,7 @@ class="focus:outline-none active:outline-none"
5960
@endphp
6061

6162
@continue(is_null($filter))
63+
@continue($filter->isHiddenFromPills())
6264

6365
<span
6466
wire:key="{{ $component->getTableName() }}-filter-pill-{{ $filter->getKey() }}"
@@ -91,7 +93,7 @@ class="badge badge-pill badge-light"
9193
</div>
9294
@elseif ($theme === 'bootstrap-5')
9395
<div>
94-
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedFiltersWithValues())
96+
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedVisibleFiltersForPills())
9597
<div class="mb-3">
9698
<small>@lang('Applied Filters'):</small>
9799

@@ -101,6 +103,7 @@ class="badge badge-pill badge-light"
101103
@endphp
102104

103105
@continue(is_null($filter))
106+
@continue($filter->isHiddenFromPills())
104107

105108
<span
106109
wire:key="{{ $component->getTableName() }}-filter-pill-{{ $filter->getKey() }}"

0 commit comments

Comments
 (0)