Skip to content
Merged
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
34 changes: 18 additions & 16 deletions src/Component/DataGrid.latte
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,23 @@
{$export->setClass('btn noajax')->render()}
{/foreach}
</span>
{block isActive}
{ifset $filters['isActive']}
{if isset($control['filter']['filter']) && isset($control['filter']['filter'][$filters['isActive']->getKey()])}
{snippet isActive}
{var $form = $control['filter']}
<span n:ifset="$filters['isActive']" class="datagrid-exports d-flex align-items-center">
{include $filters['isActive']->getTemplate(),
filter => $filters['isActive'],
input => $form['filter'][$filters['isActive']->getKey()],
outer => TRUE
}
</span>
{/snippet}
{/if}
{/ifset}
{block switchers}
{snippet switchers}
{var $form = $control['filter']}
{foreach $switcherValues as $switcherKey => $_}
{ifset $filters[$switcherKey]}
{if isset($control['filter']['filter']) && isset($control['filter']['filter'][$filters[$switcherKey]->getKey()])}
<span class="datagrid-exports d-flex align-items-center">
{include $filters[$switcherKey]->getTemplate(),
filter => $filters[$switcherKey],
input => $form['filter'][$filters[$switcherKey]->getKey()],
outer => TRUE
}
</span>
{/if}
{/ifset}
{/foreach}
{/snippet}
{/block}
</div>
</div>
Expand All @@ -151,7 +153,7 @@
{var $i = 0}
{var $filter_columns_class = 'col-sm-' . (12 / $control->getOuterFilterColumnsCount())}
{foreach $filters as $filterName => $f}
{continueIf in_array($filterName, ['search', 'isActive'])}
{continueIf in_array($filterName, ['search', 'isActive']) || isset($switcherValues[$filterName])}
{**
* Each fitler is rendered separately in its own template
*}
Expand Down
80 changes: 61 additions & 19 deletions src/Component/DataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class DataGrid extends \Contributte\Datagrid\Datagrid
protected string $gridName;
protected string $email;
private bool $isActiveValue = true;
private array $switcherValues = [];
protected ?string $parentTemplate = null;

public function getSessionData(?string $key = null, mixed $defaultValue = null): array
Expand Down Expand Up @@ -128,6 +129,7 @@ public function render(): void
$this->template->showTableFoot = $this->showTableFoot;
$this->template->toolbarButons = $this->toolbarButtons;
$this->template->isActiveValue = $this->isActiveValue;
$this->template->switcherValues = $this->switcherValues;
$this->template->gridFilters = $this->gridFilterQueryFactory->create()->byGrid($this->gridName)->fetch();
$this->template->parentTemplate = $this->parentTemplate;

Expand Down Expand Up @@ -330,34 +332,74 @@ public function addExportExcel(
* @throws DataGridException
*/
public function addIsActiveSwitcher(): void
{
$this->addSwitcher(
'isActive',
'app.forms.global.isActive.label',
function (IsActiveFilter $query, $value) {
if (!$value) {
$query->disableIsActiveFilter();
}
},
title: 'app.forms.global.isActive.title'
);

$this->isActiveValue = $this->switcherValues['isActive'];
}

/**
* @throws DataGridException
*/
public function addSwitcher(string $key, string $name, callable $callback, ?string $column = null, ?string $title = null, bool $defaultValue = true): FilterSwitcher
{
$postRequest = $this->getPresenter()->getRequest()->getPost('filter');
$isActive = $this->addFilterSwitcher('isActive', 'app.forms.global.isActive.label', 'isActive')
->setValue(true)
->addAttribute('title', $this->translator->translate('app.forms.global.isActive.title'));
$switcher = $this->addFilterSwitcher($key, $name, $column)
->setValue($defaultValue)
->setCondition($callback);

if ($title) {
$switcher->addAttribute('title', $this->translator->translate($title));
}

$this->switcherValues[$key] = $defaultValue;

if (
(isset($this->params['filter']['isActive']) && (
$this->params['filter']['isActive'] === 'true'
|| $this->params['filter']['isActive'] === '1')
) || (isset($postRequest['isActive']) && $postRequest['isActive'] === '1')
(
isset($this->params['filter'][$key])
&&
(
$this->params['filter'][$key] === 'true'
||
$this->params['filter'][$key] === '1'
)
)
||
(isset($postRequest[$key]) && $postRequest[$key] === '1')
) {
$isActive
$switcher
->setValue(true)
->setDefaultValue(true)
->addAttribute('checked', 'checked');
}
elseif (
(isset($this->params['filter']['isActive']) && (
$this->params['filter']['isActive'] === 'false'
|| $this->params['filter']['isActive'] === '0')
) || (isset($postRequest['isActive']) && $postRequest['isActive'] === '0')

$this->switcherValues[$key] = true;

} elseif (
(
isset($this->params['filter'][$key])
&&
(
$this->params['filter'][$key] === 'false'
||
$this->params['filter'][$key] === '0'
)
)
||
(isset($postRequest[$key]) && $postRequest[$key] === '0')
) {
$isActive->setCondition(function (IsActiveFilter $query) {
$query->disableIsActiveFilter();
});
$this->isActiveValue = false;
$this->switcherValues[$key] = false;
}

return $switcher;
}

/**
Expand All @@ -373,7 +415,7 @@ public function addFilterSwitcher(

$this->addFilterCheck($key);

return $this->filters[$key] = new FilterSwitcher($this, $key, $name, $column);
return $this->filters[$key] = new FilterSwitcher($this, $key, $this->translator->translate($name), $column);
}

public function isFilterActive(?string $filter = null): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Component/datagrid_filter_switcher.latte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param Nette\Forms\Controls\TextInput $input
*}
<div class="form-check form-switch">
{php $input->setValue($isActiveValue)} {* TODO *}
{php $input->setValue($switcherValues[$filter->getKey()] ?? null)}
<input n:name="$input" type="hidden" value="0" />
<input n:name="$input" value="1" />
</div>