J. Carrer
J. Carrer

Reputation: 135

Changing calendar value by input - Prime Faces

I have own calendar in label:

    <cust:customField label="test" forId="calendarId">
      <p:calendar id="calendarId"
                                    value etc...
                                    pattern="yyyy-MM-dd HH:mm:ss"
                                    mask="true"
                                    styleClass="ui-inputdate"
                                    readonlyInput="true"
                                    locale="pl"
                                    required="true"
      </p:calendar>
    </cust:customField>

under that I have input:

            <h:panelGroup>
                <p:inputText id="newHour">
                    <p:ajax event="change" update="calendarId"/>
                </p:inputText>
                <h:outputText value="h"/>
            </h:panelGroup>

I try to make that if anyone send value to "newHour" it would be update "HH" value in "calendarId". Is that possible?

Upvotes: 0

Views: 463

Answers (1)

lastresort
lastresort

Reputation: 552

.xhtml:

<p:inputText id="newHour" value="#{myBean.hours}>
    <p:ajax listener="#{myBean.changeHours()}" update="calendarId"/>
</p:inputText>

myBean:

private int hours;
private Date date;

public void changeHours(){
    Calendar calendar = Calendar.getInstance();   
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY, hours);
    date = calendar.getTime();
}

//getter + setter

Note that date.setHours() would also work instead of using an extra calendar, but the constructor is deprecated because of some issues with internationalization.

Upvotes: 1

Related Questions