Skip to content

Commit d7bfcbb

Browse files
committed
Filtering of from_to
1 parent 1860e5d commit d7bfcbb

10 files changed

Lines changed: 93 additions & 48 deletions

File tree

Component/Grid/GridViewModel.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,9 @@ private function getGridFilterStateValues(): array
359359
$values = [];
360360
foreach ($this->getGridFilters() as $gridFilter) {
361361
$gridFilterState = $this->getState()->getFilterState($gridFilter->getCode());
362-
$value = $gridFilterState instanceof FilterState ? $gridFilterState->getValue() : null;
363-
364-
// @todo: This needs to be moved elsewhere, perhaps default value of Filter class?
365-
if ($gridFilter->getConditionType() === 'from_to' && $value === null) {
366-
$value = [
367-
'from' => null,
368-
'to' => null
369-
];
370-
}
371-
362+
$value = $gridFilterState instanceof FilterState
363+
? $gridFilterState->getValue()
364+
: $gridFilter->getDefaultValue();
372365
$values[$gridFilter->getCode()] = $value;
373366
}
374367

Grid/Action/FilterAction.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,34 @@ public function __construct(
1515

1616
public function execute(GridRepository $gridRepository, array $value): void
1717
{
18-
if (!isset($value['filter'])) {
18+
if (!isset($value['filters'])) {
1919
return;
2020
}
2121

22-
if (!is_array($value['filter'])) {
22+
if (!is_array($value['filters'])) {
2323
return;
2424
}
2525

2626
/** @var GridViewModel $gridViewModel */
2727
$gridViewModel = $gridRepository->getComponent()->getViewModel();
2828
$gridFilters = $gridViewModel->getGridFilters();
2929

30-
if (false === array_key_exists($value['filter']['name'], $gridFilters)) {
31-
return;
30+
foreach ($value['filters'] as $filterName => $filterValue) {
31+
if (false === array_key_exists($filterName, $gridFilters)) {
32+
continue;
33+
}
34+
35+
$gridFilter = $gridFilters[$filterName];
36+
if ($gridFilter->isEmpty($filterValue)) {
37+
continue;
38+
}
39+
40+
$state = $this->stateManager->get($gridRepository->getNamespace());
41+
$state->setFilter(
42+
$filterName,
43+
$filterValue,
44+
$gridFilter->getConditionType(),
45+
);
3246
}
33-
34-
$gridFilter = $gridFilters[$value['filter']['name']];
35-
36-
$state = $this->stateManager->get($gridRepository->getNamespace());
37-
$state->setFilter(
38-
$gridFilter->getCode(),
39-
(string)$value['filter']['value'],
40-
$gridFilter->getConditionType(),
41-
);
4247
}
4348
}

Grid/Filter/Filter.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,30 @@ private function getFieldType(): string
7575

7676
return 'input';
7777
}
78+
79+
public function getDefaultValue(): mixed
80+
{
81+
if ($this->getConditionType() === 'from_to') {
82+
return [
83+
'from' => null,
84+
'to' => null,
85+
];
86+
}
87+
88+
return null;
89+
}
90+
91+
public function isEmpty(mixed $value): bool
92+
{
93+
if ($this->getConditionType() === 'from_to'
94+
&& is_array($value)
95+
&& isset($value['from'])
96+
&& isset($value['to'])
97+
&& empty($value['from'])
98+
&& !empty($value['to'])) {
99+
return true;
100+
}
101+
102+
return $value === null;
103+
}
78104
}

Grid/Filter/FilterInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ public function getCode(): string;
1212

1313
public function getConditionType(): string;
1414
public function render(): string;
15+
16+
public function getDefaultValue(): mixed;
17+
18+
public function isEmpty(mixed $value): mixed;
1519
}

Grid/State.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,22 +172,17 @@ public function getFilterState(string $name): ?FilterState
172172
public function setFilter(string $name, mixed $value, ?string $conditionType = 'like'): void
173173
{
174174
$filters = $this->getFilters();
175-
print_r($filters);
176175

177176
if (isset($filters[$name])) {
178177
unset($filters[$name]);
179178
}
180179

181-
print_r($filters);
182-
183180
$filters[$name] = [
184181
'field' => $name,
185182
'value' => $value,
186183
'condition_type' => $conditionType,
187184
];
188185

189-
print_r($filters);
190-
191186
$this->setFilters($filters);
192187
}
193188

Grid/State/FilterState.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ public function getValue(): string|array|null
2222
return $this->value;
2323
}
2424

