Skip to content

Commit

Permalink
Allow coexistence of both outer and column filters
Browse files Browse the repository at this point in the history
  • Loading branch information
survik1 committed Oct 18, 2023
1 parent e7b0ceb commit 6ad8d77
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 5 deletions.
29 changes: 28 additions & 1 deletion src/DataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ class DataGrid extends Control

/**
* @var bool
* @deprecated
*/
protected $outerFilterRendering = false;

Expand Down Expand Up @@ -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);
}
};

Check failure on line 1651 in src/DataGrid.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.1)

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 1651 in src/DataGrid.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.1)

Closure does not have void return type hint.

Check failure on line 1652 in src/DataGrid.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.1)

Tabs must be used to indent lines; spaces are not allowed
$this->outerFilterRendering = $outerFilterRendering;

Check failure on line 1653 in src/DataGrid.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.1)

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 1654 in src/DataGrid.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.1)

Tabs must be used to indent lines; spaces are not allowed
return $this;

Check failure on line 1655 in src/DataGrid.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.1)

Tabs must be used to indent lines; spaces are not allowed
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions src/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ abstract class Filter
'class' => ['form-control', 'input-sm', 'form-control-sm'],
];

/**
* @var bool
*/
protected $outerRendering = false;

/**
* @var string|null
*/
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/templates/datagrid.latte
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
{var $i = 0}
{var $filterColumnsClass = 'col-sm-' . (12 / $control->getOuterFilterColumnsCount())}
<div class="datagrid-row-outer-filters-group {$filterColumnsClass}" n:foreach="$filters as $f">
{continueIf !$f->hasOuterRendering()}

{**
* Each fitler is rendered separately in its own template
*}
Expand Down Expand Up @@ -138,7 +140,7 @@
</th>
</tr>
<tr n:block="header-column-row">
<th n:snippet="thead-group-action" n:if="$hasGroupActions" n:attr="'rowspan=2' => !empty($filters) && !$control->hasOuterFilterRendering()" class="col-checkbox">
<th n:snippet="thead-group-action" n:if="$hasGroupActions" n:attr="'rowspan=2' => !empty($filters) && $control->hasColumnFilterRendering()" class="col-checkbox">
<input n:if="$hasGroupActionOnRows" n:class="$control->shouldUseHappyComponents() ? 'happy gray-border' , primary" name="{$control->getFullName()|lower}-toggle-all" type="checkbox" data-check="{$control->getFullName()}" data-check-all="{$control->getFullName()}">
</th>
{foreach $columns as $key => $column}
Expand Down Expand Up @@ -196,12 +198,12 @@
{='ublaboo_datagrid.action'|translate}
</th>
</tr>
<tr n:block="header-filters" n:if="!empty($filters) && !$control->hasOuterFilterRendering()">
<tr n:block="header-filters" n:if="!empty($filters) && $control->hasColumnFilterRendering()">
{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()}
Expand All @@ -221,7 +223,7 @@
{$th->endTag()|noescape}
{/foreach}
<th n:if="$actions || $control->isSortable() || $itemsDetail || $inlineEdit || $inlineAdd" class="col-action text-center">
{if !$control->hasAutoSubmit() && !$control->hasOuterFilterRendering()}
{if !$control->hasAutoSubmit() && $control->hasColumnFilterRendering()}
{input $filter['filter']['submit']}
{/if}
</th>
Expand Down
32 changes: 32 additions & 0 deletions tests/Cases/FilterTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

0 comments on commit 6ad8d77

Please sign in to comment.