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

时间:2017-08-29 08:17:28

标签: visualforce apex

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

//我创建了一个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>

所以问题仍然存在,如何从Map向用户提供从上一年选择月份的选项。

以下是他们以前如何将值传递给我们试图传递的字符串SQL。

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

我希望这次我很清楚。

2 个答案:

答案 0 :(得分:1)

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

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的值,并返回年份和月份选择。

感谢eyecream的逻辑。

答案 1 :(得分:0)

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

这是一个非常复杂的演示。有关以前的版本,请参阅edit history&amp;一些技巧的解释。

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