CMS Development

jestlinbaum
Participant

HubDB Looping through filtered rows

SOLVE

I've started a HubDB template based on How To Build a Real Estate Listing.

I've got the filter to work from a multi select column. All of that is working perfectly.

My question is : after the dropdown filter is applied, how can I filter those results to get different sections of results?

 

So if "category 1" is selected from the dropdown filter. (and category 1 has some rows where the "color" column has a value of "red", and some rows where the "color" column has a value of "green.")

 

How can I loop through the "Category 1" results and print a headline for each color and then the rows for each color.

 

ie.

 

<h2>Red</h2>

{all the rows where the color column = red}

 

<h2>Green</h2>

{all the rows where the color column = green}

 

Here is the current code.

 

 

<!-- set the filter by drop down -->

<div>
<form id="form_id" method="get">
   
    <div>
        <h4>FILTER BY BUILDING TYPE: </h4>
        <select name="type" form="form_id" onChange="this.form.submit()">
            <option value="show-all">Show All</option>
            {% set types = hubdb_table_column(1040830, "building_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 %}
          
          {{ choice.name }}
        </select>
    </div>
    
</form>
</div>
  
<!-- sets the different query parameters using submitted input for hubdb query -->
{% set queryparam = "" %}
{% if request.query_dict.type in ["1", "2", "3", "4", "5", "6", "7", "8", "9"] %}
    {% set queryparam = queryparam ~ "&building_type__contains="~request.query_dict.type|urlencode %}
  
  
{% endif %}
{% if request.query_dict.type == "show-all" %}
    {% set queryparam = queryparam %}
{% endif %}

  
  
<!-- The Table -->  
{% set table = hubdb_table_rows(1040830, queryparam) %}

{% if table == [] %}
    <p class='align-center'>Sorry, no listings found for that Search. Try changing your filter and search again.</p>
{% else %}

{% for row in table %}
<div style="text-align:left; border-bottom: 1px solid #ccc; padding: 20px;">
   {{ row.itemname }}<br>
   <div style="font-size:10px;">
     Cat: {{ row.category }}<br>
     Sub: {{ row.subcategory }}<br>
     {{ row.building_type }}
   </div>
</div>
  {% endfor %}
  {% endif %}

 

0 Upvotes
1 Accepted solution
jennysowyrda
Solution
Community Manager
Community Manager

HubDB Looping through filtered rows

SOLVE

Hi @jestlinbaum,

 

There are a few other threads on the Community that discuss this tutorial as well. I wanted to link them for everyone's reference: 

1. Filter and search field alignment

2. HubDB and custom modules

3. Filter functionality from HS tutorial

4. Real estate listing Hubdb template

 

@Jon_McLaren@EricSalvi@marvelouspatric@rnoon-d22@Hawk-Steve do you have any thoughts or suggestions for @jestlinbaum?

 

Thanks,
Jenny

View solution in original post

0 Upvotes
2 Replies 2
jennysowyrda
Solution
Community Manager
Community Manager

HubDB Looping through filtered rows

SOLVE

Hi @jestlinbaum,

 

There are a few other threads on the Community that discuss this tutorial as well. I wanted to link them for everyone's reference: 

1. Filter and search field alignment

2. HubDB and custom modules

3. Filter functionality from HS tutorial

4. Real estate listing Hubdb template

 

@Jon_McLaren@EricSalvi@marvelouspatric@rnoon-d22@Hawk-Steve do you have any thoughts or suggestions for @jestlinbaum?

 

Thanks,
Jenny

0 Upvotes
jestlinbaum
Participant

HubDB Looping through filtered rows

SOLVE

@jennysowyrda 

 

Thank you so much for the reply. I was able to get what I wanted by setting different parameters after the query:

 

{% set blah_blah = table|selectattr('catid','equalto','cat_name') %}

I repeated the code for each category (14 categories)

 

So if there is an easier way to "loop through" and do it without manually using the same code over and over based on category, i'd love to hear it. If not, feel free to close the thread and thanks for taking the time.