Use category filtering AND load more
See original GitHub issueDescribe the bug
I have some checkboxes that filter entries by category (industry), and a load more button that is supposed to append additional entries to the query results. The issue is that the load more button disappears whenever a filter is applied, even if there are more entries.
Here’s my simplified sprig component:
{% set category = craft.categories.id(categoryId).one() %}
{% set industry = industry ?? '' %}
{% set offset = offset ?? 0 %}
{# This is a category page, so before any industry sorting happens, only templates related to the category should show #}
{% set relatedTo = ['and', categoryId] %}
{% set relatedTo = industry ? relatedTo|merge(industry) : categoryId %}
<div id="filters">
<p>Industry:</p>
<ul>
{% for indCat in craft.categories.group('templateCategory').all() %}
<li>
<input type="checkbox" id="{{ indCat.slug }}" value="{{ indCat.id }}" name="industry[]" {{ indCat.id in industry ? 'checked' }} sprig>
<label for="{{ indCat.slug }}">{{ indCat.title }}</label>
</li>
{% endfor %}
</ul>
</div>
<div id="entries">
{# limit = 6, sent with the sprig component include #}
{% set entryQuery = craft.entries
.section('proposalTemplates')
.limit(limit)
.offset(offset)
.relatedTo(relatedTo)
%}
{% set entries = entryQuery.all() %}
{% if entries|length %}
{% for entry in entries %}
<div class="entry">
{{ entry.title }}
</div>
{% endfor %}
{% endif %}
</div>
{% if entryQuery.count() > (offset + entries|length) %}
<button id="load-more-oob" sprig
s-val:offset="{{ offset + limit }}"
s-select="#entries .entry"
s-target="#entries"
s-swap="beforeend"
s-swap-oob="true">
Load More
</button>
{% else %}
<button id="load-more-oob" s-swap-oob="true" style="display: none"></button>
{% endif %}
Versions
- Plugin version: 1.7.0
- Craft version: Craft Pro 3.6.14
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
Sprig use category filtering AND load more
The problem is that it appends everything in the sprig component, including the category filters. _sprig/component : {% set category = craft.
Read more >Advanced Ajax Filtering | Examples | Ajax Load More
Ajax Load More will filter blog posts by category, tag and year using a variety of form element types and the Filters add-on....
Read more >Load more Button and Show Category Filter Using HTML CSS ...
Hello Guys In this tutorial you will be able to make showing data with category after that, We will Create Load more button...
Read more >How To Solve The Conflict Between The 'Load More' Feature ...
This occurs because the filter feature filters only through visible portfolio/blog items, and the load more feature loads the next, upcoming batch of...
Read more >AJAX filters for WordPress Posts with Load More Button
I decided to make this code simple and use only two dropdown selects – for posts per page and for ordering posts. If...
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
After replicating your setup locally I discovered the issue. If a checkbox is clicked then the entire component is re-rendered, but since the button still has a
s-swap-oob="true"
attribute set, it is being excluded. So the solution is to conditionally include that attribute.I’ve updated the recipe at https://putyourlightson.com/sprig/cookbook#load-more-complex to reflect this.
Sure, email it to info@putyourlightson.com