$this->crud->addFilter([
'name' => 'type',
'type' => 'dropdown',
'label'=> 'Entry Type'
], [
'income' => 'Income',
'expense' => 'Expense',
], function($value) {
if($value == 'expense') {
$this->crud->setModel('AbbyJanke\Expensed\App\Models\Expense');
} else {
$this->crud->setModel('AbbyJanke\Expensed\App\Models\Income');
}
});
When hitting "clear filters" I assumed it would go back to the default model, or the else option.
Filters were cleared and a DataTable with No matching records found was displayed.
BUT if I hit reset it does clear it. I default to the 'Income' Model, I can change back and forth between the types without issues its only when hitting "Clear Filters"
Hitting reset works, thought maybe was persistent tables but that didn't work.
PHP 7.4.3 (cli) (built: Feb 20 2020 12:23:10) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
v7.9.2@757b155658ae6da429065ba8f22242fe599824f7
Hm... It does sound like this should work that way, yes... I'll take a look, thanks.
In the meantime, if you add yet another function as a parameter (aka fallbackLogic), you'll be able to define what happens when the filter is NOT applied - that's what you want, right? So
$this->crud->addFilter([
'name' => 'type',
'type' => 'dropdown',
'label'=> 'Entry Type'
], [
'income' => 'Income',
'expense' => 'Expense',
], function($value) {
$this->crud->setModel('AbbyJanke\Expensed\App\Models\Expense');
},
function($value) {
$this->crud->setModel('AbbyJanke\Expensed\App\Models\Income');
});
This is easier to understand with the fluent syntax in 4.1:
// else alias, left to be applied by the operation itself
CRUD::filter('active')
->type('simple')
->label('Simple')
->ifActive(function ($value) {
$this->crud->addClause('where', 'active', '1');
})->else(function ($value) {
$this->crud->addClause('where', 'active', '0');
});
@tabacitu even the fallback seems to fail :/ And even after "clear filters" and i change the filter to be something new it seems to have the same issue, even with the fallback. I'm wondering if its something to do with persistent tables. Even refreshing the page (cmd + r) or the refresh in chrome it doesn't wanna be correct. Trying to debug it as I go so keep learning new things and have more feelings. Even if I click on the link in the sidebar it has same issue, go to URL bar and hit enter same thing. Only thing that seems to fix it is hitting "reset"
Hum ... Hey @AbbyJanke
Let's try to break this up. You have two options in a dropdrown.
When you open the crud list page, I assume one of those is already selected (crud has model set).
You change between them setting crud model and returning the results.
When you it "Clear Filters" what is done is that we setup the empty url (without filters) and redraw the table: - from filters_navbar.blade.php
var new_url = '{{ url($crud->route.'/search') }}';
var ajax_table = $("#crudTable").DataTable();
// replace the datatables ajax url with new_url and reload it
ajax_table.ajax.url(new_url).load();
I tried to reproduce your issue but I couldn't.
But while testing I noticed something weird is happening.
After hitting "Clear Filters", some like 4 or 5 requests are made at same time to the search endpoint.
https://recordit.co/yPzF5R392C
I think there is a bug here, we are queryin' the endpoint more times than it should. I think is some event we fire that gets caught in some other place, like in filters.
Is this happening to you too ?
@pxpm Similar but I only seem to have 2 requests, Looking at the data that gets returned in the first one it retrieves all the information, but the second one is causing the issue, looks like it is trying to apply another filter.
Doubtful it will help but i have my code on github (https://github.com/AbbyJanke/expensed) since its an opensource package for backpack i'm working on.
Thoughts:
The second load for mine is a 'select2_ajax' filter. Wondering if its certain types of filters causing additional. Downloading the demo to test that theory.
Yes it is :)
From selec2_ajax filter:
on('change', function (evt) {
var val = $(this).val();
var val_text = $(this).select2('data')[0]?$(this).select2('data')[0].text:null;
var parameter = filterName;
// behaviour for ajax table
var ajax_table = $('#crudTable').DataTable();
var current_url = ajax_table.ajax.url();
var new_url = addOrUpdateUriParameter(current_url, parameter, val);
if (val_text) {
new_url = addOrUpdateUriParameter(new_url, parameter + '_text', val_text);
}
new_url = normalizeAmpersand(new_url.toString());
I need to have a look at this. I think it's this on.change event that is present in other filters (that demo uses but you don't) that's why i got 5 requests and you 2.
I think we must check if (val_text != null || val_text != '' || val_text != typeof undefined) and that will fix this filter.
If you happen to fix this please let me know, i think only by tomorrow i am going to have a look at this.
@pxpm Yupp checking if null works.
}).on('change', function (evt) {
var val = $(this).val();
var val_text = $(this).select2('data')[0]?$(this).select2('data')[0].text:null;
if (val_text != null || val_text != '' || val_text != typeof undefined)
var parameter = filterName;
// behaviour for ajax table
var ajax_table = $('#crudTable').DataTable();
var current_url = ajax_table.ajax.url();
var new_url = addOrUpdateUriParameter(current_url, parameter, val);
if (val_text) {
new_url = addOrUpdateUriParameter(new_url, parameter + '_text', val_text);
}
new_url = normalizeAmpersand(new_url.toString());
// replace the datatables ajax url with new_url and reload it
ajax_table.ajax.url(new_url).load();
// add filter to URL
crud.updateUrl(new_url);
// mark this filter as active in the navbar-filters
if (URI(new_url).hasQuery(filterName, true)) {
$('li[filter-name='+filterName+']').removeClass('active').addClass('active');
}
else
{
$("li[filter-name="+filterName+"]").removeClass("active");
$("li[filter-name="+filterName+"]").find('.dropdown-menu').removeClass("show");
}
}
});
Could only wrap the crud.updateUrl(new_url) instead of all of it but i think wrapping all of it causes less process so faster load time.
It's going to be select2_ajax, select2, and select2_multiple that needs to be adjusted. (just jotting down for my notes to submit a PR)
@pxpm I got it working on select2_ajax. Need to replace || with OR though.
https://gist.github.com/AbbyJanke/bfba4d033dd235b10df3e4c1e8f622ab
Trying to add it to select2 it causes the styling to be messes up the styling of the dropdown. Ajax isn't my specialty.
Had the same problem with multiple requests and an empty table after filter reset. Can confirm checking the value solves it. I have it pretty simple:
select2
```javascript
$(this).change(function() {
var value = $(this).val();
if (!value) {
return;
}
````
select2_ajax
```javascript
}).on('change', function (evt) {
var val = $(this).val();
var val_text = $(this).select2('data')[0]?$(this).select2('data')[0].text:null;
if (!val_text) {
return;
}
````