CMS Development

cives
Contributor

HubDB Filtering With Params

I'm using the code below to display the options within a multi-select hubdb table; if I only want to show the options for rows of a certain type, how would I do that? 

 

Use case: on a page called Loss Prevention Forms, I only want to show rows that have form_type='loss prevention'. To take that one step further, I also want to show row.category in a sidebar filter (but only for the forms matching the previously stated criteria). I believe I'm close, but my code is not returning anything. See below. Thank you.

 

{% set table_info = hubdb_table_rows('2664478') %}

{% if row.form_type in table_info is containing 'Loss Prevention' %}
{% for choice in hubdb_table_column(2664478, 4).options %}
<div class="checkbox">
<input type="checkbox" value=".{{ choice.name|replace(' ','-')|lower }}"> <label class="checkbox-label">{{ choice.name }}</label>
</div>
{% endfor %}

{% endif %}

 

Screen Shot 2021-07-14 at 9.39.35 AM.png

0 Upvotes
4 Replies 4
webdew
Guide | Diamond Partner
Guide | Diamond Partner

HubDB Filtering With Params

Hi @cives ,

Add this code:

{% set types = hubdb_table_column(4689973, "sort_by").options %}

{% for choice in types %}
{% set type_list = type_list~choice.id|list%}
<li class="c-dropdown__item filter" data-filter="f-{{ choice.id }}" data-valfilter="{{ choice.name }}">{{ choice.name }}</li>
{% endfor %}


Hope this helps!


If we were able to answer your query, kindly help the community by marking it as a solution.

Thanks and Regards. 

Indra
Guide | Elite Partner
Guide | Elite Partner

HubDB Filtering With Params

Hi @cives

 

Did you check this tutorial on creating a lsiting with filters?

Let's Build A Real Estate Listing, using HubDB

 

The most inportant part will be the '.options'. For more info about it. Check HubDB column attributes:

{% set types = hubdb_table_column({Insert YOUR Table ID}, "type").options %}
{% for choice in types %}
  {% set type_list = type_list~choice.id|list%}
  {% if choice.id == request.query_dict.type%}
    <option selected="selected" value="{{ choice.id }}">{{ choice.name }}</option>
  {% else %}
  <option value="{{ choice.id }}">{{ choice.name }}</option>
  {% endif %}
{% endfor %}

 

By default Checkboxes aren't supported by the HubDB filter, but there is a workaround that has been solved at the following topic. So I would recommend to check that out.

Managing Multiple Filters in HubDB SOLVE


Bureau Vet

Did my post solve your question? Help the community by marking it as a solution
cives
Contributor

HubDB Filtering With Params

Thanks @dennisedson. I have it very close to functioning the way I need it to using:

 

{% set table_info = hubdb_table_rows(2664478, 'form_type__contains=Loss Prevention') %}
{% for row in table_info %}
  {% set option = row.category %}
      {% for choice in option %}
	<div class="checkbox">
	<input type="checkbox" value=".{{ choice.name|replace(' ','-')|lower }}"> <label class="checkbox-label">{{ choice.name }}</label>
	</div>
                   {% endfor %}
                       {% endfor %}

 My only challenge now is eliminating duplicates, example, the left column in the screenshot above now looks like:

Screen Shot 2021-07-14 at 5.36.36 PM.png

 Any ideas on eliminating the duplicates??

 

Thank you!

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

HubDB Filtering With Params

@cives 

I would consider using some filtering on your initial query to the HubDB

Something like this

{% set table_info = hubdb_table_rows('2664478', "form_type__contains=Loss Prevention") %}

{% for choice in table_info %}
<div class="checkbox">
<input type="checkbox" value=".{{ choice.name|replace(' ','-')|lower }}"> <label class="checkbox-label">{{ choice.name }}</label>
</div>
{% endfor %}

The Contains filter will get all of your multiselects that contain  Loss Prevention.  Rather than using Equals

 

@amwilie and @Anton are the HubDB wizards.  They may disagree with my approach 😉

Thanks,

Dennis




HubSpot Community Developer ShowMake sure to subscribe to our YouTube channel
where you can find the HubSpot Community Developer Show