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.

Use category filtering AND load more

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
bencrokercommented, Aug 27, 2021

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.

{{ sprig.trigger == 'load-more-oob' ? 's-swap-oob="true"' }}

I’ve updated the recipe at https://putyourlightson.com/sprig/cookbook#load-more-complex to reflect this.

0reactions
bencrokercommented, Aug 26, 2021

Sure, email it to info@putyourlightson.com

Read more comments on GitHub >

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

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