I had a need to use a calendar component in my application. Calendars are hard to come by, that also act as a scheduling component. In comes JQuery FullCalendar, a plugin that renders a calendar with event components.
To set this up
1. Create an Entity Framework model with all of the AdventureWorks entities. Choose whatever template you like. Leave the context name as the default “AdventureWorksEntities”.
2. Create a new MVC project. Add a new controller named OrderHistoryController. The OrderHistoryController that I used is defined as the following:
The Index action serves up the view. Once the view is loaded, an AJAX request is made back to the server, looking for any orders. Using LINQ, we load up some of those records. I used a GroupBy statement so that I could get one record back per date for a given month, as a way to test with a single record per day.
3. Download JQuery FullCalendar from the following URL: http://arshaw.com/fullcalendar/download/. Copy the JS and CSS files to your project, in the Content and Scripts folder (or whatever folder structure you are using). The files includes are:
- fullcalendar.css – the core CSS file
- fullcalendar.print.css – the CSS stylesheet for printing a calendar
- fullcalendar.js – the core JS file
- fullcalendar.min.js – the minified JS file
- gcal.js – Google calendar integration
4. Add scripts to your page
Rather than adding all the scripts to every page, I added the scripts to the OrderHistory view. To do this, I started by creating a bundle. Open up Bundle.config and add the following:
We use these bundles within the view. To setup the calendar, all that we need to do is add a DIV to a page with an ID, like <div id=”container”></div>. The FullCalendar plugin simply attaches to
The plugin supports a variety of members defined in the documentation that can be specified at initialization time. The key is the events property, which defines the source of the events. The source can be a static list, a pointer to a page or web service, or even a function callback. The latter option gives you the most flexibility, and I’ve demonstrated it’s usage in this example. The callback gets a start and end parameter. These parameters are dates, converted to epoch time (in milliseconds) as they are sent to the server. JQuery is used to make the call to the server, and as the results come back, the process does some massaging first. For instance, a date is coming back to the client in string form, and converts the date back to a date form.
Once our final list is complete, and to load them into the calendar, they are passed to the callback. The plugin handles all of the loading of data into the calendar. Remember from the MVC controller, it returned a title, start (date), and an allDay indicator. Using this data is what the FullCalendar plugin uses as the data source. More parameters