Calendar Screencast! | July 17th, 2008
Forgot to include this screencast in my last post where I go through the new features. What you can’t see in the video is all my nervous sweating, which is a good thing.
Archive for the 'features' Category
Forgot to include this screencast in my last post where I go through the new features. What you can’t see in the video is all my nervous sweating, which is a good thing.
I was just going over the Calendar component when I found some useful functionality that was all tucked away, obscured from prying eyes. But the time has come to unleash it upon you all!
1. Formatting
There’s a method, DatePicker#getFormattedDate that will return the selected date as a string in the ubiquitous ISO8601 format (YYYY-MM-DD HH:MM:SS).
What’s better still, you can customize the format returned by getFormattedDate by using the formatter attribute on the Calendar declaration to specify a custom formatting function.
The Function
Let’s say I want the Calendar to format dates like this: MM/DD/YYYY.
function customFormatter(date)
{
if (nitobi.base.DateMath.invalid(date)) return "";
return nitobi.lang.padZeros(date.getMonth() + 1) + "/" + nitobi.lang.padZeros(date.getDate()) + "/" + date.getFullYear();
};
A couple things to note here:
The Declaration
Next, we change our Calendar declaration and specify this custom function as the formatter:
<ntb:datepicker id="cal1" formatter="customFormatter"></ntb:datepicker>
Now when we call #getFormattedDate we’ll get a string of the form MM/DD/YYYY.
2. Hidden Inputs
Another useful tidbit is the Calendar renders with a hidden input field that will store the formatted date which makes using it in a form pretty simple. For a Calendar with an id of “calendar1″, the hidden input will have an id of “calendar1.value”.
Go figure!
You are currently browsing the archives for the features category.