这个
SelectOption
value
和
label
参数。所以你们可以在一个月内有更人性化的标签,但仍然可以把它作为一个数字传递给Apex。
这是一个相当复杂的演示。看见
edit history
对于早期版本(&P);解释一些技巧。
<apex:page controller="StackOverflow45934022">
<apex:form >
<fieldset>
<legend>Single dropdown</legend>
<apex:selectList value="{!monthAndYear}" multiselect="false" size="1">
<apex:selectOptions value="{!monthAndYearOptions}" />
<apex:actionSupport action="{!submitSingle}" event="onchange" reRender="results" />
</apex:selectList>
</fieldset>
<fieldset>
<legend>Two dropdowns</legend>
<apex:selectList value="{!year}" multiselect="false" size="1">
<apex:selectOptions value="{!yearOptions}" />
</apex:selectList>
<apex:selectList value="{!month}" multiselect="false" size="1">
<apex:selectOption itemValue="1" itemLabel="January"/> <!-- You can use itemLabel="{!$Label.PD_General_Month_Jan}" -->
<apex:selectOption itemValue="2" itemLabel="February"/>
<apex:selectOption itemValue="3" itemLabel="March"/>
<apex:selectOption itemValue="11" itemLabel="November"/>
<apex:selectOption itemValue="12" itemLabel="December"/>
</apex:selectList>
<apex:commandButton value="Submit" action="{!submitMultiple}" reRender="results" />
</fieldset>
</apex:form>
<hr/>
<apex:outputPanel id="results">
Month (as integer) : {!m}<br/>
Year : {!y}
</apex:outputPanel>
</apex:page>
public with sharing class StackOverflow45934022{
public Integer m {get;private set;}
public Integer y {get; private set;}
public StackOverflow45934022(){
m = System.today().month();
y = System.today().year();
}
// Single dropdown version start
public String monthAndYear {get;set;}
public List<SelectOption> getMonthAndYearOptions(){
List<SelectOption> ret = new List<SelectOption>();
Date thisMonth = System.today().toStartOfMonth(), januaryLastYear = Date.newInstance(System.today().year() -1, 1, 1);
DateTime d = DateTime.newInstance(thisMonth, Time.newInstance(0,0,0,0));
while(d >= januaryLastYear){
ret.add(new SelectOption(d.format('MM-YYYY'), d.format('MMMM YYYY'))); // you will use your map with month names to build labels
d = d.addMonths(-1);
}
return ret;
}
public void submitSingle(){
if(String.isNotBlank(monthAndYear) && monthAndYear.contains('-')){
List<String> temp = monthAndYear.split('-');
m = Integer.valueOf(temp[0]);
y = Integer.valueOf(temp[1]);
}
}
// Single dropdown version end
public String year {get;set;}
public String month {get; set;}
public List<SelectOption> getYearOptions(){
Date today = System.today();
String y0 = String.valueOf(today.year()), y1 = String.valueOf(today.year() -1);
return new List<SelectOption>{
new SelectOption(y0, y0),
new SelectOption(y1, y1)
};
}
public void submitMultiple(){
m = Integer.valueOf(month);
y = Integer.valueOf(year);
}
}