Visualforce 如何将当前月份显示为文本并传递值?

Visualforce 如何将当前月份显示为文本并传递值?,visualforce,apex,Visualforce,Apex,我第一次尝试了第二种选择,没有考虑你的宝贵建议,但现在我把想法复杂化了。我曾尝试将值作为日期选择器传递,然后将其拆分,但这对我没有帮助。我尝试了下面的代码,我将向您解释我现在面临的挑战 //我创建了一个映射来存储您为不同语言指定的值 public map<integer,string> monthmaps(){ map<integer, string> monthMapStatic = new map<integer, string>();

我第一次尝试了第二种选择,没有考虑你的宝贵建议,但现在我把想法复杂化了。我曾尝试将值作为日期选择器传递,然后将其拆分,但这对我没有帮助。我尝试了下面的代码,我将向您解释我现在面临的挑战

//我创建了一个映射来存储您为不同语言指定的值

public map<integer,string> monthmaps(){

      map<integer, string> monthMapStatic = new map<integer, string>();
        monthMapStatic.put(1, Label.PD_General_Month_Jan);
        monthMapStatic.put(2, Label.PD_General_Month_Feb);
        monthMapStatic.put(3, Label.PD_General_Month_Mar);
        monthMapStatic.put(4, Label.PD_General_Month_Apr);
        monthMapStatic.put(5, Label.PD_General_Month_May);
        monthMapStatic.put(6, Label.PD_General_Month_Jun);
        monthMapStatic.put(7, Label.PD_General_Month_Jul);
        monthMapStatic.put(8, Label.PD_General_Month_Aug);
        monthMapStatic.put(9, Label.PD_General_Month_Sep);
        monthMapStatic.put(10, Label.PD_General_Month_Oct);
        monthMapStatic.put(11, Label.PD_General_Month_Nov);
        monthMapStatic.put(12, Label.PD_General_Month_Dec);
       return monthMapStatic;
}
问题:我不能给出一年,或者默认情况下我已经过去了一年,我需要过去的两年。因此,提出了两个下拉选项

VF页面:

<apex:selectList value="{!monthSelection}"
  multiselect="false" size="1"
   styleClass="form-control pull-left monthlyDropDown"
   style="height: 33px;">
    <apex:selectOptions value="{!monthList}" />
     </apex:selectList>
希望这次我说得清楚。

该函数接受
标签
参数。所以,您可以在一个月内获得更人性化的标签,但仍然可以将其作为数字传递给Apex

这是一个相当复杂的演示。有关某些技巧的早期版本和说明,请参见


单个下拉列表
两个下拉列表

月份(整数):{!m}
年份:{!y}
public与共享类StackOverflow45934022{
公共整数m{get;私有集;}
公共整数y{get;私有集;}
公共堆栈溢出45934022(){
m=系统今天()月();
y=系统。今天()年();
}
//单下拉版本启动
公共字符串monthAndYear{get;set;}
公共列表getMonthAndYearOptions(){
List ret=新列表();
Date thisMonth=System.today().toStartOfMonth(),Januarylasyear=Date.newInstance(System.today().year()-1,1);
DateTime d=DateTime.newInstance(这个月,Time.newInstance(0,0,0));
而(d>=一月){
ret.add(新建SelectOption(d.format('MM-YYYY')、d.format('MMMM-YYYY'));//您将使用带有月份名称的地图来构建标签
d=d.addMonths(-1);
}
返回ret;
}
公共无效提交表格(){
if(String.isNotBlank(monthAndYear)&&monthAndYear.contains('-')){
列表温度=monthhandyear.split('-');
m=整数值(温度[0]);
y=整数值(温度[1]);
}
}
//单下拉版本结束
公共字符串年份{get;set;}
公共字符串月份{get;set;}
公共列表getYearOptions(){
Date today=System.today();
字符串y0=String.valueOf(今日.year()),y1=String.valueOf(今日.year()-1);
返回新列表{
新选择选项(y0,y0),
新选择选项(y1,y1)
};
}
public void submitMultiple(){
m=整数。值(月);
y=整数。值of(年);
}
}

我考虑了眼霜的第一个答案,并提出了以下逻辑

DateTime d = System.now();
for(Integer i = 1; i < 13; ++i){
            String label = d.format('MMMM yyyy');
this.monthList.add(new SelectOption(String.valueOf(i), label));
            d = d.addMonths(-1);
        } 
现在,我的查询将获取year_sel和month_sel的值,并返回所选的year和month


谢谢你的逻辑。

tnks现在,如果我想有两个字段,一个用于几个月,另一个用于过去两年,我将如何分解它?我这样问是因为,我的SQL将数字10作为10月,年份是当前年份作为默认值,我希望有两个框,其中我有今年(2017年)的所有月份(在我写这篇文章时是8月),以及我选择2016年时的所有12个月。如果你能帮我,那就太好了。你给我们的想法太棒了,但是之前的SQL格式不允许这样做,我考虑的唯一选项是手动选择年份并传递值。谢谢你,伙计,我现在正在尝试这两个选项,我正在尝试你最初建议的一个。如果我错了,请纠正我,我将给出值={!monthlyselect},然后在控件端,我给出monthlyselect.year()和monthlyselect.month()。我从你身上学到了很多。感谢您对我这样的人的指导。您对第二个选项的选择完全正确,在折叠表中重新命名Doesen work,我想知道如何使用第一个选项传递值,请帮助。如何将选项构建为
new SelectOption(d.format('MM-yyyy')、d.format('MMMM-yyyy'),这将为您提供类似于“09-2017”的值(标签仍将作为月份名称)。然后,一旦选择了某个对象,就可以使用split函数剪切该字符串<代码>列表温度=monthAndYear.split('-');字符串月份=临时值[0];字符串年份=温度[1]。然后,它可以保持原样,也可以使用
Integer.valueOf(month)
或类似的东西进行强制转换。我尝试了第一个选项,但失败了,你能帮我在@eyescream中构建它吗?
<apex:selectList value="{!monthSelection}"
  multiselect="false" size="1"
   styleClass="form-control pull-left monthlyDropDown"
   style="height: 33px;">
    <apex:selectOptions value="{!monthList}" />
     </apex:selectList>
joinQueryMonth += 'AND ((mcc.processing_year__c=\''+ thisMonthDate.year() +'\' AND mcc.processing_month__c=' + thisMonthDate.month() + ') \n';
DateTime d = System.now();
for(Integer i = 1; i < 13; ++i){
            String label = d.format('MMMM yyyy');
this.monthList.add(new SelectOption(String.valueOf(i), label));
            d = d.addMonths(-1);
        } 
Integer month_Sel = date.today().month()-(Integer.valueOf(monthSelection)-1);
        Integer year_Sel = date.today().year();
        if(month_Sel <= 0){
            month_Sel = 12+month_Sel;
            year_Sel = year_Sel-1;
        }
Date thisMonthDate = date.newInstance(year_Sel, month_Sel, 1);
September 2017
August 2017
.
.
October 2016