YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Calendar Control: Using the Render Stack

# Calendar Control: Using the Render Stack

Calendar employs a feature called the Render Stack to allow you to customize the way that certain date cells are rendered on the Calendar. The Render Stack consists of a collection of functions that can be executed when rendering certain dates. When the Calendar is rendered, each function in the Render Stack is executed for each applicable date. This functionality allows developers implementing the Calendar to alter the contents of date cells based on a set of rules.

This example shows you how to use any one of the built-in renderers provided with the Calendar, or write your own.

View example in new window.

## Setting up the Calendar

In this tutorial, we will create a Calendar that makes use of the built-in renderers, and we will also create one of our own.

The built-in renderers available are:

- renderCellDefault - Places a clickable link in the date cell
- renderBodyCellRestricted - Renders a text=only cell with the "restricted" style
- renderOutOfBoundsDate - Renders an out-of-bounds date (beyond the specified min/max dates)
- renderCellNotThisMonth - Renders a cell that is displayed in the current page, but precedes or follows the current month
- renderCellStyleToday - Renders the cell representing today's date
- renderCellStyleSelected - Renders a selected cell
- renderCellStyleHighlight1 - Adds the "highlight1" class to the date cell
- renderCellStyleHighlight2 - Adds the "highlight2" class to the date cell
- renderCellStyleHighlight3 - Adds the "highlight3" class to the date cell
- renderCellStyleHighlight4 - Adds the "highlight4" class to the date cell

To begin, we will instantiate a new Calendar and apply the restricted renderer to 2/29 of any year, and the "highlight1" style to the range of dates between 2/1/2008 and 2/7/2008.

YAHOO.example.calendar.cal1 = new YAHOO.widget.CalendarGroup("cal1","cal1Container", { pagedate:"2/2008" } ); YAHOO.example.calendar.cal1.addRenderer("2/29", YAHOO.example.calendar.cal1.renderBodyCellRestricted); YAHOO.example.calendar.cal1.addRenderer("2/1/2008-2/7/2008", YAHOO.example.calendar.cal1.renderCellStyleHighlight1);

Next, we will create a custom renderer that will place an "X" in the cell for every Sunday, and style it in a dark gray color. In order to specify that our renderer should override the default renderer (renderCellDefault), the custom function must return YAHOO.widget.Calendar.STOP_RENDER. This tells the Calendar to ignore the rest of the Render Stack and continue rendering the rest of the Calendar.

var myCustomRenderer = function(workingDate, cell) { cell.innerHTML = "X"; YAHOO.util.Dom.addClass(cell, "sunday"); return YAHOO.widget.Calendar.STOP_RENDER; } YAHOO.example.calendar.cal1.addWeekdayRenderer(1, myCustomRenderer);

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

### Calendar Control Examples:

### More Calendar Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings