I have a <g:select>
in one of my GSPs that looks like this:
<g:select id="location" name="criteria.location" from="${example.Location.list()}" optionKey="id" required="" value="1" class="many-to-one"/>
The expected result would be something like this, right?
Expected result:
<select id="location" name="criteria.location" required="" class="many-to-one" >
<option value="1" selected="selected" >1st Location Name</option>
<option value="2" >2nd Location Name</option>
<option value="3" >3rd Location Name</option>
<!-- entries omitted -->
<option value="49" >49th Location Name</option>
<option value="50" >50th Location Name</option>
</select>
But what do I get? I end up with two selected items. The 49th option gets selected as well, and I cannot begin to imagine why! There is absolutely no mention of the value "49" in my code...
Actual result:
<select id="location" name="criteria.location" required="" class="many-to-one" >
<option value="1" selected="selected" >1st Location Name</option>
<option value="2" >2nd Location Name</option>
<option value="3" >3rd Location Name</option>
<!-- entries omitted -->
<option value="49" selected="selected" >49th Location Name</option>
<option value="50" >50th Location Name</option>
</select>
If I change the value
in my <g:select>
to "3" for example, the 3rd option gets selected, but for whatever reason the 49th item still gets selected as well.
Edit: Even more bizarre, if I change the value
property to a value greater than 49, the select works and only the nth option is selected!
Update: I posted an answer detailing how I got it working, but I still do not understand the original behaviour of the <g:select>
tag.
Does anyone have any idea as to what is going on here?
Grails version: 2.2.0
Use value attribute for it
value="${location ?: 49}"
e.g.
<g:select id="location" name="criteria.location" from="${example.Location.list()}" optionKey="id" required="" value="${location ?: 49}" class="many-to-one"/>