5
<title>Example: Simple Calendar with Selection</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: Simple Calendar with Selection</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><style>
42
This example demonstrates how to instantiate a simple Calendar, with an initial date setting of September 2011. The Calendar is preconfigured to show the previous and next month's dates.
46
Try clicking on the toggle buttons to change the initial settings for showing the previous and next months' dates. You can also select dates in the calendar and see selected date on the right, reported via a <code>selectionChange</code> event, and formatted using DataType utility.
50
<div class="example yui3-skin-sam">
51
<!-- Add an additional blue button style -->
54
margin:10px 0px 10px 0px;
56
background-color: #3476b7;
60
<div id="demo" class="yui3-skin-sam yui3-g">
61
<div id="leftcolumn" class="yui3-u">
62
<!-- Container for the calendar -->
63
<div id="mycalendar"></div>
65
<div id="rightcolumn" class="yui3-u">
66
<div id="links" style="padding-left:20px;">
67
<!-- The buttons are created simply by assigning the correct CSS class -->
68
<button id="togglePrevMonth" class="yui3-button">Toggle Previous Month's Dates</button><br>
69
<button id="toggleNextMonth" class="yui3-button">Toggle Next Month's Dates</button><br>
70
Selected date: <span id="selecteddate"></span>
75
<script type="text/javascript">
76
YUI().use('calendar', 'datatype-date', 'cssbutton', function(Y) {
78
// Create a new instance of calendar, placing it in
79
// #mycalendar container, setting its width to 340px,
80
// the flags for showing previous and next month's
81
// dates in available empty cells to true, and setting
82
// the date to today's date.
83
var calendar = new Y.Calendar({
84
contentBox: "#mycalendar",
88
date: new Date()}).render();
90
// Get a reference to Y.DataType.Date
91
var dtdate = Y.DataType.Date;
93
// Listen to calendar's selectionChange event.
94
calendar.on("selectionChange", function (ev) {
96
// Get the date from the list of selected
97
// dates returned with the event (since only
98
// single selection is enabled by default,
99
// we expect there to be only one date)
100
var newDate = ev.newSelection[0];
102
// Format the date and output it to a DOM
104
Y.one("#selecteddate").setContent(dtdate.format(newDate));
108
// When the 'Show Previous Month' link is clicked,
109
// modify the showPrevMonth property to show or hide
110
// previous month's dates
111
Y.one("#togglePrevMonth").on('click', function (ev) {
113
calendar.set('showPrevMonth', !(calendar.get("showPrevMonth")));
116
// When the 'Show Next Month' link is clicked,
117
// modify the showNextMonth property to show or hide
118
// next month's dates
119
Y.one("#toggleNextMonth").on('click', function (ev) {
121
calendar.set('showNextMonth', !(calendar.get("showNextMonth")));
128
<h2>Complete Example Source</h2>
130
<pre class="code prettyprint"><!-- Add an additional blue button style -->
133
margin:10px 0px 10px 0px;
135
background-color: #3476b7;
139
<div id="demo" class="yui3-skin-sam yui3-g">
140
<div id="leftcolumn" class="yui3-u">
141
<!-- Container for the calendar -->
142
<div id="mycalendar"></div>
144
<div id="rightcolumn" class="yui3-u">
145
<div id="links" style="padding-left:20px;">
146
<!-- The buttons are created simply by assigning the correct CSS class -->
147
<button id="togglePrevMonth" class="yui3-button">Toggle Previous Month's Dates</button><br>
148
<button id="toggleNextMonth" class="yui3-button">Toggle Next Month's Dates</button><br>
149
Selected date: <span id="selecteddate"></span>
154
<script type="text/javascript">
155
YUI().use('calendar', 'datatype-date', 'cssbutton', function(Y) {
157
// Create a new instance of calendar, placing it in
158
// #mycalendar container, setting its width to 340px,
159
// the flags for showing previous and next month's
160
// dates in available empty cells to true, and setting
161
// the date to today's date.
162
var calendar = new Y.Calendar({
163
contentBox: "#mycalendar",
164
width:'340px',
167
date: new Date()}).render();
169
// Get a reference to Y.DataType.Date
170
var dtdate = Y.DataType.Date;
172
// Listen to calendar's selectionChange event.
173
calendar.on("selectionChange", function (ev) {
175
// Get the date from the list of selected
176
// dates returned with the event (since only
177
// single selection is enabled by default,
178
// we expect there to be only one date)
179
var newDate = ev.newSelection[0];
181
// Format the date and output it to a DOM
182
// element.
183
Y.one("#selecteddate").setContent(dtdate.format(newDate));
187
// When the 'Show Previous Month' link is clicked,
188
// modify the showPrevMonth property to show or hide
189
// previous month's dates
190
Y.one("#togglePrevMonth").on('click', function (ev) {
192
calendar.set('showPrevMonth', !(calendar.get("showPrevMonth")));
195
// When the 'Show Next Month' link is clicked,
196
// modify the showNextMonth property to show or hide
197
// next month's dates
198
Y.one("#toggleNextMonth").on('click', function (ev) {
200
calendar.set('showNextMonth', !(calendar.get("showNextMonth")));
203
</script></pre>
209
<div class="yui3-u-1-4">
210
<div class="sidebar">
214
<div class="sidebox">
216
<h2 class="no-toc">Examples</h2>
220
<ul class="examples">
223
<li data-description="How to create a single-pane calendar with selectable dates">
224
<a href="calendar-simple.html">Simple Calendar with Selection</a>
229
<li data-description="How to create a multi-pane calendar with custom-rendered cells and multiple date selection.">
230
<a href="calendar-multipane.html">Two-Pane Calendar with Custom Rendering and Multiple Selection</a>
245
<script src="../assets/vendor/prettify/prettify-min.js"></script>
246
<script>prettyPrint();</script>