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

I would like to make a selection 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 the bean when the page reloads.

I do this internally, so I have a table with each row having a separate checkbox.

I am currently doing this in the bottom 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>

It all works fine, but I would like to change this ugly one <select>to<g:select>

+5
source share
2 answers
<g:select id="myselect" name="myselect" value="${myInst.color}"
          from="${['r': 'RED', 'g': 'GREEN', 'b': 'BLUE']}"
          optionKey="key" optionValue="value" />
+8
source

"myselect" . , . . g:

-1

All Articles