Hello!
I was having trouble finding an answer to this.
I have a HubDB table with a few columns. One of these columns is a multiselect and another is a select column.
I am looping through on the front-end to show the options in the multiselect and select columns, but I only want those output if there is a row entry that has been assigned to one of those items.
Example, in my select column, I have
- Blog Posts
- Case Studies
- ROI Calculators
- Survey Results
On the front-end, it’s showing all 4 of those options, but I only have row items assigned to 3 of them. Here is my output code
{% set tab2 = hubdb_table_column(6934042, "type") %}{% if tab2 %}<ul class="u3m-filtered-content__multi-filter__ul fs_industries_type"><li><a data-filter="all">All Content Types</a></li>{% for cat2 in tab2.options %}<li><a data-filter=".{{ cat2.name|lower|replace(' ','-')|replace('/','-')|replace('&','-')|replace(',','-') }}">{{ cat2.name }}</a></li> {% endfor %}</ul>
How can I only show active ones?
Thanks!
Hi, @marenhogan
Thanks for your question. Your clear description and code examples are very helpful for our community.
Hey, @alyssamwilie @Indra @Bryantworks, have you worked on anything similar in your recent projects?
Thank you for taking a look! — Jaycee
Hey @marenhogan
One way would be instead of accessing the options of the columns directly, to filter out all used values in the rows of the hubdb:
{# your hubdb table id #}
{% set table_id = 6934042 %}
{# name of the (multi)select column #}
{% set multiselect_column_name = "type" %}
{# get all hubdb rows #}
{% set table = hubdb_table_rows(table_id) %}
{# remove rows that have no value and select the column #}
{% set table_filtered_multiselect = table|selectattr(multiselect_column_name)|map(multiselect_column_name) %}
{# get all the used options from the rows #}
{% set used_multiselect_options = [] %}
{% for item in table_filtered_multiselect %}
{% for subitem in item %}
{% do used_multiselect_options.append(subitem) %}
{% endfor %}
{% endfor %}
{# remove duplicates and sort the options #}
{% set used_multiselect_options = used_multiselect_options|unique|sort(False, False, "order") %}
{{ used_multiselect_options|pprint }}