开发者

GSP g:select option default selection

开发者 https://www.devze.com 2022-12-09 16:53 出处:网络
Is there any possibility to select a option field by default 开发者_StackOverflow中文版in a g:select tag?

Is there any possibility to select a option field by default 开发者_StackOverflow中文版in a g:select tag?

I only saw the "noSelection" parameter in the documentation.

<g:select name="user.age" from="${18..65}" value="${age}"
      noSelection="['':'-Choose your age-']"/>

But I need a default selection from the data I received.

For example 18..65 is my range and I want to select 20 as default selection.

Is that possible or do I have to do this with javascript?

Thank you


The value attribute does exactly that. From the Grails documentation:

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

So, if you want to select "20" if your age model variable is null, just do

<g:select name="user.age" from="${18..65}" value="${age ?: 20}"
      noSelection="['':'-Choose your age-']"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号