From f38cc74ef1dba3f6274206c7aef919d33abfe819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Wr=C3=B3blewski?= Date: Fri, 11 Oct 2024 17:03:12 +0200 Subject: [PATCH] fix: #137 --- src/DataCollector/DataTableDataCollector.php | 8 ++ .../DataTableDataCollectorInterface.php | 3 + src/DataCollector/DataTableDataExtractor.php | 3 - src/DataTable.php | 1 + src/Resources/views/themes/base.html.twig | 80 ++++++++++++++++++- 5 files changed, 89 insertions(+), 6 deletions(-) diff --git a/src/DataCollector/DataTableDataCollector.php b/src/DataCollector/DataTableDataCollector.php index 5c4287a1..d36222d0 100644 --- a/src/DataCollector/DataTableDataCollector.php +++ b/src/DataCollector/DataTableDataCollector.php @@ -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; @@ -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); diff --git a/src/DataCollector/DataTableDataCollectorInterface.php b/src/DataCollector/DataTableDataCollectorInterface.php index f3fa6d35..602964e5 100644 --- a/src/DataCollector/DataTableDataCollectorInterface.php +++ b/src/DataCollector/DataTableDataCollectorInterface.php @@ -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; @@ -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; diff --git a/src/DataCollector/DataTableDataExtractor.php b/src/DataCollector/DataTableDataExtractor.php index e8574464..e80a1cef 100644 --- a/src/DataCollector/DataTableDataExtractor.php +++ b/src/DataCollector/DataTableDataExtractor.php @@ -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']); diff --git a/src/DataTable.php b/src/DataTable.php index d47e957f..b04cfaf1 100755 --- a/src/DataTable.php +++ b/src/DataTable.php @@ -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 diff --git a/src/Resources/views/themes/base.html.twig b/src/Resources/views/themes/base.html.twig index 7ad3e988..2bc1d98d 100755 --- a/src/Resources/views/themes/base.html.twig +++ b/src/Resources/views/themes/base.html.twig @@ -205,9 +205,29 @@ {% block pagination_per_page_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({})) %} #} +{% 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 })) } %} + + {% endwith %} + {% endif %} + {% endfor %} +{% endmacro %}