Skip to content

Commit a2f89d1

Browse files
authored
Merge pull request #624 from Laravel-Backpack/add-docs-for-agnostic-filters
docs for agnostic filters
2 parents b82a9df + 3a69a87 commit a2f89d1

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

7.x-dev/crud-filters.md

+95-1
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,100 @@ CRUD::filter('trashed')
621621
<a name="tips-and-tricks"></a>
622622
## Tips and Tricks
623623

624+
<a name="using-filters-on-admin-page"></a>
625+
### Use Filters on custom admin panel pages
626+
627+
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.
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+
634+
```bash
635+
php artisan backpack:page Reports
636+
```
637+
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**:
639+
```diff
640+
@extends(backpack_view('blank'))
641+
642+
@section('content')
643+
<section class="header-operation container-fluid animated fadeIn d-flex mb-2 align-items-baseline d-print-none" bp-section="page-header">
644+
<h1 class="text-capitalize mb-0" bp-section="page-heading">Reports</h1>
645+
<p class="ms-2 ml-2 mb-0" bp-section="page-subheading">Page for Reports</p>
646+
</section>
647+
<section class="content container-fluid animated fadeIn" bp-section="content">
648+
<div class="row">
649+
<div class="col-md-12">
650+
<div class="card">
651+
<div class="card-body">
652+
+ @include('crud::inc.filters_navbar')
653+
</div>
654+
</div>
655+
</div>
656+
</div>
657+
@endsection
658+
659+
+@push('after_scripts')
660+
+<script>
661+
+ document.addEventListener('backpack:filters:cleared', function (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);
667+
+ });
668+
+
669+
+ document.addEventListener('backpack:filter:changed', function (event) {
670+
+ let filterName = event.detail.filterName;
671+
+ let filterValue = event.detail.filterValue;
672+
+ let shouldUpdateUrl = event.detail.shouldUpdateUrl;
673+
+ console.log('one filter changed', filterName, filterValue, shouldUpdateUrl);
674+
+ });
675+
+</script>
676+
+@endpush
677+
```
678+
679+
After that, time to add your own filters in your controller (in this example, `ReportsController.php`):
680+
681+
```php
682+
class ReportsController extends Controller
683+
{
684+
public function index()
685+
{
686+
$crud = app('crud');
687+
688+
$crud->addFilter([
689+
'type' => 'simple',
690+
'name' => 'checkbox',
691+
'label' => 'Simple',
692+
], false);
693+
694+
$crud->addFilter([ // dropdown filter
695+
'name' => 'select_from_array',
696+
'type' => 'dropdown',
697+
'label'=> 'Dropdown',
698+
], ['one' => 'One', 'two' => 'Two', 'three' => 'Three']);
699+
700+
return view('admin.reports', [
701+
'title' => 'Reports',
702+
'breadcrumbs' => [
703+
trans('backpack::crud.admin') => backpack_url('dashboard'),
704+
'Reports' => false,
705+
],
706+
'crud' => $crud,
707+
]);
708+
}
709+
}
710+
```
711+
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;
717+
624718
<a name="debouncing-filters"></a>
625719
### Add a debounce time to filters
626720

@@ -639,7 +733,7 @@ CRUD::filter('name')
639733
All filter types accept a `debounce`, like for example the simple filter, range filter etc.
640734

641735
<a name="adding-a-filter-using-array-syntax"></a>
642-
### Adding a filter using array syntax
736+
### Add a filter using array syntax
643737

644738
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.
645739

7.x-dev/release-notes.md

+4-2
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)