Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a select box with constant list items with g:select

Tags:

grails

groovy

I would like to make a select box using <g:select/> that translates to this html:

<select id="myselect" name="myselect">
  <option value="r">RED</option>
  <option value="g">GREEN</option>
  <option value="b">BLUE</option>
</select>

I would also like the value to be preselected from a bean when the page reloads.

I'm doing this inside a so I have a table with each row having a separate option box.

I'm currently accomplishing this in the below html:

<g:each in=${mylist} status="i" var="myInst">
   <select id="status${myInst}" name="status${myInst}" data-id="${myInst.id}">
      <option value="r" <g:if test="${myInst.color == "r"}">selected</g:if>>RED</option>
      <option value="g" <g:if test="${myInst.color == "g"}">selected</g:if>>Green</option>
      <option value="b" <g:if test="${myInst.color == "b"}">selected</g:if>>BLUE</option>
   </select>
</g:each>

This all works fine but I'd like to change that ugly <select> into <g:select>

like image 573
Anthony Avatar asked Mar 08 '13 16:03

Anthony


Video Answer


1 Answers

<g:select id="myselect" name="myselect" value="${myInst.color}"
          from="${['r': 'RED', 'g': 'GREEN', 'b': 'BLUE']}"
          optionKey="key" optionValue="value" />
like image 88
Andrew Avatar answered Sep 19 '22 14:09

Andrew