I was wondering if there is currently a way to set a default for a filter, but I cannot find any information on this. I am currently also using Ajax.
The easiest way to do this is to make the link in the sidebar directly to the url with the query strings of your default. so instead of having a sidebar item that links to /admin/members you can just make it /admin/members?filter=active-1 for example.
You could also do some checks in your controller, look for a filter in the request URI, if there isnt one, then redirect to the same page with your default filter in the url.
You could also try again checking if theres an active filter, and if there is not then add a where/clause or your constraints, which will give you the same result as your filter.
As this is not a bug, I will close this issue to keep the place easier to identify issues rather than "how to" questions. People are still free to contribute to the thread :)
Add to the your controller:
if(!request('active')){
$this->crud->addClause('where', 'active', '=', 1);
}
Hope it will help you!
Thanks for sharing your solution @soliduskodas ! I'm sure it'll help someone who stumbles upon this!
Hey @soliduskodas using that method you have no way of getting back to an unfiltered list.
I just hacked together a method that works for dropdowns, a little more will need to be done to get it working on all filters though.
PanelTraits > Filters.php
...
class CrudFilter
{
public $name;
public $type = 'select';
public $label;
public $default;
public $placeholder; // <== ADD THIS
public $values;
public $options;
public $currentValue;
public $view;
public function __construct($options, $values, $filter_logic)
{
$this->checkOptionsIntegrity($options);
$this->name = $options['name'];
$this->type = $options['type'];
$this->label = $options['label'];
$this->default = isset($options['default']) ? $options['default'] : null; // <== ADD THIS
...
dropdown.blade.php
// Add this to the script
@if(property_exists($filter, 'default') && $filter->default != null)
$("li.dropdown[filter-name={{ $filter->name }}] .dropdown-menu li a[key={{$filter->default}}]").click();
@endif
Then in your crud controller simply add a 'default' value to the filter.
For anyone finding this issue:
There is another closure in the addFilter method that will be used when the filter is inactive!
Most helpful comment
For anyone finding this issue:
There is another closure in the
addFiltermethod that will be used when the filter is inactive!