0

我正在开发最新版本的 Prime Faces 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 调用填充日期字段并根据所选月份加载日期,由于在日期字段上设置了 required =“true”,因此当天的文本框会更改从默认颜色变为红色,当我们选择年份时也会发生类似的事情。仅当我尝试提交表单时才应进行验证。

日期实用程序 .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;
}

}

谢谢!非常感谢您的帮助!

4

1 回答 1

0

你有两种可能性:

  1. 使用属性“立即”

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

    使用 immediate="true" 您可以跳过流程验证阶段

  2. 使用属性“进程”

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

    使用 process="@this" 您不会使用 ajax 请求处理整个表单,而只会处理此字段。

于 2013-05-04T17:50:29.543 回答