Help

In this article I want to share a simple but useful trick which was asked frequently at the Richfaces User Forum last weeks. The use case is to allow just month and year selections from the Calendar component and hide date selection feature.

At first we will add a simple inline calendar to our form:

<rich:calendar value="#{calendarBean.selectedDate}" popup="false"/>

Then we hide the days labels list above the grid and the footer via the attributes:

<rich:calendar value="#{calendarBean.selectedDate}" 
popup="false" showWeekDaysBar="false" showFooter="false"/>

Now we want to hide the dates grid. As you could easily check in FireBug — the calendar is a table with one <tr> for header and six <tr> for grid itself. And the date grid <tr> elements has special ids defined. And header element <tr> has no id. So all we need to add common styleClass to calendar and write simple selector which will hide <tr> elements with id defined:

<style type="text/css">
	.special tr[id]{
		display:none; 
	}
</style> 

<rich:calendar value="#{calendarBean.selectedDate}" popup="false" 
showWeekDaysBar="false" showFooter="false" styleClass="special"/>

And now the last step. The calendar should pass the selected date to the bean according to value binding defined. So we need to select some date(e.g. first day of the month) when the current month scrolling occurs. We will use oncurrentdateselected attribute for that. currentdateselected event fired every time when the month/year scrolled. So finally we will have the following code:

<rich:calendar showWeekDaysBar="false" showFooter="false" 
value="#{calendarBean.selectedDate}" 
oncurrentdateselected="event.rich.component.selectDate(event.rich.date)"
popup="false" styleClass="special"/>

Now after the user scrolls the months or years the first date of the chosen month will be selected and we will be able to process the selected month at server side. It will be passed to the selectedDate property of calendarBean.

All you will need to add in your concrete cases is maybe some styling(pretty easy using rich classes) or redefine the header itself(also easy using header facet and macrosubstitutions)

NOTE:

Somebody could note that just currentDate could be used in order to bind selected month. And it's correct but only for inline calendar. In the sample I wrote, you really could get rid of oncurrentdateselected and use just currentDate value binding. But somebody might need the same but for popup mode. And in this case they will need to use value instead of current date because the result should appears at input and because of the fact that calendar ignores currentDay on initial rendering by design and opens on selected date month or at current month if selected date isn't defined. So for popup mode — such selected date trick could be used.

11 comments:
 
13. Nov 2009, 14:21 CET | Link
Hi Ilya,

I just tried to implement in the way you mentioned. Its working fine.If lets say am selecting month September behind the current and made the rendering status false and again i made the status true and setting the value of the calendar component to current date..So it should display current month.But still the component is showing the same old month selected.Could u tell me how to change currentMonthControl.
ReplyQuote
13. Nov 2009, 18:32 CET | Link
Žiga

Hi all,

I am having troubles presenting correct values for months using month-year calendar. As you can see from the code bellow I have a normal outputText for debugging and month-year calendar. The problem is when I choose eg. October outputText shows correct value, that is 1.10.2009, but calendar shows September. The problem has something to do with timeZone but I don't know why or where and I need timezone since we are using UTC times in our app. Please help.

Best regards, Žiga

<h:outputText value="#{reportParameters.date}">
    <f:convertDateTime type="both" timeStyle="medium" timeZone="#{timeZone}"/>
</h:outputText>
<rich:calendar showWeekDaysBar="false" showFooter="false" timeZone="#{timeZone}" locale="#{locale}" id="calendar"
               value="#{reportParameters.date}" popup="false" styleClass="special"
               oncurrentdateselected="event.rich.component.selectDate(event.rich.date)"/>
 
30. Nov 2009, 18:17 CET | Link
Bhagya

Hi All, I have problem in calendar pop-up. I am using rich-faces 3.2.0.

Calendar component is rendered based on the radio button selection (it's a ajax request). When rendered, calendar pop-up is not working as expected, i.e. unable to open the popup. But if i do a refresh, calendar pop-up works fine. Please help.

With Regards, Bhagya

 
10. Jan 2010, 19:50 CET | Link
Angelo Gonçalves dias | agdias(AT)gmail.com

Hi. Is it possible to have a weekly view in Rich Faces calendar, just like in Oracle´s ADF Calendar? (see link below).

http://www.oracle.com/technology/products/jdev/11/how-tos/calendar/calendar.htm

Regards,

Angelo

 
26. Feb 2010, 03:38 CET | Link
Leonid Sokolin | leonid.sokolin(AT)nb.com
I used this logic in my application and it seems to be working fine in IE 7, but doesn't seem to work in IE 6 (all calendar data is shown instead).

Code is as follows:

<style>
...
.hide-days tr[id]{
                display:none;
}
...
</style>

...
<rich:calendar id="customTo" showWeekDaysBar="false" showFooter="false"
                                        value="#{qtBeanParam.curTimePeriod.custPeriod.to}"
                                        required="true" requiredMessage="End month and year should be selected."
                                        oncurrentdateselected="event.rich.component.selectDate(event.rich.date)"
                                        popup="false" styleClass="hide-days"/>

Any suggestions to make it work in earlier versions of IE?
 
24. Jun 2010, 20:04 CET | Link
Fábio Zoroastro | fabiozoroastro(AT)gmail.com

Hello guys, it's work for me.

A have one question: If the User don't select any option, when he submit form, the date value on the backbean is null.

How solutions you using for this case?

Thank's.

 
03. Aug 2010, 06:41 CET | Link

Please send me the calendarBean.java file i have an urgent work with rich faces Calendar component im very new to JSF and rich faces please send me the CalendarBean..

 
09. Sep 2010, 20:29 CET | Link
Leonid Sokolin wrote on Feb 25, 2010 21:38:
I used this logic in my application and it seems to be working fine in IE 7, but doesn't seem to work in IE 6 (all calendar data is shown instead). Code is as follows: ... Any suggestions to make it work in earlier versions of IE?

Hello,

I am having the same problem, but also occurs in IE7. Someone solved this problem?

Thanks

13. Jan 2011, 21:26 CET | Link

Hi , I am having drop down menu has all the month names, while click on the particular month the calendar needs to display.. i am using Rad calendar tell me how to do that?

 
01. Dec 2011, 23:37 CET | Link
Rey
Thanks for this trick, I'm using it and works OK. But I have another problem.

I use a datePattern="MMMM, yyyy" in a popup calendar (I don't need to show the day). When I click on input or popup button the input lose the date and the popup shows today's date. This behavior occurs whith any datePattern that not include at least a 'd' for showing de day.

I using RichFaces 3.3.3 Final. What can be done to avoid this and not lose the date selected?

Thanks
 
26. Jan 2012, 16:02 CET | Link
Domen

Hi

I have same problems. Did you solved them since posting?

thanks

Post Comment