Answers.Body: <p>You can use <a href="https://putyourlightson.com/plugins/sprig#s-replace" rel="nofollow noreferrer">s-replace</a> to specify the element to be replaced.</p>
  <pre><code>{# Increments `offset` by the value of `limit` and swaps itself out on click #}
  &lt;div 
      id=&quot;load-more&quot; class=&quot;button small-button&quot;
      sprig 
      s-vals=&quot;{{ { 
          offset : offset + limit, 
          filter : filter 
      }|json_encode }}&quot; 
      s-replace=&quot;#load-more&quot;
  &gt;show more&lt;/div&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-23T17:37:58.733Z
Answers.LastActivityDate: 2023-04-23T17:37:58.733Z
Answers.UserDisplayName: Ben Croker
Body: <p>I'm trying to create a page where I can view entries in a list and have the option where I can view all entries at the click of a button, or choose to load more as and when I want.</p>
  <p>With the view all button, everything works perfectly because it replaces the whole component that I want to swap out and I have a twig function that stops the load more and view all button from showing in my component if 'limit' is set to 'view-all' (generally limit is set to an integer).</p>
  <p>My problem is, using the show more button, because the s-target is set to 'this' like in the cookbook, It loads another view all button whereas in reality what I want it to do is either just keep one view all button, or remove that button completely if I'm not showing all entries.</p>
  <p>Does anybody know of a way that I can achieve this? I've been trying for a couple of hours now and I keep finding that I run into obstacles.</p>
  <p>Below I've included all the code that I've got for both the component and the load more buttons which I'm pulling in through an include because theoretically these buttons are going to be used elsewhere.</p>
  <p>Many thanks</p>
  <p><strong>Main component for listing/ setting variables</strong></p>
  <pre><code>{# 
      sprig variables 
  #}
  
  {# sets the filter and type parameter to either be what is in the url, or nothing #}
  {% set filter = filter ?? '' %}
  
  {% if reset is defined %}
      {% set filter = '' %}
  {% endif %}
  
  {# tells the component how many items to show #}
  {% set offset = offset ?? 0 %}
  {% set limit = limit != 'view-all' ? limit %}
  
  {# will ensure that filters are dynamically added to site #}
  {% set filterOptions = craft.categories.group('hubCategories').all() %} 
  
  {# For security only filters that are within filterOtions/ filterTypes can be used. Prevents people updating query params and finding hidden content #}
  {% set allowedFilters = [] %}
  
  {% for option in filterOptions %}
      {% set allowedFilters = allowedFilters|merge([ option.slug ]) %}
  {% endfor %}
  
  {% set entryQuery = craft.entries({
      section: 'hubArticles',
      orderBy: ('postDate desc'),
      offset: offset,
      limit: limit,
  }) %}
  
  {# if filter is empty then double check the query string to make sure that there's nothing there. If there is, filter == the query string value. This is for copying the url and sharing/ finding the search again later. #}
  {% if filter is empty and craft.app.request.getQueryParam('filter')|length %}
      {% if craft.app.request.getQueryParam('filter') != 'all' %}
          {% set filter = craft.app.request.getQueryParam('filter') %}
      {% endif %}
  {% endif %}
  
  {# check to make sure that first of all, the query exists and second of all that it's permitted to get content for #}
  {% if filter is not empty and craft.app.request.getQueryParam('filter') != 'all' and filter in allowedFilters %}
      {% do entryQuery.andRelatedTo(craft.categories.slug(filter)) %}
  {% endif %}
  
  {# check whether or not the filter == all or '' to show the active filter #}
  {% set allArray = [
      'all',
      ''
  ] %}
  
  {# send completed query #}
  {% set entries = entryQuery.all() %}
  
  &lt;section s-replace=&quot;#hub-listing&quot; id=&quot;sprig-listing-container&quot; class=&quot;sprig-listing section&quot;&gt;
      &lt;div class=&quot;container xtra-small&quot;&gt;
      
          &lt;div class=&quot;listing-navigation&quot;&gt;
              &lt;ul class=&quot;wrapped-listing-container center-text&quot;&gt;
  
                  {# all button #}
                  &lt;li class=&quot;listing-item listing-all upper-text xtra-small-text text-blue {{ filter in allArray ? 'active' }}&quot; sprig s-vals=&quot;{{ { filter : 'all' }|json_encode }}&quot; s-push-url=&quot;?filter=all&quot; id=&quot;all&quot; name=&quot;all&quot;&gt;All&lt;/li&gt;
  
                  {% for cat in filterOptions %}
  
                      &lt;li class=&quot;listing-item upper-text xtra-small-text text-blue {{ filter == cat.slug ? 'active' }}&quot; sprig s-vals=&quot;{{ { filter : cat.slug }|json_encode }}&quot; s-push-url=&quot;?filter={{ cat.slug }}&quot; id=&quot;{{ cat.slug }}&quot; name=&quot;{{ cat.slug }}&quot;&gt;{{ cat.title }}&lt;/li&gt;
  
                  {% endfor %}
  
              &lt;/ul&gt;
          &lt;/div&gt;
  
      &lt;/div&gt;
  
      &lt;div class=&quot;container&quot; id=&quot;hub-listing&quot;&gt;
  
          &lt;div class=&quot;listing-wrapper&quot; id=&quot;listing-wrapper&quot;&gt;
              {% if entries|length %}
  
                  {% for entry in entries %}
  
                      &lt;a class=&quot;hub-card&quot; href=&quot;{{ entry.url }} aria-label={{ entry.title }}&quot;&gt;
                          &lt;div class=&quot;hub-image&quot;&gt;
                              &lt;picture&gt;
                                  &lt;img src=&quot;{{ entry.listingImage.one.url()|default(&quot;#{ siteUrl }assets/images/default-image0#{ random(1, 6) }.jpg&quot;) }}&quot; alt=&quot;{{ entry.title }}&quot; height=&quot;auto&quot; width=&quot;auto&quot; loading=&quot;lazy&quot; /&gt;
                              &lt;/picture&gt;
                          &lt;/div&gt;
  
                          &lt;div class=&quot;text-content&quot;&gt;
                              &lt;div class=&quot;hub-categories&quot;&gt;
                                  {% for cat in entry.hubCategories.all() %}
                                      &lt;p class=&quot;hub-category text-light-blue upper-text xtra-small-text&quot;&gt;{{ cat.title }}{{ not loop.last ? ', ' }}&lt;/p&gt;
                                  {% endfor %}
                              &lt;/div&gt;
  
                              &lt;div class=&quot;text-wrapper&quot;&gt;
                                  &lt;h4 class=&quot;hub-title text-blue&quot;&gt;{{ entry.title }}&lt;/h4&gt;
  
                                  {% if entry.listingDescription %}
                                      &lt;p class=&quot;small-text text-blue&quot;&gt;{{ entry.listingDescription }}&lt;/p&gt;
                                  {% endif %}
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/a&gt;
                  
                  {% endfor %}
  
              {% else %}
                  &lt;h4 class=&quot;text-blue upper-text&quot;&gt;Sorry, we can't find any results right now. Please try another search option.&lt;/h4&gt;
              {% endif %}
          &lt;/div&gt;
  
          {% if entryQuery.count() &gt; offset + entries|length and limit != 'view-all' %}
              {% include &quot;_sprig/_load-more-view-all&quot; %}
          {% endif %}
  
      &lt;/div&gt;
  &lt;/section&gt;
  
  {% js %}
  
      $('#sprig-listing-container .listing-item').click(() =&gt; {
          if ($(this).hasClass('active')) {
              return;
          }
  
          $('.listing-item.active').removeClass('active');
          $(this).addClass('active');
      });
  
  {% endjs %}
  </code></pre>
  <p><strong>load more component</strong></p>
  <pre><code>{# Increments `offset` by the value of `limit` and swaps itself out on click #}
  &lt;div 
      class=&quot;button small-button&quot;
      sprig 
      s-vals=&quot;{{ { 
          offset : offset + limit, 
          filter : filter 
      }|json_encode }}&quot; 
      s-target=&quot;this&quot; 
      s-swap=&quot;outerHTML&quot;
  &gt;show more&lt;/div&gt;
  
  {# view all results button #}
  &lt;div 
      class=&quot;button small-button blue-transparent&quot; 
      sprig 
      s-vals=&quot;{{ { 
          offset : 0, 
          limit : 'view-all', 
          filter : filter 
      }|json_encode }}&quot;
      s-target=&quot;#hub-listing&quot; 
      s-swap=&quot;outerHTML&quot;
  &gt;View All&lt;/div&gt;
  </code></pre>
  
Tags: <plugin-sprig>
Title: Sprig - Use a view all button in conjunction with a load more
ThreadText: # Sprig - Use a view all button in conjunction with a load more
  
  (Asked by: username_0 on 2023-04-21)
  
  I'm trying to create a page where I can view entries in a list and have the option where I can view all entries at the click of a button, or choose to load more as and when I want.
  
  With the view all button, everything works perfectly because it replaces the whole component that I want to swap out and I have a twig function that stops the load more and view all button from showing in my component if 'limit' is set to 'view-all' (generally limit is set to an integer).
  
  My problem is, using the show more button, because the s-target is set to 'this' like in the cookbook, It loads another view all button whereas in reality what I want it to do is either just keep one view all button, or remove that button completely if I'm not showing all entries.
  
  Does anybody know of a way that I can achieve this? I've been trying for a couple of hours now and I keep finding that I run into obstacles.
  
  Below I've included all the code that I've got for both the component and the load more buttons which I'm pulling in through an include because theoretically these buttons are going to be used elsewhere.
  
  Many thanks
  
  **Main component for listing/ setting variables**
  
  ```
  {# 
      sprig variables 
  #}
  
  {# sets the filter and type parameter to either be what is in the url, or nothing #}
  {% set filter = filter ?? '' %}
  
  {% if reset is defined %}
      {% set filter = '' %}
  {% endif %}
  
  {# tells the component how many items to show #}
  {% set offset = offset ?? 0 %}
  {% set limit = limit != 'view-all' ? limit %}
  
  {# will ensure that filters are dynamically added to site #}
  {% set filterOptions = craft.categories.group('hubCategories').all() %} 
  
  {# For security only filters that are within filterOtions/ filterTypes can be used. Prevents people updating query params and finding hidden content #}
  {% set allowedFilters = [] %}
  
  {% for option in filterOptions %}
      {% set allowedFilters = allowedFilters|merge([ option.slug ]) %}
  {% endfor %}
  
  {% set entryQuery = craft.entries({
      section: 'hubArticles',
      orderBy: ('postDate desc'),
      offset: offset,
      limit: limit,
  }) %}
  
  {# if filter is empty then double check the query string to make sure that there's nothing there. If there is, filter == the query string value. This is for copying the url and sharing/ finding the search again later. #}
  {% if filter is empty and craft.app.request.getQueryParam('filter')|length %}
      {% if craft.app.request.getQueryParam('filter') != 'all' %}
          {% set filter = craft.app.request.getQueryParam('filter') %}
      {% endif %}
  {% endif %}
  
  {# check to make sure that first of all, the query exists and second of all that it's permitted to get content for #}
  {% if filter is not empty and craft.app.request.getQueryParam('filter') != 'all' and filter in allowedFilters %}
      {% do entryQuery.andRelatedTo(craft.categories.slug(filter)) %}
  {% endif %}
  
  {# check whether or not the filter == all or '' to show the active filter #}
  {% set allArray = [
      'all',
      ''
  ] %}
  
  {# send completed query #}
  {% set entries = entryQuery.all() %}
  
  <section s-replace="#hub-listing" id="sprig-listing-container" class="sprig-listing section">
      <div class="container xtra-small">
      
          <div class="listing-navigation">
              <ul class="wrapped-listing-container center-text">
  
                  {# all button #}
                  <li class="listing-item listing-all upper-text xtra-small-text text-blue {{ filter in allArray ? 'active' }}" sprig s-vals="{{ { filter : 'all' }|json_encode }}" s-push-url="?filter=all" id="all" name="all">All</li>
  
                  {% for cat in filterOptions %}
  
                      <li class="listing-item upper-text xtra-small-text text-blue {{ filter == cat.slug ? 'active' }}" sprig s-vals="{{ { filter : cat.slug }|json_encode }}" s-push-url="?filter={{ cat.slug }}" id="{{ cat.slug }}" name="{{ cat.slug }}">{{ cat.title }}</li>
  
                  {% endfor %}
  
              </ul>
          </div>
  
      </div>
  
      <div class="container" id="hub-listing">
  
          <div class="listing-wrapper" id="listing-wrapper">
              {% if entries|length %}
  
                  {% for entry in entries %}
  
                      <a class="hub-card" href="{{ entry.url }} aria-label={{ entry.title }}">
                          <div class="hub-image">
                              <picture>
                                  <img src="{{ entry.listingImage.one.url()|default("#{ siteUrl }assets/images/default-image0#{ random(1, 6) }.jpg") }}" alt="{{ entry.title }}" height="auto" width="auto" loading="lazy" />
                              </picture>
                          </div>
  
                          <div class="text-content">
                              <div class="hub-categories">
                                  {% for cat in entry.hubCategories.all() %}
                                      <p class="hub-category text-light-blue upper-text xtra-small-text">{{ cat.title }}{{ not loop.last ? ', ' }}</p>
                                  {% endfor %}
                              </div>
  
                              <div class="text-wrapper">
                                  <h4 class="hub-title text-blue">{{ entry.title }}</h4>
  
                                  {% if entry.listingDescription %}
                                      <p class="small-text text-blue">{{ entry.listingDescription }}</p>
                                  {% endif %}
                              </div>
                          </div>
                      </a>
                  
                  {% endfor %}
  
              {% else %}
                  <h4 class="text-blue upper-text">Sorry, we can't find any results right now. Please try another search option.</h4>
              {% endif %}
          </div>
  
          {% if entryQuery.count() > offset + entries|length and limit != 'view-all' %}
              {% include "_sprig/_load-more-view-all" %}
          {% endif %}
  
      </div>
  </section>
  
  {% js %}
  
      $('#sprig-listing-container .listing-item').click(() => {
          if ($(this).hasClass('active')) {
              return;
          }
  
          $('.listing-item.active').removeClass('active');
          $(this).addClass('active');
      });
  
  {% endjs %}
  
  ```
  
  **load more component**
  
  ```
  {# Increments `offset` by the value of `limit` and swaps itself out on click #}
  <div 
      class="button small-button"
      sprig 
      s-vals="{{ { 
          offset : offset + limit, 
          filter : filter 
      }|json_encode }}" 
      s-target="this" 
      s-swap="outerHTML"
  >show more</div>
  
  {# view all results button #}
  <div 
      class="button small-button blue-transparent" 
      sprig 
      s-vals="{{ { 
          offset : 0, 
          limit : 'view-all', 
          filter : filter 
      }|json_encode }}"
      s-target="#hub-listing" 
      s-swap="outerHTML"
  >View All</div>
  
  ```
  
  **Tags:** plugin-sprig
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-04-23. Score: 1
  
  You can use [s-replace](https://putyourlightson.com/plugins/sprig#s-replace) to specify the element to be replaced.
  
  ```
  {# Increments `offset` by the value of `limit` and swaps itself out on click #}
  <div 
      id="load-more" class="button small-button"
      sprig 
      s-vals="{{ { 
          offset : offset + limit, 
          filter : filter 
      }|json_encode }}" 
      s-replace="#load-more"
  >show more</div>
  
  ```
  
  
  ---
  
  