Using multiple filters without erasing the first
See original GitHub issueI have two Sprig filters in the one component; first searching for entries, and checkboxes to then filter other entries related to the the selected entries. It’s tough to explain so hopefully my code does it:
{% set search = search ?? '' %}
{% set categorySelect = categorySelect ?? [] %}
{% set categories = craft.entries({section: 'categories', search: search}) %}
{% set articlesQuery = craft.entries({section: 'articles', limit: 20}) %}
{% if categorySelect is not empty %}
{% do articlesQuery.relatedTo(categorySelect) %}
{% endif %}
{% set articles = articlesQuery.all() %}
<input sprig s-trigger="keyup changed delay:200ms" s-replace="#categories" type="text" name="search" value="{{ search }}">
<div id="categories">
{% if search %}
{% for category in categories %}
<input type="checkbox" sprig s-replace="#articles" name="categorySelect[]" value="{{ category.id }}" {{ category.id in categorySelect ? 'selected' }}>{{ category.title }}
{% endfor %}
{% endif %}
</div>
<div id="articles">
{% for article in articles %}
{{ article.title }}
{% endfor %}
</div>
This is working pretty well, with one large exception: I search once, check a category and the entries are filtered. I then want to check another category, but when I search again it clears the entire thing, and categorySelect
is empty again.
Is there a way to have categorySelect
persist when search
changes?
Issue Analytics
- State:
- Created 10 months ago
- Comments:15
Top Results From Across the Web
How To Use More Than One Include Filter (without Losing Data)
Google officially recommends not to use more than one include filter, because it can lead (rather unintuitively) to excluding all the data ...
Read more >Clear or remove a filter - Microsoft Support
Remove filters from columns so you can view more or all of your data. ... For example, the figure below depicts an example...
Read more >Apply Multiple Filters on a Pivot Field
... you can use filters to narrow your focus. See how to apply one filter, or apply multiple filters on a pivot field,...
Read more >Excel Filter: How to add, use and remove - Ablebits
When you edit or delete data in filtered cells, Excel AutoFilter does not update automatically to reflect the changes. To re-apply the filter, ......
Read more >Use filters in Adobe Photoshop
You can also delete applied filters by selecting the filter and clicking the Delete Layer icon . Note: To save time when trying...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Nice…this is definitely on the right track now, thank you!
You can do that, you just need to rewrite the part of the component that outputs the checkboxes. Right now, you’re only outputting category checkboxes based directly on search results. Here’s some new code that should get you started.