From e3fd646122022303f1ace7ce54af12f5faf57dcb Mon Sep 17 00:00:00 2001 From: Alberto Rodriguez Date: Tue, 8 Aug 2023 15:42:09 -0400 Subject: [PATCH 1/2] feat: support multiple order fields, WIP --- .../DatatablesPaginatorComponent.php | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Controller/Component/DatatablesPaginatorComponent.php b/src/Controller/Component/DatatablesPaginatorComponent.php index 2d2902b..9ec0188 100644 --- a/src/Controller/Component/DatatablesPaginatorComponent.php +++ b/src/Controller/Component/DatatablesPaginatorComponent.php @@ -6,6 +6,7 @@ use Cake\Controller\Component\PaginatorComponent; use Cake\Datasource\ResultSetInterface; use Cake\Http\ServerRequest; +use Migrations\ConfigurationTrait; /** * DatatablesPaginator component @@ -52,12 +53,34 @@ protected function applyOrder(array $data, array $settings): array continue; } $colName = $dtColumns[$colIndex]['data']; - $settings['order'][$colName] = $colOrder; + + $settings['order'] = array_merge($settings['order'] ?? [], $this->filterOrder($colName, $colOrder)); } return $settings; } + /** + * @param string $colName + * @param string $colOrder + * @return array + */ + protected function filterOrder(string $colName, string $colOrder): array + { + $filters = $this->getConfig('filterOrder'); + + if (empty($filters)) { + return [$colName => $colOrder]; + } + + $output = []; + if (isset($filters[$colName])) { + $output = array_fill_keys($filters[$colName], $colOrder); + } + + return $output; + } + /** * Translate limit and offset from datatables * From abaccf7c9f7f212b1c183a9e22650f380b269ead Mon Sep 17 00:00:00 2001 From: Alberto Rodriguez Date: Tue, 8 Aug 2023 15:47:34 -0400 Subject: [PATCH 2/2] fix: filterOrder default return --- .../Component/DatatablesPaginatorComponent.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Controller/Component/DatatablesPaginatorComponent.php b/src/Controller/Component/DatatablesPaginatorComponent.php index 9ec0188..2d2e2dc 100644 --- a/src/Controller/Component/DatatablesPaginatorComponent.php +++ b/src/Controller/Component/DatatablesPaginatorComponent.php @@ -67,18 +67,12 @@ protected function applyOrder(array $data, array $settings): array */ protected function filterOrder(string $colName, string $colOrder): array { - $filters = $this->getConfig('filterOrder'); - - if (empty($filters)) { - return [$colName => $colOrder]; - } - - $output = []; + $filters = $this->getConfig('filterOrder') ?? []; if (isset($filters[$colName])) { - $output = array_fill_keys($filters[$colName], $colOrder); + return array_fill_keys($filters[$colName] ?? [], $colOrder); } - return $output; + return [$colName => $colOrder]; } /**