Deepak Kumar
Deepak Kumar

Reputation: 3751

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?

Upvotes: 3

Views: 7340

Answers (3)

ScottM
ScottM

Reputation: 660

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 />

Upvotes: 1

Laurent Pireyn
Laurent Pireyn

Reputation: 6875

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.

Upvotes: 5

Dirk
Dirk

Reputation: 1903

Try something like

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

with setting actualyear somewhere in your sourcecode.

Upvotes: 1

Related Questions