2 Replies Latest reply on Jan 26, 2012 2:55 PM by giscardff

    rich:calendar with dataModel, failure when changing month/year

      Hi folks,

       

      I am trying to create a rich:calendar that will display different "class style" based on valida or invalid dates. As far as I have seen on the internet, the best way to accomplish that is using Data Model. Looking at all samples, my code seems to be right, but for some unknown reason, that calendar only display the current month and dates properly. When I try to move to next/previous month/year, the calendar disappear and no error is displayed on log.

       

      I am debugging using firedebug, and it seems that everything is OK, i.e., I return the AJAX request with the dates. I can see on the first time, the user click, all the current month JASON values being retrieved, the same way I can see it when the user try to move to the previous month, but for some reason it fails. I put the a4j:log element and I got the follwing warn:

       

       

      debug[19:45:54,303]: search for elements by name 'meta'  in element #document

      debug[19:45:54,303]: Find <meta name='Ajax-Update-Ids' content=''>

      warn[19:45:54,303]: No information in response about elements to replace

      debug[19:45:54,303]: call getElementById for id= org.ajax4jsf.oncomplete

       

      This is my XHTML file:

       

      {code:xml}

                                  <!-- Calendar -->

                                  <td><h:outputLabel value="#{label.date_csv001}"/></td>

                                  <td><rich:calendar value="#{csv001Handler.date}" mode="ajax" dataModel="#{csv001Handler.calendarModel}" enableManualInput="false" datePattern="dd/MM/yyyy" required="true" requiredMessage="#{label.date_validate_csv001}" todayControlMode="hidden"/></td>

      {code}

       

      This is my managed bean csv001Handler

       

       

      {code}

          public CalendarDataModelImpl getCalendarModel() {

              try{

                  buildDates();

                  calendarModel = new CalendarDataModelImpl(dates);

              }//end try

              catch(Exception ex){

                  displayErrorMessage(ex.getMessage());

                  ex.printStackTrace();

              }//end catch

              return calendarModel;

          }

      {code}

       

       

      This is my Calendar and CalendarItem implementation:

       

       

      {code}

      public class CalendarDataModelImpl implements CalendarDataModel {

       

          //define attributes

          private CalendarDataModelItem[] items;                    //item of dates

          private List<Date> dates;                                //available dates

       

          //--------------------------------------------------------------------

          // CONSTRUCTORs

          //--------------------------------------------------------------------

          /**

           * Create calendar with list of dates to be enabled

           * @param dates List of dates to be enabled

           */

          public CalendarDataModelImpl(List<Date> dates){

              this.dates = dates != null ? dates : new ArrayList<Date>();

          }

       

          //--------------------------------------------------------------------

          // BASEs

          //--------------------------------------------------------------------

          /**

           * Retrieve list of dates to be presented in te rich:calendar component

           * @param dateArray list of dates that are supposed to be presented

           */

          @Override

          public CalendarDataModelItem[] getData(Date[] dateArray) {

       

              if (dateArray == null)return null;                                //no date for list of dates

       

              items = new CalendarDataModelItem[dateArray.length];

              for(int i = 0; i < dateArray.length; i++)

                  items[i] = createDataModelItem(dateArray[i]);

              return items;

       

          }

       

          /**

           * Define no tooltip for any date

           * @param date the date to retrieve the tooltip

           */

          @Override

          public Object getToolTip(Date date) {

              return null;

          }

       

          //--------------------------------------------------------------------

          // UTILs

          //--------------------------------------------------------------------

          /**

           * Create a date to be presented

           * @param date the current date

           * @return CalendarDataModelItem date to be presented

           */

          protected CalendarDataModelItem createDataModelItem(Date date) {

       

              CalendarDataModelItemImpl item = new CalendarDataModelItemImpl();    //create the item

              Calendar calendar = Calendar.getInstance();                            //date calendar

              calendar.setTime(date);                                                //bind calendar to current date

              item.setDay(calendar.get(Calendar.DAY_OF_MONTH));                    //set current day of the month

       

              //run over all valid dates

              for(Date _date : dates){

       

                  Calendar _calendar = Calendar.getInstance();                    //date calendar

                  _calendar.setTime(_date);                                        //bind calendar to valid date

       

                  //check if date shall be enabled

                  if(_calendar.get(Calendar.YEAR) != calendar.get(Calendar.YEAR))continue;

                  if(_calendar.get(Calendar.MONTH) != calendar.get(Calendar.MONTH))continue;

                  if(_calendar.get(Calendar.DAY_OF_MONTH) != calendar.get(Calendar.DAY_OF_MONTH))continue;

       

                  item.setEnabled(true);                                            //enable date

                  item.setStyleClass("enabled-class");                            //enable styleClass

       

              }//end for

              return item;

          }

       

       

      }//End DwkpiCalendarDataModel class

       

       

      public class CalendarDataModelItemImpl implements CalendarDataModelItem {

       

          //define attributes

          private int day;                        //day of the calendar

          private boolean enabled;                //disable or enable day

          private String styleClass;                //style class to display enable/disable button

       

          //--------------------------------------------------------------------

          // CONSTRUCTORs

          //--------------------------------------------------------------------

          /**

           * Create calendar item with default values

           */

          public CalendarDataModelItemImpl() {

              this.day = 0;

              this.enabled = false;

              this.styleClass = "disabled-class";

          }

       

          //--------------------------------------------------------------------

          // GETs

          //--------------------------------------------------------------------

          @Override

          public boolean isEnabled() {

              return enabled;

          }

       

          @Override

          public int getDay() {

              return day;

          }

       

          @Override

          public String getStyleClass() {

              return styleClass;

          }

       

          /**

           * Return no special data

           */

          @Override

          public Object getData() {

              return null;

          }

       

          /**

           * Return no special tooltip

           */

          @Override

          public Object getToolTip() {

              return null;

          }

       

          /**

           * Return no special tooltip

           */

          @Override

          public boolean hasToolTip() {

              return false;

          }

       

          //--------------------------------------------------------------------

          // SETs

          //--------------------------------------------------------------------

          /**

           * @param enabled the enabled to set

           */

          public void setEnabled(boolean enabled) {

              this.enabled = enabled;

          }

       

          public void setDay(int day) {

              this.day = day;

          }

       

          public void setStyleClass(String styleClass) {

              this.styleClass = styleClass;

          }

       

      }//End DwkpiCalendarDataModelItem class

      {code}

       

       

      Thanks all, hope you can help me...:-)

       

      Regards

       

      Message was edited by: Giscard Faria (fixing {code} tag format)

        • 1. Re: rich:calendar with dataModel, failure when changing month/year

          Folks, one more information

           

          I changed my calendar to (by removing the attributes required and requiredMessage:

           

           

          {code:xml}

          <rich:calendar id="myCalendar" mode="ajax" boundaryDatesMode="scroll" value="#{csv001Handler.date}" dataModel="#{csv001Handler.calendarModel}" enableManualInput="false" datePattern="dd/MM/yyyy" todayControlMode="hidden"/>

          {code}

           

          and no warning is displayed; it seems that when I try to move between months/years and no date is selected the required attribute gets in action. However I am still not able to navigate between months. In fact, when I click to go next/prev in a month/year, the calendar just disappear.

           

          Hope someone can help me.

           

          Thanks a lot

          • 2. Re: rich:calendar with dataModel, failure when changing month/year

            Finally,

             

             

            The problem was that my calendar was "wrapped" by a a4j:outputPanel with a ajaxRendered="true" attribute.

            In fact I didn't know that, up until I notice that the template page from the customer has such trick.

             

            Thanks all.