Hello
I originally asked a question about single item filtering here :- Call to a member function title() on string, which worked perfectly.
The client has now asked for two field filtering and my idea of using alpine.js for filtering will no longer work.
I am struggling to find/implement a solution, what I have so far is :-
fields:
eventCategories:
label: Categories that events can be filtered by.
type: tags
sort: asc
eventMonths:
label: Months that events can be filtered by.
type: tags
sort: asc
events:
type: structure
label: Upcoming Events
sortBy: startdate desc
fields:
category:
label: Type of Event
type: select
sort: asc
options:
type: query
query: page.eventCategories.split
month:
label: Month Event Takes Place
type: select
options:
type: query
query: page.eventMonths.split
My filter form is :-
<form id="filters" class="filter--block" method="GET">
<div>
<p class="is--meta">Choose a type of Event:</p>
<select name="event" onchange="this.form.submit()">
<option value="">-</option>
<?php foreach($page->eventCategories()->split() as $item): ?>
<?php if(!$item) continue ?>
<option<?php e(isset($data['catFilter']) && $data['catFilter'] == $item, ' selected') ?> value="<?= Str::lower($item) ?>"><?= html($item) ?></option>
<?php endforeach ?>
</select>
</div>
<div>
<p class="is--meta">Select a Month</p>
<select name="month" onchange="this.form.submit()">
<option value="">-</option>
<?php foreach($page->eventMonths()->split() as $item): ?>
<?php if(!$item) continue ?>
<option<?php e(isset($data['monthFilter']) && $data['monthFilter'] == $item, ' selected') ?> value="<?= Str::lower($item) ?>"><?= html($item) ?></option>
<?php endforeach ?>
</select>
</div>
</form>
And my controller is :-
<?php
return function($page) {
$unfiltered = collection('events');
$catFilter = param('category');
$monthFilter = param('month');
$eventCategories = $unfiltered->pluck('eventCategories', ',', true);
$eventMonths = $unfiltered->pluck('eventMonths', ',', true);
// filter conditionally
$unfiltered = $unfiltered
->when($catFilter, function ($catFilter) {
return $this->filterBy('eventCategories', $catFilter);
})
->when($monthFilter, function ($monthFilter) {
return $this->filterBy('eventMonths', $monthFilter);
});
return [
'catFilter' => $catFilter,
'monthFilter' => $monthFilter,
'eventCategories' => $eventCategories,
'eventMonths' => $eventMonths,
'events'
];
};
When I attempt to use the select my URL changes but only one select is available at a time ie.
http://localhost:8888/events?event=knitting&month=
or
http://localhost:8888/events?event=&month=april
And when I look at the source code, no option in the select is marked as selected.
Could you offer some guidance/ a solution please?