Rich Faces Calendar Data Model

Often we might want to control the calendar object of our view from the server side.
For instance I needed the user to be prevented from selecting the date before today. This was achieved using the CalendarDataModel.

Simply use the following steps:

Step 1) In your view with element add the following property:

dataModel="#{calendarDataModel}" mode="ajax"

--here dataModel property indicates the model to be used. The mode defines whether to operate on client mode or on ajax mode. To use datamodel we need to have ajax mode to be used.

Step 2) Now register the bean named calendarDataModel in your applicationContext.xml(for spring application) or in faces-config.xml in JSF based application or wherever appropriate(depending on the requirement)


Step 3) Now Define the classes CalendarDataModelImpl and CalendarDataModelItemImpl as shown below:

Class CalendarDataModelImpl.java



When the calendar item is loaded on the view the main method that will be triggered is:

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


So we can customize our bean as per our requirement.


Class CalendarDataModelItemImpl.java


This class will access each and every element (item) of the date element. So we can change the corresponding attributes of the element if required.

3 comments:

  1. Interesting but you can achieve this via javascript if you want with the properties:

    isDayEnabled="isDayEnabled" dayStyleClass="getDisabledStyle"

    and this js functions

    function isDayEnabled(day){
    if (curDt==undefined){
    curDt = day.date.getDate;
    }
    return (curDt.getTime() - day.date.getTime() > 0);
    }

    function getDisabledStyle(day){
    if (!day.enabled)
    return 'rich-calendar-boundary-dates disabledDay';
    }

    I think is the same but without the need of AJAX.

    Bye

    ReplyDelete
  2. Hi Anonymous,
    Your approach seems to be reasonable when we need to do some little consideration and everything is controllable on the clientside itself.
    Is it possible for the javascript to control a certain date range that would be decided by some procedure(or algorithm to be executed on server side).
    Wont the javascript depend on the user's browser-enabling disabling of scripts or changing some data read by the javascript if any(for instance is it the system time of the javascript time to be read?).

    Both approach are equally valid in their relevances.

    Thanks

    ReplyDelete
  3. I think Javascript and AJAX is the way to go.

    ReplyDelete