How would you limit the number of choices in select element

SOLVE
AminWejebe
Participant

Hello!

 

I'm building a module but got stuck, I have a hubdb with a column of the type select and this column have 5 choices and I would like to have a select filter in my custom module but I would only wish to display 3 of those 5 choices. How would you go about doing that? Thanks in advance 

0 Upvotes
1 Accepted solution

Accepted Solutions
piersg
Solution
Key Advisor

Hi @AminWejebe,

 

Do you want to display 3 specific choices, or any 3 (i.e. the first 3). If any will do, you could add an if statement in your for loop like:

{% set choices = hubdb_table_column(table_id, "choices").options %}
<select id="myFilter">
  <option selected="selected" value="all">Choose an option</option>
  {% for item in options %}
    {% if loop.index <= 3 %}
    <option value="{{item}}">{{item}}</option>
    {% endif %}
  {% endfor %}
</select>

 

But I could be more helpful if you copy/paste your code, as the above is assuming you're pulling the choices directly from the HubDB.

View solution in original post

1 Reply 1
piersg
Solution
Key Advisor

Hi @AminWejebe,

 

Do you want to display 3 specific choices, or any 3 (i.e. the first 3). If any will do, you could add an if statement in your for loop like:

{% set choices = hubdb_table_column(table_id, "choices").options %}
<select id="myFilter">
  <option selected="selected" value="all">Choose an option</option>
  {% for item in options %}
    {% if loop.index <= 3 %}
    <option value="{{item}}">{{item}}</option>
    {% endif %}
  {% endfor %}
</select>

 

But I could be more helpful if you copy/paste your code, as the above is assuming you're pulling the choices directly from the HubDB.

View solution in original post