Skip to content

Commit

Permalink
fixed perserving empty filter for fields which are not empty by default
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Feb 11, 2017
1 parent 6e61244 commit 025f1a9
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 025f1a9

Please sign in to comment.