diff --git a/src/Grid/Filter.php b/src/Grid/Filter.php index d1249e9..703faa1 100644 --- a/src/Grid/Filter.php +++ b/src/Grid/Filter.php @@ -8,16 +8,19 @@ class Filter public $callback; public bool $enabled; public ?string $group; + public bool $hidden; public function __construct( string $formFieldName, callable $callback, bool $enabled = true, - ?string $group = null + ?string $group = null, + bool $hidden = false, ) { $this->formFieldName = $formFieldName; $this->callback = $callback; $this->enabled = $enabled; $this->group = $group; + $this->hidden = $hidden; } } diff --git a/src/Grid/GridBuilder.php b/src/Grid/GridBuilder.php index 87af95e..7a548f6 100644 --- a/src/Grid/GridBuilder.php +++ b/src/Grid/GridBuilder.php @@ -145,9 +145,9 @@ public function removeColumn(string $name): self return $this; } - public function addFilter(string $formFieldName, callable $callback, bool $enabled = true, ?string $group = null): self + public function addFilter(string $formFieldName, callable $callback, bool $enabled = true, ?string $group = null, bool $hidden = false): self { - $this->filters[] = new Filter($formFieldName, $callback, $enabled, $group); + $this->filters[] = new Filter($formFieldName, $callback, $enabled, $group, $hidden); return $this; } @@ -182,16 +182,17 @@ private function buildFilterLayout(): array if ($filter->group === null) { // Ungrouped filter: occupies its own slot. - $layout[] = ['fields' => [$filter->formFieldName], 'group' => null]; + $layout[] = ['fields' => [$filter->formFieldName], 'group' => null, 'hidden' => $filter->hidden]; } elseif (!in_array($filter->group, $processedGroups)) { // First time we encounter this group: collect all enabled fields belonging // to it (in declaration order) and emit a single slot for the whole group. + // The hidden flag of the first filter in the group applies to the whole group. $processedGroups[] = $filter->group; $groupFields = array_map( fn(Filter $f) => $f->formFieldName, array_filter($this->filters, fn(Filter $f) => $f->enabled && $f->group === $filter->group) ); - $layout[] = ['fields' => array_values($groupFields), 'group' => $filter->group]; + $layout[] = ['fields' => array_values($groupFields), 'group' => $filter->group, 'hidden' => $filter->hidden]; } // Subsequent members of an already-processed group are intentionally skipped. } diff --git a/src/Resources/translations/KibaticDatagridBundle.en.yaml b/src/Resources/translations/KibaticDatagridBundle.en.yaml index 9f9fc7f..064254f 100644 --- a/src/Resources/translations/KibaticDatagridBundle.en.yaml +++ b/src/Resources/translations/KibaticDatagridBundle.en.yaml @@ -1,3 +1,6 @@ +Show all filters: Show all filters +Show less filters: Show less filters + column: boolean: 'true': Yes diff --git a/src/Resources/translations/KibaticDatagridBundle.fr.yaml b/src/Resources/translations/KibaticDatagridBundle.fr.yaml index 360dcb9..ea6edc3 100644 --- a/src/Resources/translations/KibaticDatagridBundle.fr.yaml +++ b/src/Resources/translations/KibaticDatagridBundle.fr.yaml @@ -4,6 +4,8 @@ Select all: Tout selectionner Sort: Trier Filter: Filtrer Reset: Réinitialiser +Show all filters: Voir tous les filtres +Show less filters: Voir moins de filtres column: boolean: diff --git a/src/Resources/views/theme/bootstrap5/datagrid-filters.html.twig b/src/Resources/views/theme/bootstrap5/datagrid-filters.html.twig index da620dd..543e0c6 100644 --- a/src/Resources/views/theme/bootstrap5/datagrid-filters.html.twig +++ b/src/Resources/views/theme/bootstrap5/datagrid-filters.html.twig @@ -1,8 +1,27 @@ + + {{ form_start(form) }} {% set layoutFieldNames = [] %} +{% set hasHiddenRows = false %} +{% set hiddenRowHasValue = false %} + {% for row in grid.filterLayout %} {% set layoutFieldNames = layoutFieldNames|merge(row.fields) %} + {% if row.hidden %} + {% set hasHiddenRows = true %} + {% for fieldName in row.fields %} + {% if form[fieldName].vars.data is not empty %} + {% set hiddenRowHasValue = true %} + {% endif %} + {% endfor %} + {% endif %} +{% endfor %} + +{% macro render_row(row, form) %} {% if row.fields|length > 1 %}