Related Categories Filters
See original GitHub issueQuestion
This question is about the same project here: https://github.com/putyourlightson/craft-sprig/issues/71
I added in live search and that worked however once I started adding in filtering via dropdown related categories things got wonky.
I have 2 category group dropdowns to filter by geography and experience I was able to get filtering for one category group to work, but that also breaks the option to switch between grid view and list view - detailed in issue linked above along with pagination that was previously resolved.
In my entry query I have the following:
{% set entryQuery = craft.entries
.section('sustainingMemoriesAuthors')
.orderBy('lastName asc')
.limit(limit)
.relatedTo(experience)
.search(search) %}
{# .relatedTo([ 'and', { targetElement: experience }, { targetElement: geography } ]) #}
which filters by the experience category however If I change anything else in the filters or search I end up with zero results. Also switching from grid to list view does this as well. and if I change the relatedTo
parameter to the one in the comment there are no results at all
Full template code below:
{# set some variables #}
{# set variable for sprig to hook into with default of grid for first page load #}
{% set gridListView = gridListView ?? "grid" %}
{% set page = page ?? 1 %}
{# set search parameter here #}
{% set search = q ?? '' %}
{# set default values for the category dropdowns #}
{% set geography = geography ?? '' %}
{% set experience = experience ?? '' %}
{#
// need above values in hidden inputs for persistent state
// https://github.com/putyourlightson/craft-sprig/issues/71#issuecomment-745388432
#}
{{ hiddenInput('page', page) }}
{{ hiddenInput('gridListView', gridListView) }}
{{ hiddenInput('geography', geography) }}
{{ hiddenInput('experience', experience) }}
{# search #}
<section class="search teal" s-replace="#survivors">
<div class="container">
<div class="row">
<div class="col-sm-12">
<form action="{{ url('search/results') }}" class="search-form">
<input
sprig
s-trigger="keyup changed delay:300ms"
type="search"
name="q"
aria-label="{{ "Search by name"|t }}"
placeholder="{{ "Search by name"|t }}"
autocomplete="off"
value="{{ search }}">
<button type="submit" class="sr-only">{{ "Search"|t }}</button>
<div class="row filtering">
<div class="col-sm-9">
<div class="fieldset">
<span>{{ "Filter By:"|t }}</span>
<div class="form-group">
<label for="geography" class="sr-only">{{ "Geography"|t }}</label>
<select
sprig
class="form-control"
id="geography"
name="geography">
<option {{ geography =='' ? 'selected'}} value="all">{{ "Geography"|t }}</option>
{% for option in craft.categories.group('sustainingGeography').all() %}
<option
value="{{ option.id }}"
{{ option.id == option.slug ? 'selected'}}>{{ option.title }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="experience" class="sr-only">{{ "Experience"|t }}</label>
<select
sprig
class="form-control"
id="experience"
name="experience">
<option value="all" {{ experience =='' ? 'selected'}}>{{ "Experience"|t }}</option>
{% for option in craft.categories.group('sustainingExperience').all() %}
<option value="{{ option.id }}">{{ option.title }}</option>
{% endfor %}
</select>
</div>
</div><!-- /.fieldset -->
</div><!-- /.col-sm-9 -->
<div class="col-sm-3 grid-list-choose">
{# s-val name is same as variable gridViewList further down but here needs to be kebab case #}
{# Sprig to switch layouts #}
<a href="" sprig s-val:grid-list-view="grid" class="{{ gridListView == 'grid' ? 'active': ''}}">
{{ svg('@webroot/assets/images/grid.svg')|attr({class:''}) }}
{{ "Grid View"|t }}
</a>
<span>|</span>
{# s-push-url="?gridListView=list" #}
<a href="" sprig s-val:grid-list-view="list" class="{{ gridListView == 'list' ? 'active': ''}}">
{{ svg('@webroot/assets/images/list.svg')|attr({class:''}) }}
{{ "list View"|t }}
</a>
</div><!-- /.col-sm-3 -->
</div><!-- /.row -->
</form>
</div><!-- /.col-sm-12 -->
</div><!-- /.row -->
</div><!-- /.container -->
</section><!-- /.search -->
{# end search #}
<section class="survivors" id="survivors">
<div class="container">
<div class="row">
{# Sets a default value if not defined by `s-val:*` on the clicked element #}
{% set page = page ?? 1 %}
{% set entryQuery = craft.entries
.section('sustainingMemoriesAuthors')
.orderBy('lastName asc')
.limit(limit)
.relatedTo(experience)
.search(search) %}
{# .relatedTo([ 'and', { targetElement: experience }, { targetElement: geography } ]) #}
{# Paginates the entry query given the current page #}
{% set pageInfo = sprig.paginate(entryQuery, page) %}
{% set entries = pageInfo.pageResults %}
{% if gridListView =="grid" %}
{# grid view #}
{% include 'exhibits/sustaining-memories/_includes/grid-view' %}
{% else %}
{# list view #}
{% include 'exhibits/sustaining-memories/_includes/list-view' %}
{% endif %}
</div><!-- /.row -->
{# pagination #}
<div class="row">
<div class="col-sm-12">
{% if entries %}
<p>
{{ "Page"|t }}
{% for i in 1..pageInfo.totalPages %}
{% if i == page %}
{{ i }}
{% else %}
{# Refreshes the component and pushes the new value into the URL #}
<a href="#" sprig s-val:page="{{ i }}">{{ i }}</a> {# s-push-url="?page={{ i }}" #}
{% endif %}
{% endfor %}
</p>
{% endif %}
{% do sprig.pushUrl('?' ~ {page: page, gridListView: gridListView}|url_encode) %}
</div><!-- /.col-sm-12 -->
</div><!-- /.row -->
{# #}
</div><!-- /.container -->
</section><!-- /.survivors -->
please advise.
Additional context
Add any other context or screenshots about the support request here.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11
Top GitHub Comments
@j-greig
sure - here’s the full current template - code is ready to go live, just waiting on final approval.
@CreateSean Huge thanks for sharing your code, was struggling with categories, you helped me out enormously. Cheers.