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

How to display current month as text and pass the value?

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

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

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;
}

// 然后我 运行 下面的逻辑,但它只在当前月份运行。//

for(Integer i=1; i<date.today().month()+1; i++){
this.monthList.add(new SelectOption(String.valueOf(i), monthmaps().get(i)));
        }

// 我将值作为字符串传递给 UI

monthSelection = String.valueOf(date.today().month());

问题:我不能给出年份,或者默认情况下年份过去了,我需要过去 2 年。因此询问了两个下拉选项

VF 页面:

<apex:selectList value="{!monthSelection}"
  multiselect="false" size="1"
   styleClass="form-control pull-left monthlyDropDown"
   style="height: 33px;">
    <apex:selectOptions value="{!monthList}" />
     </apex:selectList>

所以问题仍然存在,如何将地图中的选项提供给用户以选择上一年的月份。

这是他们之前将值传递给字符串的方式 SQL 我们正试图传递。

joinQueryMonth += 'AND ((mcc.processing_year__c=\''+ thisMonthDate.year() +'\' AND mcc.processing_month__c=' + thisMonthDate.month() + ') \n';

希望这次我清楚了。

SelectOption 函数接受 valuelabel 参数。因此,您可以为月份设置更人性化的标签,但仍将其作为数字传递给 Apex。

这是一个相当复杂的演示。请参阅 edit history 了解以前的版本和一些技巧的解释。

<apex:page controller="Whosebug45934022">

<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 Whosebug45934022{

    public Integer m {get;private set;}
    public Integer y {get; private set;}

    public Whosebug45934022(){
        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);
    }
}

我先考虑了眼霜的答案,得出了以下逻辑

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);
        } 

我用来将其传递给 SQL 查询的逻辑

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

现在我的查询将采用 year_sel 和 month_sel 以及 return 的值来选择年份和月份。

感谢 eyescream 的逻辑。