Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/Grid/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
9 changes: 5 additions & 4 deletions src/Grid/GridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
}
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/translations/KibaticDatagridBundle.en.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Show all filters: Show all filters
Show less filters: Show less filters

column:
boolean:
'true': Yes
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/translations/KibaticDatagridBundle.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
48 changes: 48 additions & 0 deletions src/Resources/views/theme/bootstrap5/datagrid-filters.html.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
<style>
.datagrid-filters-toggle[aria-expanded="true"] .datagrid-filters-show-more { display: none; }
.datagrid-filters-toggle[aria-expanded="false"] .datagrid-filters-show-less { display: none; }
</style>

{{ 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 %}
<div class="row datagrid-filter-group">
{% for fieldName in row.fields %}
Expand All @@ -14,8 +33,24 @@
{% else %}
{{ form_row(form[row.fields[0]]) }}
{% endif %}
{% endmacro %}

{% for row in grid.filterLayout %}
{% if not row.hidden %}
{{ _self.render_row(row, form) }}
{% endif %}
{% endfor %}

{% if hasHiddenRows %}
<div class="collapse{% if hiddenRowHasValue %} show{% endif %}" id="datagrid-filters-advanced">
{% for row in grid.filterLayout %}
{% if row.hidden %}
{{ _self.render_row(row, form) }}
{% endif %}
{% endfor %}
</div>
{% endif %}

{% for field in form %}
{% if field.vars.block_prefixes[0] != 'button' and field.vars.name not in layoutFieldNames %}
{{ form_row(field) }}
Expand All @@ -25,6 +60,19 @@
<div class="row">
<div class="col text-end">
<ul class="list-inline">
{% if hasHiddenRows %}
<li class="list-inline-item">
<a href="#datagrid-filters-advanced"
class="btn btn-link datagrid-filters-toggle"
data-bs-toggle="collapse"
aria-expanded="{{ hiddenRowHasValue ? 'true' : 'false' }}"
aria-controls="datagrid-filters-advanced">
<span class="datagrid-filters-show-more">{{ 'Show all filters'|trans({}, 'KibaticDatagridBundle') }}</span>
<span class="datagrid-filters-show-less">{{ 'Show less filters'|trans({}, 'KibaticDatagridBundle') }}</span>
</a>
</li>
{% endif %}

<li class="list-inline-item">
<a href="{{ datagrid_reset_url(form) }}" class="btn btn-default">
{{ 'Reset'|trans({}, 'KibaticDatagridBundle') }}
Expand Down