From cadb56eb077d3e77db32c49f7457b1ab014ad0d9 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Wed, 13 Dec 2023 14:30:53 +0100 Subject: [PATCH] new: [CRUD:Filtering] Added support of options in index filtering modal --- src/Controller/Component/CRUDComponent.php | 18 ++++++++++++-- templates/genericTemplates/filters.php | 29 ++++++++++++++++------ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index f8a227d..4922209 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -51,7 +51,9 @@ class CRUDComponent extends Component $options['filters'][] = 'filteringTags'; } $optionFilters = []; - $optionFilters += empty($options['filters']) ? [] : $options['filters']; + $optionFilters += array_map(function($filter) { + return is_array($filter) ? $filter['name'] : $filter; + }, empty($options['filters']) ? [] : $options['filters']); foreach ($optionFilters as $i => $filter) { $optionFilters[] = "{$filter} !="; $optionFilters[] = "{$filter} >="; @@ -259,6 +261,13 @@ class CRUDComponent extends Component foreach ($filtersConfigRaw as $fieldConfig) { if (is_array($fieldConfig)) { $filtersConfig[$fieldConfig['name']] = $fieldConfig; + if (!empty($fieldConfig['options'])) { + if (is_string($fieldConfig['options'])) { + $filtersConfig[$fieldConfig['name']]['options'] = $this->Table->{$fieldConfig['options']}($this->Controller->ACL->getUser()); + } else { + $filtersConfig[$fieldConfig['name']]['options'] = $fieldConfig['options']; + } + } } else { $filtersConfig[$fieldConfig] = ['name' => $fieldConfig]; } @@ -1368,7 +1377,12 @@ class CRUDComponent extends Component protected function setRelatedCondition($query, $modelName, $fieldName, $filterValue) { return $query->matching($modelName, function (\Cake\ORM\Query $q) use ($fieldName, $filterValue) { - return $this->setValueCondition($q, $fieldName, $filterValue); + if (is_array($filterValue)) { + $query = $this->setInCondition($q, $fieldName, $filterValue); + } else { + $query = $this->setValueCondition($q, $fieldName, $filterValue); + } + return $query; }); } diff --git a/templates/genericTemplates/filters.php b/templates/genericTemplates/filters.php index 4cb1df0..6117b9e 100644 --- a/templates/genericTemplates/filters.php +++ b/templates/genericTemplates/filters.php @@ -56,13 +56,22 @@ $filteringForm = $this->Bootstrap->table( 'label' => '', 'class' => 'fieldValue form-control-sm' ]; - if (!empty($filtersConfig[$fieldName]['multiple'])) { - $fieldData['type'] = 'dropdown'; - $fieldData['multiple'] = true; - $fieldData['select2'] = [ - 'tags' => true, - 'tokenSeparators' => [',', ' '], - ]; + if (!empty($filtersConfig[$fieldName])) { + if (!empty($filtersConfig[$fieldName]['options'])) { + $fieldData['options'] = $filtersConfig[$fieldName]['options']; + } + if (!empty($filtersConfig[$fieldName]['select2'])) { + $fieldData['type'] = 'dropdown'; + $fieldData['select2'] = true; + } + if (!empty($filtersConfig[$fieldName]['multiple'])) { + $fieldData['type'] = 'dropdown'; + $fieldData['multiple'] = true; + $fieldData['select2'] = [ + 'tags' => true, + 'tokenSeparators' => [',', ' '], + ]; + } } $this->Form->setTemplates([ 'formGroup' => '
{{input}}
', @@ -187,6 +196,7 @@ echo $this->Bootstrap->modal([ $row = $filteringTable.find('td > span.fieldName').filter(function() { return $(this).data('fieldname') == field }).closest('tr') + $row.addClass('table-warning') $row.find('.fieldOperator').val(operator) const $formElement = $row.find('.fieldValue'); if ($formElement.attr('type') === 'datetime-local') { @@ -195,7 +205,10 @@ echo $this->Bootstrap->modal([ let newOptions = []; value.forEach(aValue => { const existingOption = $formElement.find('option').filter(function() { - return $(this).val() === aValue + if ($(this).val() === aValue) { + $(this).prop('selected', true) + return true + } }) if (existingOption.length == 0) { newOptions.push(new Option(aValue, aValue, true, true))