Skip to content

Commit 3a69a87

Browse files
committed
polish custom filters docs
1 parent 5b17718 commit 3a69a87

File tree

2 files changed

+91
-81
lines changed

2 files changed

+91
-81
lines changed

7.x-dev/crud-filters.md

Lines changed: 87 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -555,17 +555,87 @@ Inside this file, you'll have:
555555

556556
<hr>
557557

558+
<a name="examples"></a>
559+
## Examples
560+
561+
Use a dropdown to filter by the values of a MySQL ENUM column:
562+
563+
```php
564+
CRUD::filter('published')
565+
->type('select2')
566+
->values(function() {
567+
return \App\Models\Test::getEnumValuesAsAssociativeArray('published');
568+
})
569+
->whenActive(function($value) {
570+
CRUD::addClause('where', 'published', $value);
571+
});
572+
```
573+
574+
Use a select2 to filter by a 1-n relationship:
575+
```php
576+
CRUD::filter('category_id')
577+
->type('select2')
578+
->values(function() {
579+
return \App\Models\Category::all()->pluck('name', 'id')->toArray();
580+
})
581+
->whenActive(function($value) {
582+
CRUD::addClause('where', 'category_id', $value);
583+
});
584+
```
585+
586+
Use a select2_multiple to filter Products by an n-n relationship:
587+
```php
588+
CRUD::filter('tags')
589+
->type('select2_multiple')
590+
->values(function() { // the options that show up in the select2
591+
return Product::all()->pluck('name', 'id')->toArray();
592+
})
593+
->whenActive(function($values) {
594+
foreach (json_decode($values) as $key => $value) {
595+
$this->crud->query = $this->crud->query->whereHas('tags', function ($query) use ($value) {
596+
$query->where('tag_id', $value);
597+
});
598+
}
599+
});
600+
```
601+
602+
Use a simple filter to add a scope if the filter is active:
603+
```php
604+
// add a "simple" filter called Published
605+
CRUD::filter('published')
606+
->type('simple')
607+
->whenActive(function() { // if the filter is active (the GET parameter "published" exits)
608+
CRUD::addClause('published');
609+
});
610+
```
611+
612+
Use a simple filter to show the softDeleted items (trashed):
613+
```php
614+
CRUD::filter('trashed')
615+
->type('simple')
616+
->whenActive(function($values) {
617+
$this->crud->query = $this->crud->query->onlyTrashed();
618+
});
619+
```
620+
621+
<a name="tips-and-tricks"></a>
622+
## Tips and Tricks
623+
558624
<a name="using-filters-on-admin-page"></a>
559-
## How to use Filters on any admin panel page
625+
### Use Filters on custom admin panel pages
560626

