Overview:
In this post we will learn about one more wrapper from Kendo UI library called Calendar. If you are starting new on Kendo UI, make sure to go through the blog post – Jump starting Development with Kendo UI for ASP.NET MVC. We will look at the usage of the Calendar widget and how to program the Calendar widget. If you are looking for the index post on this getting started series, click here.
To follow this blog post, just create a “C# Kendo UI for MVC Web Application” project type and work along.
What is Calendar Widget:
The Calendar Widget renders a graphical calendar that supports navigation and selection. It supports custom templates for its “month” view, configurable options for a minimum and maximum date, start view and the depth of the navigation.
Instantiating a Calendar:
To instantiate a Calendar widget on any page, we just make use of the Calendar builder available within the Kendo helper. Here is the code snippet to instantiate a Calendar:
1: @(Html.Kendo().Calendar()
2: .Name("kcalendar")
3: )
Note that the “Name” is mandatory setting. This defines the ID of the control on the client side. So here is the output of the code we wrote:
That’s it – with just one line of code we have a Calendar on our page. Next lets see how we can control the Calendar View.
Calendar View Settings:
Be default if nothing is said about the view we need to see on the Calendar, it is going to use Month view i.e. the current month is shown in the calendar. The Calendar widget has the following view options to choose from:
-
Century – When selected, Calendar will show decades of the current century
-
Decade – When selected, Calendar will show years of the the current decade
-
Month – When selected, Calendar will show days of the current month
-
Year – When selected, Calendar will show months of the current year
The way we set the view is through a method called Start(). The different view settings are available as a CalendarView enum. Here is the code snippet to set the view of the calendar:
1: @(Html.Kendo().Calendar()
2: .Name("kcalendarCentury")
3: .Start(CalendarView.Century)
4: )
Here is the output of the above code:
Setting Calendar Value:
So far we have been able to instantiate a calendar on the page. But if you notice clearly the current date is not highlighted by default. The current date is displayed at the footer of the calendar but the day is not highlighted. That is because, calendar has a property called Value() and to have a date highlighted this property needs to provided with a DateTime instance. It can be current day or any other day. Here is the code snippet to set the value of the calendar:
1: @(Html.Kendo().Calendar()
2: .Name("kcalendarvalue")
3: .Value(DateTime.Today)
4: )
Setting Calendar Value:
1: @(Html.Kendo().Calendar()
2: .Name("kcalendarrestriction")
3: .Min("2012/11/19")
4: .Max("2012/11/23")
5: )
1: var calendar = $("#kcalendar").data("kendoCalendar");
1: var calendar = $("#kcalendar").data("kendoCalendar");
2: calendar.value(new Date(2012, 0, 1));
- Change – occurs when the value is changed
- Navigate – occurs when any navigation is performed by the user i.e. month change, year change etc.
1: @(Html.Kendo().Calendar()
2: .Name("kcalendarevents")
3: .Events(evnt => evnt.Change("onValueChange"))
4: )
5: <script>
6: function onValueChange(e)
7: {
8: var kcal = $("#kcalendarevents").data("kendoCalendar");
9: alert(kcal.value());
10: }
11: </script>
Pingback: Getting Started Series on Kendo UI Wrappers for ASP.NET MVC « Telerik Helper