2013-05-03 32 views
0

我正在研究最新版本的素面3.5。 問題:只要發生ajax更新,就會啓用所需的true。顯示下拉框時出錯?

<p:column> 
    <h:outputText value="Your Birth Date " style="width:50%"/> 
</p:column> 
<p:column> 
<p:selectOneMenu required="true" id="inputMonth" 
value="#{createProfile.dateUtils.inputMonth}" 
requiredMessage="Invalid Month."   
style="width:28%;text-align:left;" 
tabindex="4" 
label="Month" 
height="355"> 
    <f:selectItem itemLabel="MM" itemValue="" /> 
    <f:selectItems value="#{createProfile.dateUtils.monthList}" /> 
    <p:ajax update="inputDay" listener="#{createProfile.dateUtils.loadDays}"> 
</p:selectOneMenu> 

<p:selectOneMenu required="true" id="inputDay" 
value="#createProfile.dateUtils.inputDay}" 
requiredMessage="Invalid Date." 
style="width:28%;text-align:left;" 
tabindex="5" 
label="Day" 
height="355"> 
    <f:selectItem itemLabel="DD" itemValue="" /> 
    <f:selectItems value="#{createProfile.dateUtils.dayList}" /> 
    <p:ajax /> 
</p:selectOneMenu> 

<p:selectOneMenu required="true" id="inputYear" 
value="#{createProfile.dateUtils.inputYear}" 
requiredMessage="Invalid Year." 
style="width:34%;text-align:left;" tabindex="6" 
label="Year" height="455"> 
    <f:selectItem itemLabel="YYYY" itemValue="" /> 
    <f:selectItems value="#{createProfile.dateUtils.yearList}" /> 
    <p:ajax update="inputDay" listener="#{createProfile.dateUtils.loadDays}" /> 
</p:selectOneMenu> 
</p:column> 

當月份從下拉選擇,它的填充與Ajax調用日字段並因爲所需=「true」是設置在日場加載基於選定月份的日期時,當天的文本框從默認顏色變爲紅色,同樣的事情發生在我們選擇年份的時候。驗證應該只在我嘗試提交表單時纔會發生。

日期utils的的.java

public class DateUtils { 

private Map<String,String> monthList; 
private Map<String,String> dayList; 
private Map<String,String> yearList; 
private String inputYear=""; 
private String inputMonth=""; 
private String inputDay=""; 

/** 
* Method to get the Month List. 
* @return monthList 
*/ 
public Map<String, String> getMonthList() { 
    this.monthList =new TreeMap<String, String>(); 
    //this.monthList = new HashMap<String, String>(); 
    for(int i=1;i<=12;i++){ 
     String monthIndex = i<=9?"0"+i:""+i; 
     this.monthList.put(monthIndex, monthIndex); 
    } 
    //monthList = sortByComparator(monthList); 
    return monthList; 
} 
/** 
* Method to set the Month List. 
* @param monthList 
*/ 
public void setMonthList(Map<String, String> monthList) { 
    this.monthList = monthList; 
} 
/** 
* method to get the day list. 
* @return dayList 
*/ 
public Map<String, String> getDayList() { 
    return dayList; 
} 
/** 
* method to set the Day List. 
* @param dayList 
*/ 
public void setDayList(Map<String, String> dayList) { 
    this.dayList = dayList; 
} 
/** 
* Method to get the yearList. 
* @return yearList 
*/ 
public Map<String, String> getYearList() { 
    //this.yearList = new HashMap<String, String>(); 
    this.yearList =new TreeMap<String, String>(Collections.reverseOrder()); 
    Calendar cal = Calendar.getInstance(); 
    int yyyy = cal.get(Calendar.YEAR); 
    for(int i=yyyy-18; i>=(yyyy-112); i--){ 
     this.yearList.put(""+i, ""+i); 
    } 
    return yearList; 
} 
/** 
* Method to setYearList 
* @param yearList 
*/ 
public void setYearList(Map<String, String> yearList) { 
    this.yearList = yearList; 
} 

/** 
* Ajax Method is called when the month value is selected , 
* based on the selected month the days are populated. 
*/ 
public void loadDays(){ 
    this.dayList = new TreeMap<String, String>(); 
    Calendar cal = Calendar.getInstance(); 
    int yyyy = (this.inputYear!=null && this.inputYear.trim().length()>0)?Integer.parseInt(this.inputYear):cal.get(Calendar.YEAR); 
    String selDate = this.inputDay; 
    String selMonth = this.inputMonth; 
    int lastDay = 1; 
    if(yyyy%4==0 && selMonth!=null && selMonth.equals("02")){ 
     lastDay = 29; 
    }else if(selMonth!=null && selMonth.equals("02")){ 
     lastDay = 28; 
    }else if(selMonth!=null && ((selMonth.equals("01") || selMonth.equals("03") || selMonth.equals("05") || selMonth.equals("07") || selMonth.equals("08") || selMonth.equals("10") || selMonth.equals("12")))) { 
     lastDay = 31; 
    }else{ 
     lastDay = 30; 
    } 
    for(int i=1; i<=lastDay;i++){ 
     String days = i<=9?"0"+i:""+i; 
     this.dayList.put(days, days); 
    } 

    if(selDate!=null && selDate.trim().length()>0 && Integer.parseInt(selDate)<=lastDay){ 
     this.inputDay = selDate; 
    } 
} 
/** 
* Method to get the Input day. 
* @return inputDay 
*/ 
public String getInputDay() { 
    return inputDay; 
} 
/** 
* Method to set the Input Day. 
* @param inputDay 
*/ 
public void setInputDay(String inputDay) { 
    this.inputDay = inputDay; 
} 
/** 
* Method to get the Input year. 
* @return inputYear 
*/ 
public String getInputYear() { 
    return inputYear; 
} 
/** 
* Method to set the Input year. 
* @param inputYear 
*/ 
public void setInputYear(String inputYear) { 
    this.inputYear = inputYear; 
} 
/** 
* Method to getInputMonth 
* @return inputMonth 
*/ 
public String getInputMonth() { 
    return inputMonth; 
} 
/** 
* Method to setInputMonth 
* @param inputMonth 
*/ 
public void setInputMonth(String inputMonth) { 
    this.inputMonth = inputMonth; 
} 

}

謝謝!主要讚賞你的幫助!

回答

0

你有2種可能:

  1. 使用屬性 「立竿見影」

    <p:ajax update="inputDay" listener="#{createProfile.dateUtils.loadDays}" immediate="true" />

    即日= 「真」,你跳過處理驗證階段

  2. 使用屬性「過程「

    <p:ajax update="inputDay" listener="#{createProfile.dateUtils.loadDays}" process="@this" />

    使用process =「@ this」,您不會使用ajax請求處理整個表單,而只處理該字段。

+0

嘗試使用兩個屬性,但仍然不成功,有沒有其他方式可以做到這一點。 – user1669692 2013-05-06 14:36:16