question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using multiple filters without erasing the first

See original GitHub issue

I 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:closed
  • Created 10 months ago
  • Comments:15

github_iconTop GitHub Comments

1reaction
shifumacommented, Nov 26, 2022

Nice…this is definitely on the right track now, thank you!

0reactions
bencrokercommented, Nov 25, 2022

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.

{% set categories = craft.entries.section('categories').id(categorySelect).all() %}
{% if search %}
    {% set searchCategories = craft.entries.section('categories').id(['not']|merge(categorySelect)).search(search).all() %}
    {% set categories = categories|merge(searchCategories) %}
{% endif %}

{% for category in categories %}
    <input type="checkbox" sprig s-replace="#articles" name="categorySelect[]" value="{{ category.id }}" {{ category.id in categorySelect ? 'checked' }}>{{ category.title }}
{% endfor %}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found