Setting primefaces calendar end date after setting

2019-03-04 18:26发布

问题:

I'm using primefaces calendar for the creation of an event. With the "mindate" parameter i have disabled the days before the current day. I want to do this even with the end date, disabling the days before the start date. I don't know how to handle this, since the backing bean gets the start date only after the validation of the entire form.

I need the backing bean to be set right after entering the start date on the inputText field.

Here the HTML:

<p:calendar immediate="true" mindate="#{createEventBean.today}" id="startingtime" value="#{createEventBean.current.startingtime}"/>

<p:calendar mindate="#{createEventBean.current.startingtime}" id="endingtime" value="#{createEventBean.current.endingtime}"/>

here the backing bean's method:

public Date getToday(){
    return new Date();
}

回答1:

You can use <p:ajax> to update the end date on select of start date. The <p:calendar> supports the ajax event dateSelect which is fired when a date is selected.

So, this should do:

<p:calendar value="#{bean.startDate}" mindate="#{bean.today}">
    <p:ajax event="dateSelect" update="endDate" />
</p:calendar>
<p:calendar id="endDate" value="#{bean.endDate}" mindate="#{bean.startDate}" />