4 Replies Latest reply on Oct 7, 2013 6:54 AM by Tom Hain

    Update Bean with selectedDate of Calendar

    Tom Hain Newbie

      Hi,

       

      I'd like to update a date property of a bean with the selected date of the RichFaces4 calendar component.

      The JSF code is:

       

      I expected by usage of "value", the corresponding Bean gets updated because the selected date is displayed correctly inside the calendar component. However, this is not the case.

      I am new to Ajax and RichFaces. Could somebody give me a hint how to make use of the selected date in:

      a) a backing bean

      b) in other ui components

       

      At the end I'd like to update a given data table of the same JSF page by a JPA Query which selects only the data for the selected date.

      Thanks in advance for your help!

        • 1. Re: Update Bean with selectedDate of Calendar
          Tom Hain Newbie

          Sorry, the source code disappeared. Here it is again:

              <rich:calendar id="trainingCalendar2"

                            mode="ajax"
                            valueChangeListener="#{calendarModel.submit()}"
                            popup="false"
                            boundaryDatesMode="scroll"
                            locale="de/DE"
                            buttonLabel="Heute"
                            datePattern="dd.MM.yyyy"
                            dataModel="#{calendarModel}"
                            value="#{calendarModel.selectedDate}">
              </rich:calendar>

          <!-- The following date is always todays date, not the selected date of the calendar component -->
              <h2>Training am #{calendarModel.selectedDate}:</h2>

          • 2. Re: Update Bean with selectedDate of Calendar
            Juraj Húska Master

            Hello Tom,

             

            You need to re-render the selected date information. You can do it with putting <a4j:ajax render event="change" render="selectedDateComponent id" /> into <rich:calendar>.

            You will also need to add the id to the component like: <h2><h:outputText id="selectedDateComponent" value="#{calendar.selectedDAte}"/></h2>

             

            You may find this post useful as well:

            How to fetch Selected Date Records from Rich:Calender

            • 3. Re: Re: Update Bean with selectedDate of Calendar
              Tom Hain Newbie

              Hi Juraj,

               

              thanks for your feedback. I tried the same, but it still does not work.

              Here is my updated JSF:

              <rich:calendar id="trainingCalendar2"
                  mode="ajax" 
                  popup="false"
                  boundaryDatesMode="scroll" 
                  locale="de/DE"
                  buttonLabel="Heute"
                  datePattern="dd.MM.yyyy"
                  dataModel="#{calendarModel}" 
                  value="#{calendarModel.selectedDate}">
                       <a4j:ajax event="change" render="output" />
              </rich:calendar>
              
              <h2><h:outputText id="output" value="#{calendarModel.selectedDate}" /> </h2>
              

               

              The Bean itself is straight forward. However, to make it complete here are the relevant parts:

              @ApplicationScoped
              @Named("calendarModel")
              public class CalendarModel implements CalendarDataModel {
                   
                   private Date selectedDate;
                   
              // ....
              
                  public void setSelectedDate(Date selectedDate) {
                      this.selectedDate = selectedDate;
                  }
              
                  public Date getSelectedDate() {
                      return selectedDate;
                  }
              
              // ...
              
              
              
              

              I even tried it with ValueChangeListener

              valueChangeListener="#{calendarModel.onChangedDate}">
              onChange="submit()"

              and the corresponding Java Code

                  public void onChangedDate(ValueChangeEvent e) {
                      selectedDate = (Date)e.getNewValue();
                      log.info("onChangedDate, newValue " + e.getNewValue().toString());
                  }
              

               

              But both variants do not work. Does somebody see the missing piece ? Thanks in advance!

              • 4. Re: Update Bean with selectedDate of Calendar
                Tom Hain Newbie

                It works! I have forgotten to include the calendar in a form element. What a shame...

                 

                Thanks Juraj for your quick help!!