CMS Development

AminWejebe
Participant

How would you limit the number of choices in select element

Résolue

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 Votes
1 Solution acceptée
piersg
Solution
Conseiller clé

How would you limit the number of choices in select element

Résolue

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.

Voir la solution dans l'envoi d'origine

1 Réponse
piersg
Solution
Conseiller clé

How would you limit the number of choices in select element

Résolue

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.