From 025f1a9071c8957b145d1c8475540f16bc488cc7 Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Sat, 11 Feb 2017 15:40:43 +0100 Subject: [PATCH] fixed perserving empty filter for fields which are not empty by default --- src/Datagrid.php | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index 67644ee..d9f4a61 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -11,6 +11,8 @@ use Nette\Application\UI; use Nette\Bridges\ApplicationLatte\Template; +use Nette\Forms\Container; +use Nette\Forms\Controls\Button; use Nette\Utils\Html; use Nette\Utils\Paginator; use Nette\Localization\ITranslator; @@ -418,12 +420,7 @@ public function createComponentForm() $form['filter']->addSubmit('cancel', $this->translate('Cancel')); } - $this->filterDefaults = []; - foreach ($form['filter']->controls as $name => $control) { - $this->filterDefaults[$name] = $control->getValue(); - } - $this->filterDefaults = $this->filterFormFilter($this->filterDefaults); - + $this->prepareFilterDefaults($form['filter']); if (!$this->filterDataSource) { $this->filterDataSource = $this->filterDefaults; } @@ -530,16 +527,35 @@ public function handlePaginate() } - private function filterFormFilter($values) + private function prepareFilterDefaults(Container $container) { - return array_filter($values, function($val) { - if (is_array($val)) { - return !empty($val); + $this->filterDefaults = []; + foreach ($container->controls as $name => $control) { + if ($control instanceof Button) { + continue; } - if (is_string($val)) { - return strlen($val) > 0; + + $value = $control->getValue(); + $isNonEmptyValue = + (is_array($value) && !empty($value)) + || (is_string($value) && strlen($value) > 0) + || (!is_array($value) && !is_string($value) && $value !== null); + if ($isNonEmptyValue) { + $this->filterDefaults[$name] = $value; + } + } + } + + + private function filterFormFilter(array $values) + { + $filtered = []; + foreach ($values as $key => $val) { + $default = isset($this->filterDefaults[$key]) ? $this->filterDefaults[$key] : null; + if ($default !== $val) { + $filtered[$key] = $val; } - return $val !== null; - }); + } + return $filtered; } }