Skip to content

Commit

Permalink
Added filters to the package (#14)
Browse files Browse the repository at this point in the history
* Added filters to the package

* Updated the docs
  • Loading branch information
wimurk authored Sep 2, 2019
1 parent 16779f2 commit 3e435cb
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 7 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
89 changes: 89 additions & 0 deletions docs/filters.md
Original file line number Diff line number Diff line change
@@ -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
];
}),
```
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
6 changes: 3 additions & 3 deletions src/Controllers/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/Fields/Multiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -138,4 +137,4 @@ public function implode(string $separate = ", ")
return $this;
}

}
}

0 comments on commit 3e435cb

Please sign in to comment.