From 6ad8d779a5c6a6729a6aa35a0d469921d5b5ac62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Neumaier?= Date: Wed, 18 Oct 2023 13:30:21 +0200 Subject: [PATCH 1/2] Allow coexistence of both outer and column filters --- src/DataGrid.php | 29 ++++++++++++++++++++++++++++- src/Filter/Filter.php | 22 ++++++++++++++++++++++ src/templates/datagrid.latte | 10 ++++++---- tests/Cases/FilterTest.phpt | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 5 deletions(-) diff --git a/src/DataGrid.php b/src/DataGrid.php index 81e328da2..128ddea8c 100644 --- a/src/DataGrid.php +++ b/src/DataGrid.php @@ -313,6 +313,7 @@ class DataGrid extends Control /** * @var bool + * @deprecated */ protected $outerFilterRendering = false; @@ -1636,11 +1637,19 @@ public function filterSucceeded(NetteForm $form): void $this->reload(); } + /** * @return static */ public function setOuterFilterRendering(bool $outerFilterRendering = true): self { + $this->onFiltersAssembled['setOuterFilterRendering'] = function (iterable $filters) use ($outerFilterRendering) { + /** @var Filter $filter */ + foreach ($filters as $filter) { + $filter->setOuterRendering($outerFilterRendering); + } + }; + $this->outerFilterRendering = $outerFilterRendering; return $this; @@ -1649,10 +1658,28 @@ public function setOuterFilterRendering(bool $outerFilterRendering = true): self public function hasOuterFilterRendering(): bool { - return $this->outerFilterRendering; + foreach ($this->filters as $filter) { + if ($filter->hasOuterRendering()) { + return true; + } + } + + return false; } + public function hasColumnFilterRendering(): bool + { + foreach ($this->filters as $filter) { + if (!$filter->hasOuterRendering()) { + return true; + } + } + + return false; + } + + /** * @return static * @throws InvalidArgumentException diff --git a/src/Filter/Filter.php b/src/Filter/Filter.php index 18e13813b..0b50c3e79 100644 --- a/src/Filter/Filter.php +++ b/src/Filter/Filter.php @@ -61,6 +61,11 @@ abstract class Filter 'class' => ['form-control', 'input-sm', 'form-control-sm'], ]; + /** + * @var bool + */ + protected $outerRendering = false; + /** * @var string|null */ @@ -192,6 +197,23 @@ public function getType(): ?string } + /** + * @return static + */ + public function setOuterRendering(bool $outerRendering = true): self + { + $this->outerRendering = $outerRendering; + + return $this; + } + + + public function hasOuterRendering(): bool + { + return $this->outerRendering; + } + + /** * @param mixed $value * @return static diff --git a/src/templates/datagrid.latte b/src/templates/datagrid.latte index 3a70f59bb..e62588e2e 100644 --- a/src/templates/datagrid.latte +++ b/src/templates/datagrid.latte @@ -40,6 +40,8 @@ {var $i = 0} {var $filterColumnsClass = 'col-sm-' . (12 / $control->getOuterFilterColumnsCount())}
+ {continueIf !$f->hasOuterRendering()} + {** * Each fitler is rendered separately in its own template *} @@ -138,7 +140,7 @@ - + {foreach $columns as $key => $column} @@ -196,12 +198,12 @@ {='ublaboo_datagrid.action'|translate} - + {foreach $columns as $key => $column} {var $th = $column->getElementForRender('th', $key)} {$th->startTag()|noescape} {var $col_header = 'col-filter-' . $key . '-header'} - {if !$control->hasOuterFilterRendering() && isset($filters[$key])} + {if isset($filters[$key]) && !$filters[$key]->hasOuterRendering()} {var $i = $filter['filter'][$key]} {var $filter_block = 'filter-' . $filters[$key]->getKey()} @@ -221,7 +223,7 @@ {$th->endTag()|noescape} {/foreach} - {if !$control->hasAutoSubmit() && !$control->hasOuterFilterRendering()} + {if !$control->hasAutoSubmit() && $control->hasColumnFilterRendering()} {input $filter['filter']['submit']} {/if} diff --git a/tests/Cases/FilterTest.phpt b/tests/Cases/FilterTest.phpt index 2734f997a..c6a74fc85 100644 --- a/tests/Cases/FilterTest.phpt +++ b/tests/Cases/FilterTest.phpt @@ -10,6 +10,7 @@ use Nette\Application\AbortException; use Tester\Assert; use Tester\TestCase; use Ublaboo\DataGrid\DataGrid; +use Ublaboo\DataGrid\Tests\Files\TestingDataGridFactory; use Ublaboo\DataGrid\Tests\Files\TestingDataGridFactoryRouter; final class FilterTest extends TestCase @@ -27,6 +28,37 @@ final class FilterTest extends TestCase }, AbortException::class); } + public function testFilterRendering(): void + { + $factory = new TestingDataGridFactory(); + /** @var DataGrid $grid */ + $grid = $factory->createTestingDataGrid(); + + $grid->addFilterText('default', 'default'); + $grid->assembleFilters(); + Assert::false($grid->hasOuterFilterRendering()); + Assert::true($grid->hasColumnFilterRendering()); + + $grid->setOuterFilterRendering(); + $grid->assembleFilters(); + Assert::true($grid->hasOuterFilterRendering()); + Assert::false($grid->hasColumnFilterRendering()); + + $grid->removeFilter('default'); + $filters = $grid->assembleFilters(); + Assert::count(0, $filters); + + $grid->addFilterText('outerFilter', 'outerFilter') + ->setOuterRendering(); + $grid->assembleFilters(); + Assert::true($grid->hasOuterFilterRendering()); + Assert::false($grid->hasColumnFilterRendering()); + + $grid->addFilterText('columnFilter', 'columnFilter'); + Assert::true($grid->hasOuterFilterRendering()); + Assert::true($grid->hasColumnFilterRendering()); + } + } (new FilterTest)->run(); From f9a8d599ea52ebe09bdc3cb9229bcfacd2d5eddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Neumaier?= Date: Wed, 6 Dec 2023 10:41:52 +0100 Subject: [PATCH 2/2] Fix formatting --- src/DataGrid.php | 44 ++++++++++++++++++------------------ src/Filter/Filter.php | 32 +++++++++++++------------- src/templates/datagrid.latte | 2 +- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/DataGrid.php b/src/DataGrid.php index 128ddea8c..a79f8b6b4 100644 --- a/src/DataGrid.php +++ b/src/DataGrid.php @@ -313,7 +313,7 @@ class DataGrid extends Control /** * @var bool - * @deprecated + * @deprecated */ protected $outerFilterRendering = false; @@ -1643,13 +1643,13 @@ public function filterSucceeded(NetteForm $form): void */ public function setOuterFilterRendering(bool $outerFilterRendering = true): self { - $this->onFiltersAssembled['setOuterFilterRendering'] = function (iterable $filters) use ($outerFilterRendering) { - /** @var Filter $filter */ - foreach ($filters as $filter) { - $filter->setOuterRendering($outerFilterRendering); - } - }; - + $this->onFiltersAssembled['setOuterFilterRendering'] = function (iterable $filters) use ($outerFilterRendering) { + /** @var Filter $filter */ + foreach ($filters as $filter) { + $filter->setOuterRendering($outerFilterRendering); + } + }; + $this->outerFilterRendering = $outerFilterRendering; return $this; @@ -1658,26 +1658,26 @@ public function setOuterFilterRendering(bool $outerFilterRendering = true): self public function hasOuterFilterRendering(): bool { - foreach ($this->filters as $filter) { - if ($filter->hasOuterRendering()) { - return true; - } - } + foreach ($this->filters as $filter) { + if ($filter->hasOuterRendering()) { + return true; + } + } return false; } - public function hasColumnFilterRendering(): bool - { - foreach ($this->filters as $filter) { - if (!$filter->hasOuterRendering()) { - return true; - } - } + public function hasColumnFilterRendering(): bool + { + foreach ($this->filters as $filter) { + if (!$filter->hasOuterRendering()) { + return true; + } + } - return false; - } + return false; + } /** diff --git a/src/Filter/Filter.php b/src/Filter/Filter.php index 0b50c3e79..6d4ac6f57 100644 --- a/src/Filter/Filter.php +++ b/src/Filter/Filter.php @@ -61,10 +61,10 @@ abstract class Filter 'class' => ['form-control', 'input-sm', 'form-control-sm'], ]; - /** - * @var bool - */ - protected $outerRendering = false; + /** + * @var bool + */ + protected $outerRendering = false; /** * @var string|null @@ -197,21 +197,21 @@ public function getType(): ?string } - /** - * @return static - */ - public function setOuterRendering(bool $outerRendering = true): self - { - $this->outerRendering = $outerRendering; + /** + * @return static + */ + public function setOuterRendering(bool $outerRendering = true): self + { + $this->outerRendering = $outerRendering; - return $this; - } + return $this; + } - public function hasOuterRendering(): bool - { - return $this->outerRendering; - } + public function hasOuterRendering(): bool + { + return $this->outerRendering; + } /** diff --git a/src/templates/datagrid.latte b/src/templates/datagrid.latte index e62588e2e..e1a4ec50d 100644 --- a/src/templates/datagrid.latte +++ b/src/templates/datagrid.latte @@ -40,7 +40,7 @@ {var $i = 0} {var $filterColumnsClass = 'col-sm-' . (12 / $control->getOuterFilterColumnsCount())}
- {continueIf !$f->hasOuterRendering()} + {continueIf !$f->hasOuterRendering()} {** * Each fitler is rendered separately in its own template