CMS Development

bagha07
Member

How to Get Filtered Data From HubDB from Multiple Select Column?

SOLVE
<div class="porfolio-services">
    <div class="website box" id="portfolio_search">
            {% set table = hubdb_table_rows(111111, queryparam) %} {% if table == [] %}
            {% else %}
          {% for row in table %}
            
                <div class="categories-bx {{ row["type"].name }}">
                    <div class="product-img">
                        <img src="{{ row["IMAGE"].url}}"> 
                    </div>                   
                    <div class="categories-hover">
                        <h5>{{ row.pname }}</h5>
                        <p>{{ row.description }}</p>
                        <a href="{{ row.portfolio_url }}" class="btn-morinfo">View Details</a></div>      
                </div>
  
            {% endfor %} 
          {% endif %}
    </div>
https://prnt.sc/mul2uq
0 Upvotes
1 Accepted solution
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

How to Get Filtered Data From HubDB from Multiple Select Column?

SOLVE

Hi @bagha07,

 

For querying by multi select columns, you can actually use the same filtering documented for the HubDB API here: https://developers.hubspot.com/docs/methods/hubdb/v2/get_table_rows.

 

Multi select fields in particular return a list, where each selection is an object containing information like the ID of the selection and the name (the text of it). So let's walk through some examples. Let's first assume that you have a multi select column named labeled "Colors" and whose name is "colors". Let's say it has selections of "red", "green", "blue".

 

First, you can print out what those multi selects look like, just to understand the structure you're working with. So you can print the multi selects or pretty print them like this:

 

{% set table = hubdb_table_rows(111111) %}
{% for row in table %}
  <p>row.colors: {{ row.colors }}</p>
  <p>row.colors: {{ row.colors|pprint }}</p>
  <br>
{% endfor %}

Since a multiselect column returns a list, you can loop through that as well with something like this:

 

{% set table = hubdb_table_rows(111111) %}
{% for row in table %}
  {% for color in row.colors %}
    <p>color: {{ color }}</p>
    <p>color.id: {{ color.id }}</p>
    <p>color.name: {{ color.name }}</p>
  {% endfor %}
  <br>
{% endfor %}

For the filtering, you can use different operators for returning only the rows that meet your filter criteria. For example if you want rows that are equal to only green, you can filter like this:

 

{% set table = hubdb_table_rows(111111, "&colors=green") %}
{% for row in table %}
  <p>row.name: {{ row.name }}</p>
  <p>row.colors: {{ row.colors }}</p>
{% endfor %}

If you want only rows that are equal to exactly red and green, you can filter like this:

 

{% set table = hubdb_table_rows(111111, "&colors=green,red") %}
{% for row in table %}
  <p>row.name: {{ row.name }}</p>
  <p>row.colors: {{ row.colors }}</p>
{% endfor %}

If you want rows that contain the selection red, you can filter like this:

 

{% set table = hubdb_table_rows(111111, "&colors__contains=green") %}
{% for row in table %}
	<p>row.name: {{ row.name }}</p>
  <p>row.colors: {{ row.colors }}</p>
  <br>
{% endfor %}

Let me know if you have any trouble with it or questions about the filtering. But I think those examples should get you pointed in the right direction.

 

- Leland

Leland Scanlan

HubSpot Developer Support

View solution in original post

1 Reply 1
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

How to Get Filtered Data From HubDB from Multiple Select Column?

SOLVE

Hi @bagha07,

 

For querying by multi select columns, you can actually use the same filtering documented for the HubDB API here: https://developers.hubspot.com/docs/methods/hubdb/v2/get_table_rows.

 

Multi select fields in particular return a list, where each selection is an object containing information like the ID of the selection and the name (the text of it). So let's walk through some examples. Let's first assume that you have a multi select column named labeled "Colors" and whose name is "colors". Let's say it has selections of "red", "green", "blue".

 

First, you can print out what those multi selects look like, just to understand the structure you're working with. So you can print the multi selects or pretty print them like this:

 

{% set table = hubdb_table_rows(111111) %}
{% for row in table %}
  <p>row.colors: {{ row.colors }}</p>
  <p>row.colors: {{ row.colors|pprint }}</p>
  <br>
{% endfor %}

Since a multiselect column returns a list, you can loop through that as well with something like this:

 

{% set table = hubdb_table_rows(111111) %}
{% for row in table %}
  {% for color in row.colors %}
    <p>color: {{ color }}</p>
    <p>color.id: {{ color.id }}</p>
    <p>color.name: {{ color.name }}</p>
  {% endfor %}
  <br>
{% endfor %}

For the filtering, you can use different operators for returning only the rows that meet your filter criteria. For example if you want rows that are equal to only green, you can filter like this:

 

{% set table = hubdb_table_rows(111111, "&colors=green") %}
{% for row in table %}
  <p>row.name: {{ row.name }}</p>
  <p>row.colors: {{ row.colors }}</p>
{% endfor %}

If you want only rows that are equal to exactly red and green, you can filter like this:

 

{% set table = hubdb_table_rows(111111, "&colors=green,red") %}
{% for row in table %}
  <p>row.name: {{ row.name }}</p>
  <p>row.colors: {{ row.colors }}</p>
{% endfor %}

If you want rows that contain the selection red, you can filter like this:

 

{% set table = hubdb_table_rows(111111, "&colors__contains=green") %}
{% for row in table %}
	<p>row.name: {{ row.name }}</p>
  <p>row.colors: {{ row.colors }}</p>
  <br>
{% endfor %}

Let me know if you have any trouble with it or questions about the filtering. But I think those examples should get you pointed in the right direction.

 

- Leland

Leland Scanlan

HubSpot Developer Support