Closed VincentLanglet closed 3 years ago
The issue seems to be related to the getFilterParameters
behavior:
if (false !== $this->persistFilters && $this->hasFilterPersister()) {
// if reset filters is asked, remove from storage
if ('reset' === $this->getRequest()->query->get('filters')) {
$this->getFilterPersister()->reset($this->getCode());
}
// if no filters, fetch from storage
// otherwise save to storage
if (empty($filters)) {
$filters = $this->getFilterPersister()->get($this->getCode());
} else {
$this->getFilterPersister()->set($this->getCode(), $filters);
}
}
If all the value submitted are removed, the $filters are coming from the FilterPersisterInterface
, so the old value are used.
If at least one (non-default) value is set, the filter persister is correctly updated.
One solution would be to
either to trigger the reset if we submit only default values, i.e. when all the value are removed here:
jQuery('.sonata-filter-form', subject).on('submit', function () {
var $form = jQuery(this);
$form.find('[sonata-filter="true"]:hidden :input').val('');
if (!this.dataset.defaultValues) {
return;
}
var defaults = Admin.convert_query_string_to_object(
$.param({'filter': JSON.parse(this.dataset.defaultValues)})
);
// Keep only changed values
$form.find('[name*=filter]').each(function (i, field) {
if (JSON.stringify(defaults[field.name] || '') === JSON.stringify($(field).val())) {
field.removeAttribute('name');
}
});
});
I don't use javascript/jquery a lot.
Is it possible to add the query param filters=reset
automatically if we've filtered all the value of the form ?
I found one use-case not fixed by https://github.com/sonata-project/SonataAdminBundle/pull/6871
Let's say you have two filter A and B. Let's say the default situation is "The list is filtered by filter A with the value "TRUE"".
If I change the filter A to the value "FALSE", and submit, it works fine. But then, if I change to the value "TRUE", and submit, the "FALSE" is still used. If I change again to the value "TRUE", and I also set a value for the filter B, and submit, now the "TRUE" value is used.
I think that if all the values submitted are equal to the default value, the filter is not (or not correctly) submitted so no value are changed. Maybe we should be sure to always keep one field submitted (?).
cc @kirya-dev and @willemverspyck who works on this recently.