I have found some info and examples about using beforeShowDay
attribute to enable/disable days in p:calendar
component from PrimeFaces, it is easy and strightforward. But, these examples did not retrieve data from back bean, instead it calculate some conditions on JavaScript.
After some reviews on StackOverflow I found a great entry in Zenida’s blog, about how to do it.
However, this approach did not work on Chrome browser, neither Internet Explorer. So I started debugging and fixed some bugs. Now here you are with a complete solution valid for every browser.
I have created a github repository https://github.com/malaguna/samples to build empty projects to test or show some behaviour. Inside this repository I have crieated a beforeShowDay maven project. You can check it out, it is fully working.
Special considerations
Here you will find what are the relevant parts that differs with normal usage of prime faces components, and those that bugged me while doing this job.
Including JavaScript into xhtml
It is not possible to include a JavaScript function inside an xhtml page anyhow. To avoid problems, the best way is to use <h:outputScript>
and CDATA
as follows:
1 2 3 4 5 6 7 |
|
It is very important to use double backslash before CDATA
starts and also at the end.
Calling back bean from JavaScript
If you want to call a back bean method from JavaScript function, you have to use JSTL functions. In order to use JSTL functions inside an xhtml page, you have to include the namespace xmlns:fn="http://java.sun.com/jsp/jstl/functions"
.
Now you can use it to invoke a back bean method. For example, the following line will call calendarBean.getSpecialDays();
that returns a Java array of strings. This array is joined into a JavaScript string with comma separated elements, that are converted into a JavaScript array of strings. I know it seems a bit confusing.
1
|
|
Date format
Due to I want to compare dates inside JavaScript, it is necessary to send dates from back bean to JavaScript well formated. Following formatter is used into Java code:
1
|
|
Finally, inside JavaScript code you must compare dates ignoring time and timezone information, so previosly it is necessary to transform dates as following:
1
|
|
So now you can compare dates by means of getTime()
function.
Resulting code
So, here you have relevant xhtml code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
and back bean method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
This method only returns three special days, yesterday, today and tomorrow, as you can see, it is quite easy to calculate other days and return it.
Hope you find it useful.