0

Currently I have hardcoded the picklist values in visualforce.I need to dynamically generate picklist values based on current year.If current year is 2014 then I need to display 2016,2015,2014,2013,2012,2011.If current year is 2015 then I need to display 2017,2016,2015,2014,2013,2012.

I need to dynamically display the values based on current year.

 <th  scope="row"><label>Last 5 Years ending in: </label><select name="selFiscalYearEnd" onchange="drawChart()" id="selFiscalYearEnd">
                <option value="2011|2012|2013|2014|2015|and Fiscal_Year__c >='2011' and Fiscal_Year__c <='2015' ">2015</option>
                <option value="2010|2011|2012|2013|2014|and Fiscal_Year__c >='2010' and Fiscal_Year__c <='2014' ">2014</option>
                <option selected="true" value="2009|2010|2011|2012|2013|and Fiscal_Year__c >='2009' and Fiscal_Year__c <='2013' ">2013</option>
                <option value="2008|2009|2010|2011|2012|and Fiscal_Year__c >='2008' and Fiscal_Year__c <='2012' ">2012</option>
                <option value="2007|2008|2009|2010|2011|and Fiscal_Year__c >='2007' and Fiscal_Year__c <='2011' ">2011</option>
                <option value="2006|2007|2008|2009|2010|and Fiscal_Year__c >='2006' and Fiscal_Year__c <='2010' ">2010</option>
            </select></th>

1 Answer 1

5

I think something like this could work.

Controller:

public class TestSelectController{
    public List<SelectOption> getYears() {
        List<SelectOption> options = new List<SelectOption>();
        for (Integer i = System.Today().year() - 3; i < System.Today().year() + 3; i++) {
            options.add(new SelectOption(String.valueOf(i), String.valueOf(i)));
        }
        return options;
    }

    public String selectedYear {get; set;}

    public TestSelectController() {
        selectedYear = String.valueOf(System.Today().year());
    }
}

Page:

<apex:page controller="TestSelectController">
    <apex:form>
        <apex:selectList value="{!selectedYear}" size="1">
            <apex:selectOptions value="{!Years}"/>
        </apex:selectList>
    </apex:form>
</apex:page>
2
  • Thanks @Daniel Hoechst.But how do I set the current year as default value of the picklist
    – sfdc
    Jan 8, 2014 at 5:27
  • 2
    Easy! Just update the constructor. I've updated my answer. Jan 8, 2014 at 5:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .