10.2. <rich:calendar>
The
<rich:calendar> component allows the user to enter a date and time through an in-line or pop-up calendar. The pop-up calendar can navigate through months and years, and its look and feel can be highly customized.
10.2.1. Basic usage
Basic usage of the
<rich:calendar> component requires only the value attribute, which holds the currently selected date. Example 10.3, “Basic usage” shows a basic declaration, with the value pointing to a bean property. The bean property holds the selected date.
10.2.2. Behavior and appearance
The
<rich:calendar> component is presented as a pop-up by default, appearing as a text field with a button to expand the full pop-up calendar. To render the calendar in-line on the page instead, set popup="false. This displays the full calendar without the text field and display button.
To add keyboard support for manual input, set
enableManualInput="true". To disable the calendar from any user input, set disabled="true".
To change the appearance of the display button from the standard calendar icon, use the
buttonIcon and buttonDisabledIcon attributes to replace the icon with a specified file. Alternatively, use the buttonLabel attribute to display text on the button without an icon. If buttonLabel is specified then both the buttonIcon and buttonDisabledIcon attributes are ignored. To hide the text field box, set showInput="false".
The calendar features a button for locating today's date on the calendar. This can be set to three different values using the
todayControlMode attribute:
hidden, which does not display the button;select, the default setting, which scrolls the calendar to the current month and selects the date; andscroll, which scrolls the calendar to the month but does not select the date.inactive, which displays the date but performs no action when clicked.
To make the entire calendar read-only, set
readonly="true". This allows months and years to be browsed through with the arrow controls, but dates and times cannot be selected.
10.2.3. Time of day
The
<rich:calendar> component can additionally allow a time of day to be specified with the date. After selecting a date the option to set a time becomes available. The default time can be set with the defaultTime attribute. If the time is altered and a new date is selected, it will not reset unless resetTimeOnDateSelect="true" is specified.
The date selection feature is activated if the time is present in the
datePattern attribute for the calendar.
Note
In RichFaces 4, the
<rich:calendar> component supports times that include seconds. Previous versions of RichFaces only supported hours and minutes.
10.2.4. Localization and formatting
Date and time strings can be formatted in a set pattern. Use standard locale formatting strings specified by ISO 8601 (for example,
d/M/yy HH:mm a) with the datePattern attribute to format date and time strings.
To set the locale of the calendar, use the
locale attribute. The calendar will render month and day names in the relevant language. For example, to set the calendar to the US locale, specify locale="en/US".
Use an application resource bundle to localize the calendar control labels. Define the following strings in the resource bundle:
- The RICH_CALENDAR_APPLY_LABEL string is the label for the button.
- The RICH_CALENDAR_TODAY_LABEL string is the label for the button.
- The RICH_CALENDAR_CLOSE_LABEL string is the label for the button.
- The RICH_CALENDAR_OK_LABEL string is the label for the button.
- The RICH_CALENDAR_CLEAN_LABEL string is the label for the button.
- The RICH_CALENDAR_CANCEL_LABEL string is the label for the button.
Alternatively, use the
org.richfaces.calendar resource bundle with Java Archive files (JARs) defining the same properties.
10.2.5. Using a data model
The look and feel of the
<rich:calendar> component can be customized through the use of a data model on the server side. The component supports two different ways of loading data from the server side through defining the mode attribute.
When the
mode attribute is not specified, the component uses the client mode. The client mode loads an initial portion of data within a set date range. The range can be defined by using the preloadDateRangeBegin and preloadDateRangeEnd attributes. Additional data requests for months outside the range are not sent.
Alternatively, with
mode="ajax" the <rich:calendar> requests portions of data from the data model every time the month is switched. The data model can be defined through the dataModel attribute, which points to an object that implements the CalendarDataModel interface. If the dataModel attribute is not defined or has a value of null, the ajax mode functions the same as the client mode.
10.2.6. Client-side customization
Instead of using a data model, the
<rich:calendar> component can be customized on the client-side using JavaScript. Use the dayClassFunction attribute to reference the function that determines the CSS style class for each day cell. Use the dayDisableFunction to reference the function that enables or disables a day cell. Example 10.4, “Client-side customization” demonstrates how client-side customization can be used to style different days in a calendar.
Example 10.4. Client-side customization
<style> .everyThirdDay { background-color: gray; } .weekendBold { font-weight: bold; font-style: italic; } </style> <script type="text/javascript"> var curDt = new Date(); function disablementFunction(day){ if (day.isWeekend) return false; if (curDt==undefined){ curDt = day.date.getDate(); } if (curDt.getTime() - day.date.getTime() < 0) return true; else return false; } function disabledClassesProv(day){ if (curDt.getTime() - day.date.getTime() >= 0) return 'rf-ca-boundary-dates'; var res = ''; if (day.isWeekend) res+='weekendBold '; if (day.day%3==0) res+='everyThirdDay'; return res; } </script> <rich:calendar dayDisableFunction="disablementFunction" dayClassFunction="disabledClassesProv" boundaryDatesMode="scroll" />
10.2.7. JavaScript API
The
<rich:calendar> component can be controlled through the JavaScript API. The JavaScript API provides the following functions:
showPopup()- Expand the pop-up calendar element.
hidePopup()- Collapse the pop-up calendar element.
switchPopup()- Invert the state of the pop-up calendar element.
getValue()- Return the selected date value of the calendar.
getValueAsString()- Return the selected date value of the calendar as a formatted string.
setValue(newValue)- Set the selected date value to the
newValuedate passed as a parameter. If the new date is not in the currently displayed month, a request is performed to display the correct month. resetValue()- Clear the selected date value.
today()- Select today's date.
getCurrentMonth()- Return the number of the month currently being displayed.
getCurrentYear()- Return the number of the year currently being displayed.
showSelectedDate()- Show the calendar month that contains the currently selected date.
showDateEditor()- Show the date editor pop-up.
hideDateEditor()- Hide the date editor pop-up.
showTimeEditor()- Show the time editor pop-up.
hideTimeEditor()- Hide the time editor pop-up.
10.2.8. Reference data
component-type:org.richfaces.Calendarcomponent-class:org.richfaces.component.UICalendarcomponent-family:org.richfaces.Calendarrenderer-type:org.richfaces.CalendarRendererhandler-class:org.richfaces.view.facelets.CalendarHandler
10.2.9. Style classes and skin parameters
Table 10.2. Style classes (selectors) and corresponding skin parameters
| Class (selector) | Skin Parameters | Mapped CSS properties |
|---|---|---|
| panelBorderColor
|
border-color
|
| No skin parameters. | |
| panelBorderColor
|
border-bottom-color
|
additionalBackgroundColor
|
background-color
| |
generalSizeFont
|
font-size
| |
generalFamilyFont
|
font-family
| |
| panelBorderColor
|
border-bottom-color
|
additionalBackgroundColor
|
background-color
| |
generalSizeFont
|
font-size
| |
generalFamilyFont
|
font-family
| |
| headerBackgroundColor
|
background-color
|
headerSizeFont
|
font-size
| |
headerFamilyFont
|
font-family
| |
headerWeightFont
|
font-weight
| |
headerTextColor
|
color
| |
| panelBorderColor
|
border-right-color, border-bottom-color
|
additionalBackgroundColor
|
background
| |
generalSizeFont
|
font-size
| |
generalFamilyFont
|
font-family
| |
| panelBorderColor
|
border-right-color, border-bottom-color
|
additionalBackgroundColor
|
background
| |
generalSizeFont
|
font-size
| |
generalFamilyFont
|
font-family
| |
| headerBackgroundColor
|
background-color
|
headerSizeFont
|
font-size
| |
headerFamilyFont
|
font-family
| |
headerWeightFont
|
font-weight
| |
headerTextColor
|
color
| |
| additionalBackgroundColor
|
background
|
generalSizeFont
|
font-size
| |
generalFamilyFont
|
font-family
| |
| No skin parameters. | |
| No skin parameters. | |
| calendarWeekBackgroundColor
|
background-color
|
generalTextColor
|
color
| |
tableBackgroundColor
|
border-color
| |
panelBorderColor
|
border-right-color, border-bottom-color
| |
| panelBorderColor
|
border-color
|
panelBorderColor
|
border-right-color, border-bottom-color
| |
| No skin parameters. | |
| panelBorderColor
|
border-bottom-color, border-right-color
|
tableBackgroundColor
|
background-color
| |
generalSizeFont
|
font-size
| |
generalFamilyFont
|
font-family
| |
| No skin parameters. | |
| calendarCurrentBackgroundColor
|
background-color
|
calendarCurrentTextColor
|
color
| |
| headerBackgroundColor
|
background-color
|
headerTextColor
|
color
| |
| calendarSpecBackgroundColor
|
background-color
|
calendarSpecTextColor
|
color
| |
| panelBorderColor
|
border-bottom-color, border-right-color
|
calendarWeekBackgroundColor
|
background-color
| |
generalSizeFont
|
font-size
| |
generalFamilyFont
|
font-family
| |
| calendarHolidaysBackgroundColor
|
background-color
|
calendarHolidaysTextColor
|
color
| |
| No skin parameters. | |
| buttonSizeFont
|
font-size
|
buttonFamilyFont
|
font-family
| |
| controlBackgroundColor
|
background-color
|
panelBorderColor
|
border-color
| |
subBorderColor
|
border-right-color, border-bottom-color
| |
| headerBackgroundColor
|
background-color, border-color
|
| No skin parameters. | |
| No skin parameters. | |
| No skin parameters. | |
| tableBackgroundColor
|
background
|
| shadowBackgroundColor
|
background-color
|
| No skin parameters. | |
| panelBorderColor
|
border-color
|
calendarSpecBackgroundColor
|
background
| |
| calendarCurrentBackgroundColor
|
background-color
|
calendarCurrentTextColor
|
color
| |
| additionalBackgroundColor
|
background
|
tableBackgroundColor
|
border-color
| |
panelBorderColor
|
border-right-color, border-bottom-color
| |
| additionalBackgroundColor
|
background
|
panelBorderColor
|
border-color
| |
tableBackgroundColor
|
border-right-color, border-bottom-color
| |
| No skin parameters. | |
| tableBackgroundColor
|
border-color
|
panelBorderColor
|
border-right-color, border-bottom-color
| |
| tableBackgroundColor
|
border-right-color, border-bottom-color
|
panelBorderColor
|
border-color
| |
calendarWeekBackgroundColor
|
background-color
| |
| panelBorderColor
|
border-color
|
additionalBackgroundColor
|
background
| |
generalSizeFont
|
font-size
| |
generalFamilyFont
|
font-family
| |
| generalSizeFont
|
font-size
|
generalFamilyFont
|
font-family
| |
| No skin parameters. | |
| No skin parameters. | |
| panelBorderColor
|
border-color
|
tableBackgroundColor
|
background
| |
generalSizeFont
|
font-size
| |
generalFamilyFont
|
font-family
| |
| additionalBackgroundColor
|
background
|
panelBorderColor
|
border-top-color
| |
| additionalBackgroundColor
|
background
|
panelBorderColor
|
border-top-color
| |
| panelBorderColor
|
border-right-color
|
