Using the EXT JS Date Picker Widget - Finishing continued
(Page 4 of 4 )
We can add a listener for this event very easily by specifying it in the configuration object passed to the date picker constructor. Change the constructor so that it appears as follows (new code is again shown in bold):
//create the date picker
var myDP = new Ext.DatePicker(
{
startDay: 1,
listeners: {
'select':selectHandler
}
}
);
The listeners property accepts an object literal listing the event to listen for as a property key and the handler function as the property value. Try the code out now; it should do everything that we set out to achieve. For reference, the complete file should now appear as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="ext/resources/css/ext-all.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>EXT JS Datepicker Example</title>
</head>
<body>
<label>Enter a date: </label><input type="text" id="dateField"><button id="openCalendar">Open Calendar</button><div id="calendar"></div>
<script type="text/javascript" src="ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext/ext-all.js"></script>
<script type="text/javascript">
//set local blank image
Ext.BLANK_IMAGE_URL = 'ext/resources/images/default/s.gif';
//define function to be executed when page loaded
Ext.onReady(function() {
//define select handler
var selectHandler = function(myDP, date) {
//get the text field
var field = document.getElementById('dateField');
//add the selected date to the field
field.value = date.format('d/m/Y');
//hide the date picker
myDP.hide();
};
//create the date picker
var myDP = new Ext.DatePicker(
{
startDay: 1,
listeners: {
'select':selectHandler
}
}
);
//render the date picker
myDP.render('calendar');
//hide date picker straight away
myDP.hide();
//define click handler
var clickHandler = function() {
//show the date picker
myDP.show();
};
//add listener for button click
Ext.EventManager.on('openCalendar', 'click', clickHandler);
});
</script>
</body>
</html>
Summary
Using the widgets and other classes provided by EXT JS is easy and intuitive, and can help you to quickly and easily create robust and attractive interfaces. The widgets are styled to a professional degree and work exactly as the API describes.
-DOWNLOAD SOURCE-
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |