new: [CRUD:Filtering] Added support of options in index filtering modal

refacto/CRUDComponent
Sami Mokaddem 2023-12-13 14:30:53 +01:00
parent d823190624
commit cadb56eb07
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
2 changed files with 37 additions and 10 deletions

View File

@ -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;
});
}

View File

@ -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' => '<div class="col-sm-10">{{input}}</div>',
@ -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))