Search code examples
grailsgroovygsp

GSP- Select tag. How to achive selected="selected"


I have a select tag in my GSP file as

<g:select name="clientId" id="clientId"   size = "4" from="${com.springcommunity.fleet.partymodel.roles.ClientRole.list()}" class = "filter_combo" optionKey="id" />

i want client with id 2 is selected initially (in simple html it is achived by using selected="selected")

how can i do it?


Solution

  • You need to specify the value attribute in this tag. http://grails.org/doc/2.0.x/ref/Tags/select.html

    So in your example,

    <g:select ... value="${com.springcommunity.fleet.partymodel.roles.ClientRole.get(2)}" />
    

    One thing to be aware of here is that the value that you're selecting must be an object equal to the item in the list, and not just an id - this is where a lot of people get tripped up. So you can't just say value='2', you need to specify the object in the list that you have in your from attribute.

    From the docs -

    value (optional) - The current selected value that evaluates equals() to true for one of the elements in the from list.