2017-08-29 64 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爲您的邏輯。

相關問題