25+
public function renderValue(): string
26+
{
27+
if (is_string($this->value)) {
28+
return $this->value;
29+
}
30+
31+
if (is_array($this->value)) {
32+
return implode(',', $this->value);
33+
}
34+
35+
return '';
36+
}
37+
2538
public function getConditionType(): string
2639
{
2740
return $this->conditionType;

Grid/State/FilterStateFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function __construct(
1313

1414
public function create(
1515
string $field,
16-
string $value,
16+
mixed $value,
1717
string $conditionType = '',
1818
): FilterState {
1919
return $this->objectManager->create(FilterState::class, [

view/adminhtml/templates/form/field_type/from_to.phtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ $fieldType = $field->getFieldType();
3131
id="<?= $escaper->escapeHtml($field->getScope()) ?>.<?= $escaper->escapeHtml($field->getCode()) ?>"
3232
class="admin__control-text"
3333
type="text"
34-
data-name="<?= $escaper->escapeHtml($field->getCode()) ?>"
34+
data-name="<?= $escaper->escapeHtml($field->getCode()) ?>.from"
3535
maxlength="<?= $escaper->escapeHtml(
3636
$fieldType->getMaximumLength()
3737
) ?>"
@@ -53,7 +53,7 @@ $fieldType = $field->getFieldType();
5353
<input
5454
class="admin__control-text"
5555
type="text"
56-
data-name="<?= $escaper->escapeHtml($field->getCode()) ?>"
56+
data-name="<?= $escaper->escapeHtml($field->getCode()) ?>.to"
5757
maxlength="<?= $escaper->escapeHtml(
5858
$fieldType->getMaximumLength()
5959
) ?>"

view/adminhtml/templates/grid/filters.phtml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if (empty($gridFilters)) {
2323
?>
2424
<div class="data-grid-filters-actions-wrap">
2525
<div class="data-grid-filters-action-wrap">
26-
<button :class="filterButtonClass" class="action-default" @click.prevent="toggleFilters">
26+
<button :class="filterButtonClass" class="action-default" @click="toggleFilters">
2727
Filters
2828
</button>
2929
</div>
@@ -42,7 +42,7 @@ if (empty($gridFilters)) {
4242
<li>
4343
<span class="label"><?= $escaper->escapeHtml($gridFilterState->getField()) ?></span>
4444
<span class="condition-type"><?= $escaper->escapeHtml($gridFilterState->getConditionType()) ?></span>
45-
<span class="preview"><?= $escaper->escapeHtml($gridFilterState->getValue()) ?></span>
45+
<span class="preview"><?= $escaper->escapeHtml($gridFilterState->renderValue()) ?></span>
4646

4747
<button class="action-remove" type="button" @click="removeFilter" data-field="<?= $gridFilterState->getField() ?>">
4848
<span><?= __('Remove') ?></span>
@@ -63,13 +63,22 @@ if (empty($gridFilters)) {
6363
<?php endforeach; ?>
6464
</fieldset>
6565

66-
<?php if (!empty($gridFilterStates)): ?>
6766
<div class="admin__data-grid-filters-footer">
6867
<div class="admin__footer-main-actions">
68+
<?php if (!empty($gridFilterStates)): ?>
6969
<button class="action-tertiary" type="button" @click="clearFilters">
7070
<span><?= __('Clear all') ?></span>
7171
</button>
72+
<?php endif; ?>
73+
74+
<button class="action-tertiary" type="button" data-action="grid-filter-cancel" @click="toggleFilters">
75+
<span><?= __('Cancel') ?></span>
76+
</button>
77+
78+
<button class="action-secondary" type="button" data-action="grid-filter-apply" @click="applyFilters">
79+
<span><?= __('Apply Filters') ?></span>
80+
</button>
7281
</div>
7382
</div>
74-
<?php endif; ?>
7583
</div>
84+

view/adminhtml/templates/script/component-type/grid-component-type.phtml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ use Magento\Framework\View\Element\Template;
2222
toggleOtherActions() {
2323
this.showOtherActions = !this.showOtherActions;
2424
},
25-
setGridFilter(event) {
26-
const value = event.target.value;
27-
const name = event.target.name;
28-
this.gridFilters[name] = value;
29-
30-
this.post({
31-
filter: {
32-
name: name,
33-
value: value,
34-
}
35-
});
25+
setGridFilter() {
26+
const value = this.$el.value;
27+
const nameParts = this.$el.getAttribute('data-name').split('.');
28+
if (nameParts.length > 1) {
29+
this.gridFilters[nameParts[0]][nameParts[1]] = value;
30+
} else {
31+
this.gridFilters[nameParts[0]] = value;
32+
}
33+
},
34+
applyFilters() {
35+
this.post({filters: this.gridFilters});
3636
},
37-
clearFilters() {
37+
clearFilters() { // @todo: Rename to clearGridFilters
3838
this.post({
3939
clearFilters: true
4040
});
4141
},
42-
removeFilter() {
42+
removeFilter() { // @todo: Rename to removeGridFilter
4343
const targetFilter = this.$el.getAttribute('data-field');
4444
if (!targetFilter) {
4545
return;

0 commit comments

Comments
 (0)