Filters
Datatables can have filters. The enable the filter widget - define $filterconfig
. For example, this can be done using a helper class (good for getting dynamic lists such as a list pages)
Datatables can have filters. The enable the filter widget - define $filterconfig
. For example, this can be done using a helper class (good for getting dynamic lists such as a list pages)
class PageComponentsTable extends Table {
public function mount()
{
$this->filterconfig = (new PageComponentFilterBuilder())->collect();
parent::mount();
}
...
The easiest way to create a filter is to create a class that extends the FilterBuilder
class. For example:
<?php
namespace Medialight\Textstem\Domain\Pages\Config;
use Medialight\Textstem\Contracts\CollectsFilterConfig;
use Medialight\Textstem\Filters\FilterBuilder;
class PageFilterBuilder extends FilterBuilder implements CollectsFilterConfig
{
public function collect()
{
$filters = [
'title' => [
'field' => 'title',
'input' => 'text',
'operations' => ['contains', 'starts with', 'is', 'is not'],
],
'tag' => [
'field' => 'tag',
'input' => 'text',
'model' => 'App\Models\GigListing',
],
'access' => [
'field' => 'access',
'input' => 'select',
'options' => ['public', 'private', 'protected'],
],
'template' => [
'field' => 'template',
'input' => 'select',
'options' => collect($this->listPageTemplates())->pluck('name')->toArray(),
],
'created_at' => [
'field' => 'created_at',
'input' => 'date',
'operations' => ['before', 'after'],
],
'updated_at' => [
'field' => 'updated_at',
'input' => 'date',
'operations' => ['before', 'after'],
],
];
return $this->prepare($filters);
}
}
For each filter, the following can be defined:
field
- the field name to filter on (corresponds to a model attribute)input
- the type of input to use (text, select, date)operations
- the operations to allow (contains, starts with, is, is not, before, after, in, approximately, between,where)model
- the model to be searched (if not defined, the model will be guessed from the field name)options
- array of values to use for the select inputkoptions
- array of value=>labels to use for the select input (where the key is the option value, and the value is the option label)label
- the label to use for the filter (if not defined, the label will be guessed from the field name)casts
- the casts to use on the search value when filtering (eg 'filesize' will convert the value such as "2MB" to a number of bytes)Possible Operations are
The following operations are implemented:
To use a where
filter, you must use a select with options that follow the format "operation fieldname". For example, to create a filter that compares the value of the price field to the value of the sale_price field:
'sale_price' => [
'field' => 'sale_price',
'input' => 'select',
'operations' => ['where'],
'options' => [
'same as price',
'different to price',
'less than price',
'more than price'
],
],