|
| 1 | +# Add SearchPane |
| 2 | + |
| 3 | +[SearchPanes](https://datatables.net/extensions/searchpanes/) ([example](https://datatables.net/extensions/searchpanes/examples/initialisation/simple.html)) |
| 4 | +allow the user to quickly filter the datatable after predefined filters. |
| 5 | + |
| 6 | +> {note} To use datatables you need to make sure that the npm packages `datatables.net-select-bs4` and `datatables.net-searchpanes-bs4` are installed and added to your `app.js`/`app.css` files. |
| 7 | +
|
| 8 | +## Adding SearchPanes to the frontend |
| 9 | + |
| 10 | +To be able to see SearchPanes you need to either add them fixed in the dom (displayed at all time) or add a button which |
| 11 | +opens them as popup. |
| 12 | + |
| 13 | +### Adding SearchPanes fixed in the dom |
| 14 | + |
| 15 | +SearchPanes can be added to a table via the dom string, in it, they are marked with a `P` if you for example |
| 16 | +are using `Bfrtip` as dom you can use `PBfrtip` to display the SearchPanes at the top of the datatable, or `BfrtipP` |
| 17 | +to display them at the very bottom. |
| 18 | + |
| 19 | +Setting the dom String with the `\Yajra\DataTables\Html\Builder`: |
| 20 | +```php |
| 21 | +public function html() : \Yajra\DataTables\Html\Builder |
| 22 | +{ |
| 23 | + // Setting the dom string directly |
| 24 | + return $this->builder() |
| 25 | + ->searchPanes(SearchPane::make()) |
| 26 | + ->dom('PBfrtip'); |
| 27 | + |
| 28 | + // Alternatively set the dom with parameters |
| 29 | + return $this->builder() |
| 30 | + ->searchPanes(SearchPane::make()) |
| 31 | + ->parameters([ |
| 32 | + 'dom' => 'PBfrtip' |
| 33 | + ]); |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Adding SearchPanes with a button |
| 38 | + |
| 39 | +To add a button which opens the SearchPanes you need to make one extending `searchPanes`: |
| 40 | + |
| 41 | +```php |
| 42 | +public function html() : \Yajra\DataTables\Html\Builder |
| 43 | +{ |
| 44 | + // Adding via class |
| 45 | + return $this->builder() |
| 46 | + ->searchPanes(SearchPane::make()) |
| 47 | + ->buttons([ |
| 48 | + \Yajra\DataTables\Html\Button::make('searchPanes') |
| 49 | + // other buttons... |
| 50 | + ]); |
| 51 | + |
| 52 | + // Alternatively set the buttons with options |
| 53 | + return $this->builder() |
| 54 | + ->searchPanes(SearchPane::make()) |
| 55 | + ->parameters([ |
| 56 | + 'buttons' => ['searchPanes', /*other buttons...*/] |
| 57 | + ]); |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## Adding SearchPanes to the backend |
| 62 | + |
| 63 | +The SearchPanes can be filled in the datatables declaration via the `searchPane()` method. The method takes the column |
| 64 | +for which the SearchPane is, as well as the options of the SearchPane. It also allows you to set custom processing for |
| 65 | +the options. |
| 66 | + |
| 67 | + |
| 68 | +```php |
| 69 | +public function dataTable($query) : Yajra\DataTables\DataTableAbstract |
| 70 | +{ |
| 71 | + return datatables() |
| 72 | + ->eloquent($query) |
| 73 | + // Adding the SearchPane |
| 74 | + ->searchPane( |
| 75 | + /* |
| 76 | + * This is the column for which this SearchPane definition is for |
| 77 | + */ |
| 78 | + 'user_id', |
| 79 | + |
| 80 | + /* |
| 81 | + * Here we define the options for our SearchPane. This should be either a collection or an array with the |
| 82 | + * form: |
| 83 | + * [ |
| 84 | + * [ |
| 85 | + * 'value' => 1, |
| 86 | + * 'label' => 'display value', |
| 87 | + * 'total' => 5, // optional |
| 88 | + * 'count' => 3 // optional |
| 89 | + * ], |
| 90 | + * [ |
| 91 | + * 'value' => 2, |
| 92 | + * 'label' => 'display value 2', |
| 93 | + * 'total' => 6, // optional |
| 94 | + * 'count' => 5 // optional |
| 95 | + * ], |
| 96 | + * ] |
| 97 | + */ |
| 98 | + fn() => User::query()->select('id as value', 'name as label')->get(), |
| 99 | + |
| 100 | + /* |
| 101 | + * This is the filter that gets executed when the user selects one or more values on the SearchPane. The |
| 102 | + * values are always given in an array even if just one is selected |
| 103 | + */ |
| 104 | + function (\Illuminate\Database\Eloquent\Builder $query, array $values) { |
| 105 | + return $query |
| 106 | + ->whereIn( |
| 107 | + 'id', |
| 108 | + $values); |
| 109 | + } |
| 110 | + ); |
| 111 | +} |
| 112 | +``` |
0 commit comments