Aug 4, 2022 2:06 PM
Hello there! I want to be able to print out my multi select field called 'state' (it was single prior, I'll change it later lol). My code says 'Cannot Resolve Property: Name' when I try to extract the value from my HubDB.
I was able to extract it successfully when I had a single select dropdown, but I need it for multi-select now.
Here is my complete code:
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">
{% set int_state_ID = 1 %}
<!-- set the filter -->
<div>
<form id="form_id" method="get">
<div>
<h2>FILTER BY STATE: </h2>
<select name="state" form="form_id" onChange="this.form.submit()">
<option value="show-all">Show All</option>
{% set states = hubdb_table_column(5440613, "state").options %}
{% for choice in states %}
{% set state_list = state_list~choice.id|list%}
{% if choice.id == request.query_dict.state%}
<option selected="selected" value="{{ choice.id }}">{{ choice.name }}</option>
{% else %}
<option value="{{ choice.id }}">{{ choice.name }}</option>
{% endif %}
{% endfor %}
</select>
<input name="name" type="text" id="search-by" class="autocomplete" placeholder="Search by name...">
</div>
{# <div>
<input name="name" type="text" id="search-by" class="autocomplete" placeholder="Search by name...">
</div> #}
<div class="button button-decor" input id="submit-button" type="submit" value="Search">Search</div>
{# <div class="button button-decor"><input type="button" onclick="location.href='http://21908587.hs-sites.com/uson-local-support';" value="Show All" /></div>
<div class="button button-decor"><input type="button" onclick="location.href='http://21908587.hs-sites.com/uson-local-support-0';" value="View International Consultants" /></div> #}
<div class="button button-decor" onclick="location.href='http://21908587.hs-sites.com/uson-local-support';">Show All</div>
<div class="button button-decor-2" onclick="location.href='http://21908587.hs-sites.com/uson-local-support-0';">View International Consultants</div>
</form>
</div>
<!-- sets the different query parameters using submitted input for hubdb query -->
{% set queryparam = "" %}
{% if request.query_dict.state in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50"] and request.query_dict.name == "" %}
{% set queryparam = queryparam ~ "&state=__contains"~request.query_dict.state|urlencode %}
{% endif %}
{% if request.query_dict.state in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50"] and request.query_dict.name != "" %}
{% set queryparam = queryparam~"&state="~request.query_dict.state|urlencode~"&zip__icontains="~request.query_dict.zip|urlencode %}
{% endif %}
{% if request.query_dict.state == "show-all" and request.query_dict.name != "" %}
{% endif %}
{% set table = hubdb_table_rows(5440613, queryparam) %}
<!-- Check if table exists, set sort by state -->
{% 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|sort(False, False, 'state') %}
<div class="consultant-listing">
<h2>{{ row["name"] }}</h2>
<div class="information">
{% for row in table %}
{% for state in row.state %}
<div class="fac_state">{{ row.state.name|pprint }} </div>
{% endfor %}
{% endfor %}
<div class="fac_city"">{{ row["city"] }}, {{ row["zip"] }}</div>
<i class="fas fa-phone"></i> {{ row["phone"] }}
<br> <i class="fas fa-link"></i> <a href="{{row["website"]}}">Website</a>
<br><i class="fas fa-envelope"></i> <a href="mailto:{{ row["email"] }}">{{ row["email"] }}</a>
<div class="location_details">{{ row["location_details"] }}</div>
</div>
</div>
{% endfor %}
{% endif %}</div>
row.state.name is printing out null, so there is no value.
When I put down {{row.state}} it renders:
Solved! Go to Solution.
Aug 4, 2022 2:56 PM
Hi @unicorndev
{{row.state}} is an array and your code looks like this:
{% for row in table %}
{% for state in row.state %}
<div class="fac_state">{{ row.state.name|pprint }} </div>
{% endfor %}
What is the output if you do this instead:
{% for row in table %}
{% for state in row.state %}
<div class="fac_state">{{state.name|pprint }}
</div>
{% endfor %}
Aug 4, 2022 2:56 PM
Hi @unicorndev
{{row.state}} is an array and your code looks like this:
{% for row in table %}
{% for state in row.state %}
<div class="fac_state">{{ row.state.name|pprint }} </div>
{% endfor %}
What is the output if you do this instead:
{% for row in table %}
{% for state in row.state %}
<div class="fac_state">{{state.name|pprint }}
</div>
{% endfor %}
Aug 4, 2022 4:38 PM
And sorry one more question... since it has multiple states, I want to add a comma between each state with display: inline so it is all on one line. Any way I can do that?
They're just block elements right now 🙂
Aug 5, 2022 3:54 AM
Aug 4, 2022 4:23 PM
Thanks! I took out the forloop going through each row in the table because it was looping and printing out all states in every company name, even for fields it didn't belong to lol. I appreciate that so much, thank you.