You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
You start by [creating a new page](/docs/{{version}}/base-about#custom-pages-1) to hold your custom content, eg: a reports page.
633
+
563
634
```bash
564
635
php artisan backpack:page Reports
565
636
```
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)
567
637
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**:
569
639
```diff
570
640
@extends(backpack_view('blank'))
571
641
@@ -589,20 +659,24 @@ After the page is created, you should edit the `resources/views/admin/reports.bl
589
659
+@push('after_scripts')
590
660
+<script>
591
661
+ 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);
593
667
+ });
594
668
+
595
669
+ document.addEventListener('backpack:filter:changed', function (event) {
596
670
+ let filterName = event.detail.filterName;
597
671
+ let filterValue = event.detail.filterValue;
598
672
+ let shouldUpdateUrl = event.detail.shouldUpdateUrl;
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`):
606
680
607
681
```php
608
682
class ReportsController extends Controller
@@ -635,77 +709,11 @@ class ReportsController extends Controller
635
709
}
636
710
```
637
711
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
-
<aname="examples"></a>
645
-
## Examples
646
-
647
-
Use a dropdown to filter by the values of a MySQL ENUM column:
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;
709
717
710
718
<aname="debouncing-filters"></a>
711
719
### Add a debounce time to filters
@@ -725,7 +733,7 @@ CRUD::filter('name')
725
733
All filter types accept a `debounce`, like for example the simple filter, range filter etc.
726
734
727
735
<aname="adding-a-filter-using-array-syntax"></a>
728
-
### Adding a filter using array syntax
736
+
### Add a filter using array syntax
729
737
730
738
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.
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).
0 commit comments