5
<title>Example: Two-Pane Calendar with Custom Rendering and Multiple 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: Two-Pane Calendar with Custom Rendering and Multiple Selection</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><style>
43
This example demonstrates how to instantiate a Calendar, switch its template to a double-pane, and create custom renderers for its header and certain cells (based on rules), as well as turn on multiple date selection and disable certain dates from being selected.
45
<p><strong>The <code>selectionMode</code> in this example is set to <code>multiple</code></strong>, which allows additional dates to be selected if a <strong>Shift</strong> or <strong>Ctrl/Meta</strong> key is held down. This selection mode does not allow multiple selection on touchscreen devices; for such devices, use the <code>multiple-sticky</code> selection mode instead.
49
<strong>There are two custom filtering rules provided in the example code.</strong> One matches all Saturdays and Sundays (weekends in the United States), and the other matches Tuesdays and Fridays. The first rule is used in conjunction with a custom renderer to set the corresponding date cell text color to red. The second rule is used to disable matching dates from selection and interaction.
53
<div class="example yui3-skin-sam">
55
.yui3-skin-sam .redtext {
59
<div id="demo" class="yui3-skin-sam">
60
<div id="mycalendar"></div>
62
<script type="text/javascript">
63
YUI().use('calendar', 'datatype-date', 'datatype-date-math', function(Y) {
66
// Switch the calendar main template to the included two pane template
67
Y.CalendarBase.CONTENT_TEMPLATE = Y.CalendarBase.TWO_PANE_TEMPLATE;
69
// Create a new instance of calendar, setting the showing of previous
70
// and next month's dates to true, and the selection mode to multiple
71
// selected dates. Additionally, set the disabledDatesRule to a name of
72
// the rule which, when matched, will force the date to be excluded
73
// from being selected. Also configure the initial date on the calendar
74
// to be July of 2011.
75
var calendar = new Y.Calendar({
76
contentBox: "#mycalendar",
80
selectionMode: 'multiple',
81
disabledDatesRule: "tuesdays_and_fridays",
82
date: new Date(2011, 6, 1)
85
// Create a set of rules to match specific dates. In this case,
86
// the "tuesdays_and_fridays" rule will match any Tuesday or Friday,
87
// whereas the "all_weekends" rule will match any Saturday or Sunday.
92
"2,5": "tuesdays_and_fridays",
99
// Set the calendar customRenderer, provides the rules defined above,
100
// as well as a filter function. The filter function receives a reference
101
// to the node corresponding to the DOM element of the date that matched
102
// one or more rule, along with the list of rules. Check if one of the
103
// rules is "all_weekends", and if so, apply a custom CSS class to the
105
calendar.set("customRenderer", {
107
filterFunction: function (date, node, rules) {
108
if (Y.Array.indexOf(rules, 'all_weekends') >= 0) {
109
node.addClass("redtext");
114
// Set a custom header renderer with a callback function,
115
// which receives the current date and outputs a string.
116
// use the Y.Datatype.Date format to format the date, and
117
// the Datatype.Date math to add one month to the current
118
// date, so both months can appear in the header (since
119
// this is a two-pane calendar).
120
calendar.set("headerRenderer", function (curDate) {
121
var ydate = Y.DataType.Date,
122
output = ydate.format(curDate, {
124
}) + " — " + ydate.format(ydate.addMonths(curDate, 1), {
130
// When selection changes, output the fired event to the
131
// console. the newSelection attribute in the event facade
132
// will contain the list of currently selected dates (or be
133
// empty if all dates have been deselected).
134
calendar.on("selectionChange", function (ev) {
144
<h2>Complete Example Source</h2>
146
<pre class="code prettyprint"><style>
147
.yui3-skin-sam .redtext {
151
<div id="demo" class="yui3-skin-sam">
152
<div id="mycalendar"></div>
154
<script type="text/javascript">
155
YUI().use('calendar', 'datatype-date', 'datatype-date-math', function(Y) {
158
// Switch the calendar main template to the included two pane template
159
Y.CalendarBase.CONTENT_TEMPLATE = Y.CalendarBase.TWO_PANE_TEMPLATE;
161
// Create a new instance of calendar, setting the showing of previous
162
// and next month's dates to true, and the selection mode to multiple
163
// selected dates. Additionally, set the disabledDatesRule to a name of
164
// the rule which, when matched, will force the date to be excluded
165
// from being selected. Also configure the initial date on the calendar
166
// to be July of 2011.
167
var calendar = new Y.Calendar({
168
contentBox: "#mycalendar",
169
width: "700px",
172
selectionMode: 'multiple',
173
disabledDatesRule: "tuesdays_and_fridays",
174
date: new Date(2011, 6, 1)
177
// Create a set of rules to match specific dates. In this case,
178
// the "tuesdays_and_fridays" rule will match any Tuesday or Friday,
179
// whereas the "all_weekends" rule will match any Saturday or Sunday.
184
"2,5": "tuesdays_and_fridays",
185
"0,6": "all_weekends"
191
// Set the calendar customRenderer, provides the rules defined above,
192
// as well as a filter function. The filter function receives a reference
193
// to the node corresponding to the DOM element of the date that matched
194
// one or more rule, along with the list of rules. Check if one of the
195
// rules is "all_weekends", and if so, apply a custom CSS class to the
197
calendar.set("customRenderer", {
199
filterFunction: function (date, node, rules) {
200
if (Y.Array.indexOf(rules, 'all_weekends') >= 0) {
201
node.addClass("redtext");
206
// Set a custom header renderer with a callback function,
207
// which receives the current date and outputs a string.
208
// use the Y.Datatype.Date format to format the date, and
209
// the Datatype.Date math to add one month to the current
210
// date, so both months can appear in the header (since
211
// this is a two-pane calendar).
212
calendar.set("headerRenderer", function (curDate) {
213
var ydate = Y.DataType.Date,
214
output = ydate.format(curDate, {
215
format: "%B %Y"
216
}) + " &mdash; " + ydate.format(ydate.addMonths(curDate, 1), {
217
format: "%B %Y"
222
// When selection changes, output the fired event to the
223
// console. the newSelection attribute in the event facade
224
// will contain the list of currently selected dates (or be
225
// empty if all dates have been deselected).
226
calendar.on("selectionChange", function (ev) {
232
</script></pre>
238
<div class="yui3-u-1-4">
239
<div class="sidebar">
243
<div class="sidebox">
245
<h2 class="no-toc">Examples</h2>
249
<ul class="examples">
252
<li data-description="How to create a single-pane calendar with selectable dates">
253
<a href="calendar-simple.html">Simple Calendar with Selection</a>
258
<li data-description="How to create a multi-pane calendar with custom-rendered cells and multiple date selection.">
259
<a href="calendar-multipane.html">Two-Pane Calendar with Custom Rendering and Multiple Selection</a>
274
<script src="../assets/vendor/prettify/prettify-min.js"></script>
275
<script>prettyPrint();</script>