From 3e435cbe0832335b2ab31cc2a891667aa93b8f57 Mon Sep 17 00:00:00 2001 From: Wimurk Date: Mon, 2 Sep 2019 11:34:28 +0200 Subject: [PATCH] Added filters to the package (#14) * Added filters to the package * Updated the docs --- README.md | 3 +- docs/filters.md | 89 ++++++++++++++++++++++++++++++++++++++ docs/index.md | 1 + src/Controllers/Filter.php | 6 +-- src/Fields/Multiple.php | 3 +- 5 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 docs/filters.md diff --git a/README.md b/README.md index 45292e6..35b25f4 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,7 @@ composer require singlequote/laravel-datatables ``` ## Whats new -Yes, we live in a time where everything is automated. So in this version we introduce some very cool features. -In this version the html and the script is created for you. Also this version is based on how laravel nova works. +In this version we introduce a cool new feature. In this version you can use filters to add directly to your tableModel. Check it out [here](https://singlequote.github.io/Laravel-datatables/filters) ## Let's start We wanted our code as clean as possible and use the same code more than once. diff --git a/docs/filters.md b/docs/filters.md new file mode 100644 index 0000000..c5604b2 --- /dev/null +++ b/docs/filters.md @@ -0,0 +1,89 @@ +# Filters +Filters can be used to filter your data. The filters can be used directly in your tableModel and filters the data server side. + +<= [Go back](https://singlequote.github.io/Laravel-datatables/) +------------------------------------------------------------------- + +## Basic example +In this example we will use a drop-down to filter the users. Let's create a filter to filter deleted users. +In your `tableModel` create a function named `filter` +```php + use SingleQuote\DataTables\Filter\Dropdown; + + /** + * Create filters + * + * @return array + */ + public function filter() : array + { + $filter = [ + [ + 'name' => "Without deleted", + "id" => 1 + ],[ + 'name' => "With deleted", + "id" => 2 + ] + ]; + + return [ + //the data attribute accepts an array as an object|collection + Dropdown::make('filter')->label("Filter deleted")->data($filter), + ]; + } +``` +The above will create a dropdown filter above your datatable. +Now let's use the filter. Create a function named `query` in your `tableModel` +You can call the method `$this->getFilter(...)` to retrieve your filters data. +```php + public function query($query) + { + if ($this->getFilter('filter') && (int) $this->getFilter('filter') === 2) { + return $query->withTrashed(); //return with deleted users + } + + return $query; + } +``` + +## Data attribute +The data attribute is used to show the filters. As in the example you can pass an array or object to create your filter. By default the labels `name` and `id` are used to create the filter. See the options below. + +#### array | object +Pass an array or object to create a filter. +```php +$filter = [ + [ + 'name' => "Without deleted", + "id" => 1 + ],[ + 'name' => "With deleted", + "id" => 2 + ] +]; + +Dropdown::make('filter')->label("Filter deleted")->data($filter), +``` + +### Model | collections +By default you the method uses the `id` as value and the `name` as label. +```php +$data = Status::all(); // {id : 1, name : 'invited', etc...} + +Dropdown::make('filter')->label("Filter by status")->data($data), +``` + +### Closure +You can edit the `label` or `value` with an closure. + +```php +$data = Status::all(); // {id : 1, name : 'invited', etc...} + +Dropdown::make('filter')->label("Filter by status")->data($data, function($status){ + return [ + 'label' => $status->name, + 'value' => $status->id + ]; +}), +``` \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index f9cd726..559064f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,6 +1,7 @@ # Docs * [Table models](https://singlequote.github.io/Laravel-datatables/table-models) * [Fields](https://singlequote.github.io/Laravel-datatables/fields) +* [Filters](https://singlequote.github.io/Laravel-datatables/filters) * [Translations](https://singlequote.github.io/Laravel-datatables/translations) * [Middleware, permissions and roles](https://singlequote.github.io/Laravel-datatables/middleware) diff --git a/src/Controllers/Filter.php b/src/Controllers/Filter.php index 947f486..e9d8196 100644 --- a/src/Controllers/Filter.php +++ b/src/Controllers/Filter.php @@ -171,11 +171,11 @@ public function getMultiple() : bool */ public function data($data, \Closure $closure = null) { - foreach($data as $key => $item){ + foreach($data as $item){ if(!$closure){ $this->data[] = [ - 'value' => $item->id, - 'label' => $item->name + 'value' => is_array($item) ? $item['id'] : $item->id, + 'label' => is_array($item) ? $item['name'] : $item->name ]; }else{ $this->data[] = $closure($item); diff --git a/src/Fields/Multiple.php b/src/Fields/Multiple.php index b87cd64..ce949fa 100644 --- a/src/Fields/Multiple.php +++ b/src/Fields/Multiple.php @@ -67,7 +67,6 @@ public static function make(string $column) */ public function each(string $column, \Closure $closure) { - $this->emptyCheck = false; foreach($closure() as $field){ $this->eachFields[] = [ "rendered" => $this->getBetweenTags($field->build(), 'script'), @@ -138,4 +137,4 @@ public function implode(string $separate = ", ") return $this; } -} +} \ No newline at end of file