2017-08-29 99 views
0

我已经尝试过第二个选项,但没有考虑您的宝贵建议,但现在我已经完成了复杂的想法。我曾尝试传递值作为日期选择器,然后拆分它们,但这并没有帮助我。我尝试了下面的代码,我会向你解释我现在面临的挑战。如何将当前月份显示为文本并传递值?

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

//然后我跑到下面的逻辑,但它只能运行于当前Month.//

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

问题:我不能给一个年头,或者我有默认的年份,我需要过去两年。因此,两个下拉选项被问

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

我希望我这次清楚。

回答

0

SelectOption功能接受valuelabel参数。因此,您可以在月份拥有更人性化的标签,但仍然会将其作为数字传递给Apex。

这里是相当复杂的演示。有关以前版本的edit history &解释一些技巧。

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

tnks现在,如果我想要有两个字段,一个用于过去两年的月份和其他字段,我将如何分解它?我这样问是因为,我的SQL以10月份为10,今年为默认年份,我希望今年有两个月(2017年)(本例中为8月份)和所有12个月,当我选择2016年。这将是很好,如果你能帮助我。你给我们的想法是太棒了,但以前的SQL形成不允许的,我想到的唯一选择是手动选择一年并传递值。 –

+0

感谢队友我现在正在尝试这两个选项,我正在尝试最初建议的选项。请纠正我,如果我错了我会给的价值= {!monthlyselect},然后在控制端我给monthlyselect.year()和Monthselect.month()。我正在向你学习。谢谢你指导像我这样的人。 –

+0

你是绝对正确的第二个选择,重新渲染可折叠的内部工作,我想知道如何通过第一个选项的值,请帮助。 –

1

我已经考虑EYESCREAM第一个答案,并拿出了以下逻辑

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

感谢eyescream为您的逻辑。

相关问题