Filters
Add filters on a crud in the configure function. There are two ways to do this:
1. Extending Crud Filter classes (the preferred way)
Enso comes with some base CrudFilters to get things started. These will be added to over time.
Matching items directly
The following will add a text-input filter:
- Where it's wrapper element will have the css class
is-halfadded to it - That matches the any records where
titleorexcerptcontains the value entered into the filter - Where the initial value is
null - Which has a label of
Search - Has placeholder and help text content
- Has some field settings.
->filters([
'search' => TextFilter::make()
->class('is-half')
->columns(['title', 'excerpt'])
->default(null)
->label('Search')
->props([
'placeholder' => 'Search...',
'help-text' => 'Search for records containing the given text',
])
->settings([...]),
]);
Matching items via related items
The following will add a text-input filter that matches any record that has a user where the preferred_name or email contain the value entered into the filter.
->filters([
'user' => TextFilter::make()
->label('User')
->columns(['preferred_name', 'email'])
->relationshipName('user'),
]);
Best Practice
Ideally you should extend a crud filter and make your own by defining default properties directly onto the class. This will also ensure that you maintain clean and minimal Configs, and that any single type of search can be applied to multiple different Cruds and still happen uniform manner. For an example of a TextFilter see Yadda\Enso\Crud\Filters\UserFilter, and for a SelectFilter see Yadda\Enso\Crud\Filters\RoleSelectFilter.
When applied as UserFilter::make(), it will search the current Crud for records where any of it's columns contain the input value. When applied as UserFilter::make()->relationshipName('author'), it will search all records on the current Crud where they have a relationship called author where any of their columns of related items contain the input value.
2. An array of options and properties (the old way)
->filters([
'form' => [
'type' => 'text'
'label' => 'Form',
'default' => null,
'props' => [
'settings' => [
'ajax_url' => route('admin.users.search'),
...
],
],
'classes' => 'is-4',
'callable' => {callable},
],
])
The index for each filter item is the key you want to send data back as for filtering in the backend.
type: The type of filter input.'text','select','checkbox'or'date'.label: The label that the filter should use. Setting this to an empty string means no labeldefault: The initial value that this filter should holdprops: Any Props that you might normally apply have applied by a SelectFieldclasses: A string of classes to add to the filterItem. Default isis-4callable: Special case filtering.This is used in the controller to determine how the value should be used instead of the default action for the given type. Current types and their defaults:
- text:
$name LIKE %{$value}% - select:
$name = $value - checkbox:
$name = $value- This assumes truthy value. Will currently not work out-the-box with checkbox groups. - date:
$name >= $value- Initial purpose: 'Created Since'
- text:
NOTE: Internally, the select type uses an EnsoSelectField, so pass everything as you normally would in a props value. As this does not pass through SelectField.php, the standard helpers wont work. For example, you'll have to translate useAjax(route(), Item::query) into is base value: settings['ajax_url'] (the query part is reduntant at his point, it's used to populating the form from an item).