I’ve got an Image gallery built with HubDB.
There are 2 dropdown filters (Application, State)
They are currently working together so the url becomes something like this when the filters are selected:
mysite.com/gallery-3?application=1&state=9
I’d like the user to only be able to use one filter at a time. So if a state is selected, the application resets to “show all” or “application=1” is removed from the url.
Does anyone know how I could code that?
Here is the current code:
<div style="margin: 0 0 60px 0;">
<form id="form_id" method="get">
<div>
<div class="drop-half">
<h5>FILTER BY APPLICATION TYPE: </h5>
<select name="application" form="form_id" onChange="this.form.submit()">
<option value="show-all">Show All</option>
{% set applications = hubdb_table_column(XXXXXX, "Application").options %}
{% for choice in applications %}
{% set application_list = application_list~choice.id|list%}
{% if choice.id == request.query_dict.application%}
<option selected="selected" value="{{ choice.id }}">{{ choice.name }}</option>
{% else %}
<option value="{{ choice.id }}">{{ choice.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="drop-half">
<h5>FILTER BY STATE: </h5>
<select name="state" form="form_id" onChange="this.form.submit()">
<option value="show-all">Show All</option>
{% set states = hubdb_table_column(XXXXXX, "State").options %}
{% for choice in states %}
{% set state_list = state_list~choice.id|list%}
{% if choice.id == request.query_dict.state%}
<option selected="selected" value="{{ choice.id }}">{{ choice.name }}</option>
{% else %}
<option value="{{ choice.id }}">{{ choice.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="clear"></div>
</div>
</form>
</div>
{% set applicationquery = "" %}
{% if request.query_dict.application in ["1", "2", "3", "4", "5", "6", "7"] %}
{% set applicationquery = applicationquery ~ "&application__contains="~request.query_dict.application|urlencode %}
{% endif %}
{% if request.query_dict.application == "show-all" %}
{% set applicationquery = applicationquery %}
{% endif %}
{% set statequery = "" %}
{% if request.query_dict.state in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50"] %}
{% set statequery = statequery ~ "&state="~request.query_dict.state|urlencode %}
{% endif %}
{% if request.query_dict.state == "show-all" %}
{% set statequery = statequery %}
{% endif %}
<div class="row-fluid">
{% set table = hubdb_table_rows(XXXXXX, applicationquery + statequery) %}
{% if table == [] %}
<p class='align-center'>Sorry, no listings found for that Search. Try changing your filter and search again.</p>
{% else %}
<div class="light-gal-wrap">
{% for row in table %}
<figure>
<a href="{{ row.image.url }}" data-fancybox="images">
<div class="thumb-hold" style="background: url({{ row.image.url }});"></div>
</a>
<figcaption>
<strong>{{ row.image_description }}</strong>
<div class="location">{{ row.city }}, {{ row.state.name }} </div>
<div class="button-wrap">
{% if row.profile_url %}
<a class="prof-but" href="{{ row.profile_url }}" target="_blank">View Project Profile</a>
{% endif %}
</div>
</figcaption>
</figure>
{% endfor %}
</div>
{% endif %}
</div>
Any help is greatly appreciated!