CMS Development

hayleyh
Participant

Need help looping through HubDB rows and columns

SOLVE

Hi, I am trying to loop through columns, and then through rows coming from a HubDB table. Per the Hubspot docs, it seems like I should be able to set the current row and current column ID I'm in in each of my loops to call <current row>.<current column name>, but that is not working for some reason. My code is below:

 

{% set comp_table = hubdb_table(2686696) %}
{% set comp_table_rows = hubdb_table_rows(2686696) %}

<div class="bdt-table" id="bdt-table">
    <div class="container" id="spec-container">
        <div class="row">
            <div class="col-xs-12">
                <table>
                    {% for column in comp_table.columns %}
                        {% set current_col = column.name %}
                        <tr>
                            <td class="table-spec-name"> {{ column.label }} </td>
                            {% for row in hubdb_table_rows(2686696) %}
                                <td class="table-spec-name"> {{ row.current_col }} </td>
                            {% endfor %}
                        </tr>
                    {% endfor %}
                </table>
            </div>
        </div>
    </div>
</div>
    

 

I really appreciate any help I can get with this, I have been trying to make this work for awhile and I'm really not getting anywhere. Thank you!

0 Upvotes
1 Accepted solution
BarryGrennan
Solution
Top Contributor

Need help looping through HubDB rows and columns

SOLVE

Hi Hayley, 

 

This took me ages of messing around with it 😂 but I think I've figured it out.

 

It seems that you can't call your variable "current_col" by putting "row.current_col", instead you need to put "row|attr(current_col)"

 

That's not actually in the HubDB documentation from what I could see but I decided to play around with the hubl and found that this worked.

 

So the code I got to work looks like:

 

 

{% set comp_table = hubdb_table(2686696) %}
{% set comp_table_rows = hubdb_table_rows(2686696) %}

<div class="bdt-table" id="bdt-table">
<div class="container" id="spec-container">
<div class="row">
<div class="col-xs-12">
<table>
{% for column in comp_table.columns %}
{% set current_col = column.name %}
<tr>
<td class="table-spec-name"> {{ column.label }} </td>
{% for row in comp_table_rows %}
<td class="table-spec-name"> {{ row|attr(current_col) }} </td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
</div>

 

  

 

Hope that works for you!

View solution in original post

3 Replies 3
BarryGrennan
Solution
Top Contributor

Need help looping through HubDB rows and columns

SOLVE

Hi Hayley, 

 

This took me ages of messing around with it 😂 but I think I've figured it out.

 

It seems that you can't call your variable "current_col" by putting "row.current_col", instead you need to put "row|attr(current_col)"

 

That's not actually in the HubDB documentation from what I could see but I decided to play around with the hubl and found that this worked.

 

So the code I got to work looks like:

 

 

{% set comp_table = hubdb_table(2686696) %}
{% set comp_table_rows = hubdb_table_rows(2686696) %}

<div class="bdt-table" id="bdt-table">
<div class="container" id="spec-container">
<div class="row">
<div class="col-xs-12">
<table>
{% for column in comp_table.columns %}
{% set current_col = column.name %}
<tr>
<td class="table-spec-name"> {{ column.label }} </td>
{% for row in comp_table_rows %}
<td class="table-spec-name"> {{ row|attr(current_col) }} </td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
</div>

 

  

 

Hope that works for you!

hayleyh
Participant

Need help looping through HubDB rows and columns

SOLVE

Wow thank you so much, that works!!! I appreciate you taking the time!

0 Upvotes
BarryGrennan
Top Contributor

Need help looping through HubDB rows and columns

SOLVE

Awesome! Glad to hear it worked for you. It's an interesting problem.

0 Upvotes