代码之家  ›  专栏  ›  技术社区  ›  vinieth anirudh

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

  •  0
  • vinieth anirudh  · 技术社区  · 7 年前

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

    VF页面:

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

    我希望这次我清楚了。

    2 回复  |  直到 7 年前
        1
  •  1
  •   vinieth anirudh    7 年前

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

    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
    

    现在,我的查询将获取year\u sel和month\u sel的值,并返回年份和月份选择。

    谢谢你的逻辑。

        2
  •  0
  •   eyescream    7 年前

    这个 SelectOption value label 参数。所以你们可以在一个月内有更人性化的标签,但仍然可以把它作为一个数字传递给Apex。

    这是一个相当复杂的演示。看见 edit history 对于早期版本(&P);解释一些技巧。

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