开发者

set current year in select box in freemarker

I have a select box in my freemarker page 开发者_开发知识库in which year are coming from data database

<select id = "years" name = "years">
    <#list getYears as year>
        <option value = "${year.years}">${year.years}</option>
    </#list>
</select>

Suppose value are coming 2009,2010,2011,2012,2013 but I want the select value should be 2011 ie. the current year how can I do that?


Try this:

<select id="years" name="years">
    <#list years as year>
        <option value="${year?c}"<#if (year == .now?string("yyyy"))> selected="selected"</#if>>${year?c}</option>
    </#list>
</select>

I assume the years variable is a collection of the possible years.


Try something like

<option value="${years.year}" <#if years.year == actualyear>selected</#if>/>

with setting actualyear somewhere in your sourcecode.


The more updated (since Freemarker 2.3.23) way to do it would be with then ?then operand

<select id = "years" name = "years">
    <#list getYears as year>
        <option value = "${year.years}" ${(year.years == thisyear)?then('selected', '')}>${year.years}</option>
    </#list>
</select>

As another answer stated you would have to assign thisyear earlier in the code:

<#assign thisyear .now?string.yyyy />
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