chg: [component:CRUD] Pass searched value to the view and allow searching by hitting <enter>

pull/37/head
mokaddem 2021-01-15 11:49:20 +01:00
parent 62426e0160
commit ba65c60838
3 changed files with 5 additions and 1 deletions

View File

@ -341,6 +341,7 @@ class CRUDComponent extends Component
{ {
$queryConditions = []; $queryConditions = [];
if (!empty($params['quickFilter']) && !empty($quickFilterFields)) { if (!empty($params['quickFilter']) && !empty($quickFilterFields)) {
$this->Controller->set('quickFilterValue', $params['quickFilter']);
foreach ($quickFilterFields as $filterField) { foreach ($quickFilterFields as $filterField) {
$likeCondition = false; $likeCondition = false;
if (is_array($filterField)) { if (is_array($filterField)) {
@ -352,6 +353,8 @@ class CRUDComponent extends Component
} }
} }
$query->where(['OR' => $queryConditions]); $query->where(['OR' => $queryConditions]);
} else {
$this->Controller->set('quickFilterValue', '');
} }
return $query; return $query;
} }

View File

@ -23,6 +23,7 @@ echo $this->element('genericElements/IndexTable/index_table', [
'button' => __('Filter'), 'button' => __('Filter'),
'placeholder' => __('Enter value to search'), 'placeholder' => __('Enter value to search'),
'data' => '', 'data' => '',
'value' => $quickFilterValue,
'searchKey' => 'value' 'searchKey' => 'value'
] ]
] ]

View File

@ -57,7 +57,7 @@
var url = '/' + controller + '/' + action + additionalUrlParams + '?quickFilter=' + encodeURIComponent($('#quickFilterField-<?= h($tableRandomValue) ?>').val()); var url = '/' + controller + '/' + action + additionalUrlParams + '?quickFilter=' + encodeURIComponent($('#quickFilterField-<?= h($tableRandomValue) ?>').val());
executePagination(randomValue, url); executePagination(randomValue, url);
}); });
$('#quickFilterField').on('keypress', function (e) { $('#quickFilterField-' + randomValue).on('keypress', function (e) {
if(e.which === 13) { if(e.which === 13) {
var url = '/' + controller + '/' + action + additionalUrlParams + '?quickFilter=' + encodeURIComponent($('#quickFilterField-<?= h($tableRandomValue) ?>').val()); var url = '/' + controller + '/' + action + additionalUrlParams + '?quickFilter=' + encodeURIComponent($('#quickFilterField-<?= h($tableRandomValue) ?>').val());
executePagination(randomValue, url); executePagination(randomValue, url);