Skip to content

Commit

Permalink
fix: #137
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Oct 11, 2024
1 parent cc07580 commit f38cc74
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/DataCollector/DataTableDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Kreyu\Bundle\DataTableBundle\Filter\FilterInterface;
use Kreyu\Bundle\DataTableBundle\Filter\FilterView;
use Kreyu\Bundle\DataTableBundle\Filter\FiltrationData;
use Kreyu\Bundle\DataTableBundle\Pagination\PaginationData;
use Kreyu\Bundle\DataTableBundle\Sorting\SortingData;
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -118,6 +119,13 @@ public function collectSortingData(DataTableInterface $dataTable, SortingData $d
}
}

public function collectPaginationData(DataTableInterface $dataTable, PaginationData $data): void
{
$this->data[$dataTable->getName()]['page'] = $data->getPage();
$this->data[$dataTable->getName()]['per_page'] = $data->getPerPage();
$this->data[$dataTable->getName()]['total_count'] = $dataTable->getPagination()->getTotalItemCount();
}

public function collectFilterView(FilterInterface $filter, FilterView $view): void
{
$this->data[$filter->getDataTable()->getName()]['filters'][$filter->getName()]['view_vars'] = $this->ksort($view->vars);
Expand Down
3 changes: 3 additions & 0 deletions src/DataCollector/DataTableDataCollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Kreyu\Bundle\DataTableBundle\Filter\FilterInterface;
use Kreyu\Bundle\DataTableBundle\Filter\FilterView;
use Kreyu\Bundle\DataTableBundle\Filter\FiltrationData;
use Kreyu\Bundle\DataTableBundle\Pagination\PaginationData;
use Kreyu\Bundle\DataTableBundle\Sorting\SortingData;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
use Symfony\Component\VarDumper\Cloner\Data;
Expand All @@ -30,6 +31,8 @@ public function collectColumnValueView(ColumnInterface $column, ColumnValueView

public function collectSortingData(DataTableInterface $dataTable, SortingData $data): void;

public function collectPaginationData(DataTableInterface $dataTable, PaginationData $data): void;

public function collectFilterView(FilterInterface $filter, FilterView $view): void;

public function collectFiltrationData(DataTableInterface $dataTable, FiltrationData $data): void;
Expand Down
3 changes: 0 additions & 3 deletions src/DataCollector/DataTableDataExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public function extractDataTableConfiguration(DataTableInterface $dataTable): ar
'persistence_enabled' => $dataTable->getConfig()->isPersonalizationPersistenceEnabled(),
],
],
'page' => $dataTable->getPagination()->getCurrentPageNumber(),
'per_page' => $dataTable->getPagination()->getItemNumberPerPage(),
'total_count' => $dataTable->getPagination()->getTotalItemCount(),
];

ksort($data['passed_options']);
Expand Down
1 change: 1 addition & 0 deletions src/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ private function dispatch(string $eventName, DataTableEvent $event): void
private function resetPagination(): void
{
$this->pagination = null;
$this->resultSet = null;
}

private function getInitialPaginationData(): ?PaginationData
Expand Down
80 changes: 77 additions & 3 deletions src/Resources/views/themes/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,29 @@

{% block pagination_per_page_form %}
<form>
{% set url_query_parameters = data_table.vars.url_query_parameters %}

{#
Changing the "per page" parameter automatically changes page to the first one.
You can disable this behavior by in your own theme that extends this one, for example:
{% block pagination_per_page_form %}
{% with { should_reset_to_first_page: false } %}
{{ parent() }}
{% endwith %}
{% endblock %}
#}

{% if should_reset_to_first_page ?? true %}
{% set url_query_parameters = url_query_parameters|merge({ (data_table.vars.page_parameter_name): 1 }) %}
{% endif %}

{{ _self.array_to_form_inputs(url_query_parameters) }}

{% set select_attr = {
name: data_table.vars.per_page_parameter_name,
onchange: 'this.form.submit()',
autocomplete: 'off',
}|merge(select_attr|default({})) %}

<select {% with { attr: select_attr } %}{{ block('attributes') }}{% endwith %}>
Expand All @@ -219,9 +239,11 @@
{% endblock %}

{% block pagination_counters %}
<span {{- block('attributes') -}}>
{{- block('pagination_counters_message', theme) -}}
</span>
{% if total_item_count > 0 %}
<span {{- block('attributes') -}}>
{{- block('pagination_counters_message', theme) -}}
</span>
{% endif %}
{% endblock %}

{% block pagination_counters_message %}
Expand Down Expand Up @@ -308,6 +330,35 @@
{% if form.count > 0 %}
{{ block('filtration_widget', theme) }}
{% endif %}

{#
Submitting a filtration form should keep current "per page" and change current page to the first one.
You can disable this behavior by in your own theme that extends this one, for example:
{% block kreyu_data_table_filters_form %}
{% with { should_reset_to_first_page: false, should_keep_per_page: false } %}
{{ parent() }}
{% endwith %}
{% endblock %}
#}

{% set data_table = form.vars.data_table_view %}

{% if data_table.vars.pagination_enabled %}
{% set url_query_parameters = [] %}

{% if should_reset_to_first_page ?? true %}
{% set url_query_parameters = url_query_parameters|merge({ (data_table.vars.page_parameter_name): 1 }) %}
{% endif %}

{% if should_keep_per_page ?? true %}
{% set url_query_parameters = url_query_parameters|merge({
(data_table.vars.per_page_parameter_name): data_table.vars.pagination.vars.item_number_per_page,
}) %}
{% endif %}

{{ _self.array_to_form_inputs(url_query_parameters, { form: form.vars.id }) }}
{% endif %}
{% endblock %}

{% block filtration_widget %}
Expand Down Expand Up @@ -584,3 +635,26 @@
{% block attributes %}
{% for key, value in attr|default({}) %}{{ key }}="{{ value }}"{% endfor %}
{% endblock %}
{# Transforms given array to form inputs. Supports nested arrays. #}
{# For example, ['foo' => ['bar' => 'baz']] will be rendered as: #}
{# <input name="foo[bar]" value="baz"> #}
{% macro array_to_form_inputs(input, attr = [], parent = null) %}
{% for key, value in input %}
{% if value is iterable %}
{% if parent is not null %}
{% set key = parent ~ '[' ~ key ~ ']' %}
{% endif %}
{{ _self.array_to_form_inputs(value, attr, key) }}
{% else %}
{% if parent is not null %}
{% set key = '[' ~ key ~ ']' %}
{% endif %}
{% with { attr: { type: 'hidden' }|merge(attr|merge({ name: parent ~ key, value })) } %}
<input {{ block('attributes') }}>
{% endwith %}
{% endif %}
{% endfor %}
{% endmacro %}

0 comments on commit f38cc74

Please sign in to comment.