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

Allow coexistence of both outer and column filters #1103

Closed
Closed
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
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);
}
};

$this->outerFilterRendering = $outerFilterRendering;

return $this;
Expand All @@ -1649,7 +1658,25 @@ 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;
}


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();