561627
Filters can be added to any admin panel page, not just the main CRUD table. Imagine that you want to have a dashboard page, with a few widgets that show some data. You can add filters to that page, and use them to filter the data shown in the widgets.
562-
You start by creating a new page, eg: a reports page.
628+
629+
630+
![](https://backpackforlaravel.com/uploads/docs/filters/filters-in-custom-page.png)
631+
632+
You start by [creating a new page](/docs/{{version}}/base-about#custom-pages-1) to hold your custom content, eg: a reports page.
633+
563634
```bash
564635
php artisan backpack:page Reports
565636
```
566-
**NOTE:** You can read more about creating your custom pages in the [creating a custom page docs.](/docs/{{version}}/base-about#custom-pages-1)
567637

568-
After the page is created, you should edit the `resources/views/admin/reports.blade.php` file and add the filters navbar and the event listeners:
638+
To use filters on a custom admin panel page, you should edit the blade file (in this example the `resources/views/admin/reports.blade.php` file) to **add the filters navbar** and **the event listeners**:
569639
```diff
570640
@extends(backpack_view('blank'))
571641

@@ -589,20 +659,24 @@ After the page is created, you should edit the `resources/views/admin/reports.bl
589659
+@push('after_scripts')
590660
+<script>
591661
+ document.addEventListener('backpack:filters:cleared', function (event) {
592-
+ console.log('filters cleared', event);
662+
+ console.log('all filters cleared', event);
663+
+ });
664+
+
665+
+ document.addEventListener('backpack:filter:cleared', function (event) {
666+
+ console.log('one filter cleared', event);
593667
+ });
594668
+
595669
+ document.addEventListener('backpack:filter:changed', function (event) {
596670
+ let filterName = event.detail.filterName;
597671
+ let filterValue = event.detail.filterValue;
598672
+ let shouldUpdateUrl = event.detail.shouldUpdateUrl;
599-
+ console.log('filter changed', filterName, filterValue, shouldUpdateUrl);
673+
+ console.log('one filter changed', filterName, filterValue, shouldUpdateUrl);
600674
+ });
601675
+</script>
602676
+@endpush
603677
```
604678

605-
After that, time to add your own filters in the `ReportsController.php`
679+
After that, time to add your own filters in your controller (in this example, `ReportsController.php`):
606680

607681
```php
608682
class ReportsController extends Controller
@@ -635,77 +709,11 @@ class ReportsController extends Controller
635709
}
636710
```
637711

638-
That's it, you should now have the filters navbar on your reports page. You can use the event listeners to update the data shown on the page based on the filters selected by the user.
639-
640-
<-- INSERT IMAGE -->
641-
642-
<hr>
643-
644-
<a name="examples"></a>
645-
## Examples
646-
647-
Use a dropdown to filter by the values of a MySQL ENUM column:
648-
649-
```php
650-
CRUD::filter('published')
651-
->type('select2')
652-
->values(function() {
653-
return \App\Models\Test::getEnumValuesAsAssociativeArray('published');
654-
})
655-
->whenActive(function($value) {
656-
CRUD::addClause('where', 'published', $value);
657-
});
658-
```
659-
660-
Use a select2 to filter by a 1-n relationship:
661-
```php
662-
CRUD::filter('category_id')
663-
->type('select2')
664-
->values(function() {
665-
return \App\Models\Category::all()->pluck('name', 'id')->toArray();
666-
})
667-
->whenActive(function($value) {
668-
CRUD::addClause('where', 'category_id', $value);
669-
});
670-
```
671-
672-
Use a select2_multiple to filter Products by an n-n relationship:
673-
```php
674-
CRUD::filter('tags')
675-
->type('select2_multiple')
676-
->values(function() { // the options that show up in the select2
677-
return Product::all()->pluck('name', 'id')->toArray();
678-
})
679-
->whenActive(function($values) {
680-
foreach (json_decode($values) as $key => $value) {
681-
$this->crud->query = $this->crud->query->whereHas('tags', function ($query) use ($value) {
682-
$query->where('tag_id', $value);
683-
});
684-
}
685-
});
686-
```
687-
688-
Use a simple filter to add a scope if the filter is active:
689-
```php
690-
// add a "simple" filter called Published
691-
CRUD::filter('published')
692-
->type('simple')
693-
->whenActive(function() { // if the filter is active (the GET parameter "published" exits)
694-
CRUD::addClause('published');
695-
});
696-
```
697-
698-
Use a simple filter to show the softDeleted items (trashed):
699-
```php
700-
CRUD::filter('trashed')
701-
->type('simple')
702-
->whenActive(function($values) {
703-
$this->crud->query = $this->crud->query->onlyTrashed();
704-
});
705-
```
706-
707-
<a name="tips-and-tricks"></a>
708-
## Tips and Tricks
712+
That's it, you should now have the filters navbar on your reports page. You can use the event listeners to update the data shown on the page based on the filters selected by the user.
713+
Here are the Javascript events you can listen to:
714+
- `backpack:filter:changed` when a filter is changed;
715+
- `backpack:filter:cleared` when a filter is cleared;
716+
- `backpack:filters:cleared` when all filters are cleared;
709717

710718
<a name="debouncing-filters"></a>
711719
### Add a debounce time to filters
@@ -725,7 +733,7 @@ CRUD::filter('name')
725733
All filter types accept a `debounce`, like for example the simple filter, range filter etc.
726734

727735
<a name="adding-a-filter-using-array-syntax"></a>
728-
### Adding a filter using array syntax
736+
### Add a filter using array syntax
729737

730738
In Backpack v4-v5 we used an "array syntax" to add and manipulate filters. That syntax is still supported for backwards-compatiblity. But it most cases it's easier to use the fluent syntax.
731739

7.x-dev/release-notes.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ Previously when working with Operations, developers found themselves needing to
3030

3131
### Re-Usable Filters
3232

33-
// just like your air purifier
34-
// TODO
33+
34+
![](https://backpackforlaravel.com/uploads/docs/filters/filters-in-custom-page.png)
35+
36+
Starting with this Backpack version, you can use the [filters](/docs/{{version}}/crud-filters) in custom pages too. Instead of being tied to DataTables, filters now trigger generic Javascript events like `backpack:filter:changed`. You can catch those events using custom code in Javascript or Livewire... and do stuff. This it possible to use filters on completely custom pages - like custom dashboards, custom reports or custom operations. [Read more](/docs/{{version}}/crud-filters#use-filters-on-custom-admin-panel-pages).
3537

3638
### Filters inside CustomViews
3739

0 commit comments

Comments
 (0)