Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unchanged filters params #6700

Merged
merged 6 commits into from
Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,14 +658,19 @@ public function preBatchAction($actionName, ProxyQueryInterface $query, array &$
{
}

public function getFilterParameters()
final public function getDefaultFilterParameters(): array
{
$parameters = array_merge(
return array_merge(
$this->getModelManager()->getDefaultSortValues($this->getClass()), // NEXT_MAJOR: Remove this line.
$this->datagridValues, // NEXT_MAJOR: Remove this line.
$this->getDefaultSortValues(),
$this->getDefaultFilterValues()
);
}

public function getFilterParameters()
{
$parameters = $this->getDefaultFilterParameters();

// build the values array
if ($this->hasRequest()) {
Expand Down
10 changes: 10 additions & 0 deletions src/Admin/AdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*
* @method array configureActionButtons(string $action, ?object $object = null)
* @method string getSearchResultLink(object $object)
* @method array getDefaultFilterParameters()
* @method bool isCurrentRoute(string $name, ?string $adminCode)
* @method bool canAccessObject(string $action, object $object)
* @method mixed getPersistentParameter(string $name)
Expand Down Expand Up @@ -396,6 +397,15 @@ public function configure();
*/
public function preBatchAction($actionName, ProxyQueryInterface $query, array &$idx, $allElements);

/**
* Return array of default filter parameters.
*
* NEXT_MAJOR: uncomment this method
*
* @return array<string, mixed>
*/
// public function getDefaultFilterParameters(): array;

/**
* Return array of filter parameters.
*
Expand Down
24 changes: 23 additions & 1 deletion src/Resources/public/Admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,29 @@ var Admin = {
});

jQuery('.sonata-filter-form', subject).on('submit', function () {
jQuery(this).find('[sonata-filter="true"]:hidden :input').val('');
var $form = jQuery(this);
$form.find('[sonata-filter="true"]:hidden :input').val('');

if (!this.dataset.defaultValues) {
return;
}

var defaultValues = $.param({'filter': JSON.parse(this.dataset.defaultValues)}).split('&'),
kirya-dev marked this conversation as resolved.
Show resolved Hide resolved
submittedValues = $form.serialize().split('&');

// Compare default and submitted filter values in `keyValue` representation. (`keyValue` ex: "filter[publish][value][end]=2020-12-12")
// Only allow to submit non default and non empty values, because empty values means they are not present.
var changedValues = submittedValues.filter(function (keyValue) {
return defaultValues.indexOf(keyValue) === -1 && keyValue.split('=')[1] !== '';
});

// Disable all inputs and enable only the required ones
$form.find('[name*=filter]').attr('disabled', 'disabled');
changedValues
.map(function (keyValue) { return decodeURIComponent(keyValue.split('=')[0]); })
.forEach(function (key) {
$form.find('[name="' + key + '"]').removeAttr('disabled');
});
});

/* Advanced filters */
Expand Down
11 changes: 10 additions & 1 deletion src/Resources/views/CRUD/base_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,16 @@ file that was distributed with this source code.
<div class="col-xs-12 col-md-12 sonata-filters-box" style="display: {{ admin.datagrid.hasDisplayableFilters ? 'block' : 'none' }}" id="filter-container-{{ admin.uniqid() }}">
<div class="box box-primary" >
<div class="box-body">
<form class="sonata-filter-form form-horizontal {{ admin.isChild and 1 == admin.datagrid.filters|length ? 'hide' : '' }}" action="{{ admin.generateUrl('list') }}" method="GET" role="form">
<form
class="sonata-filter-form form-horizontal {{ admin.isChild and 1 == admin.datagrid.filters|length ? 'hide' : '' }}"
action="{{ admin.generateUrl('list') }}"
method="GET"
role="form"
{# NEXT_MAJOR: Remove condition #}
kirya-dev marked this conversation as resolved.
Show resolved Hide resolved
{% if attribute(admin, 'getDefaultFilterParameters') is defined %}
data-default-values="{{ admin.defaultFilterParameters|json_encode }}"
{% endif %}
>
{{ form_errors(form) }}

<div class="row">
Expand Down