开发者

how to make select tag to have by default current year and current month?

开发者 https://www.devze.com 2023-03-08 10:49 出处:网络
i have 2 select tags as, <td><g:select name=\"Year\" from=\"${2004..2014}\"optionValue=\"${Year}\" /></td>

i have 2 select tags as,

 <td><g:select name="Year" from="${2004..2014}"  optionValue="${Year}" /></td>
  <td><g:select name="Month" from="${['January','February','March','April','May','Jun','July','August','September','October','November','December']}"  optionValue="${month}"  /></td>

Now i want in gsp it should display me the current year and month in select box of year and month instea开发者_开发技巧d of displaying first year and month in list ..

how to achieve it?..can any one answer me please.

advance thanks Laxmi


Try this

<g:set var="months" value="${new java.text.DateFormatSymbols().months}"/>
<g:set var="today" value="${new Date()}"/>

<td><g:select name="Year" from="${2004..2014}" value="${today[Calendar.YEAR]}" /></td>

<td><g:select name="Month" from="${months as List}" 
    value="${months[today[Calendar.MONTH]]}"  /></td>


In the controller create the date:

def year = new Date().format("yyyy")
def month = new Date().format("MM")
[year:year, month:month]

Should give you the current month and year.

0

精彩评论

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