Learn MVC Using Angular UI Calendar

Introduction

This article demonstrates MVC using Angular UI Calendar with JSON files in Visual Studio 2017.

By following the steps below, you can use Angular UI Calendar in Angular JS in MVC.

  • Create MVC Project.
  • Configure Angular UI calendar.
  • Work in Angular UI calendar.

Create MVC Project

Open Visual Studio 2017.

MVC

Go to New menu >Click New and Project. Now it will open a New Project Window

MVC

You can select ASP.NET Web Application on Framework 4.5. Enter the name of the project in the “Solution Name” textbox and then click OK.

MVC

One more Window should open. Select MVC Template in this popup and the click OK. Now start cropping your image.

Configure Angular UI Calendar

You can download the plugin from these sources:

Open the _Layout.cshtml and use it refer the .js file from your downloaded folder to this view page.

<script src="~/Plugin/angular/angular.min.js"></script>
 
   <script src="~/Plugin/angular-ui-router/release/angular-ui-router.min.js"></script>
 
   <script src="~/Plugin/moment/min/moment-with-locales.min.js"></script>
 
   <script src="~/Plugin/fullcalendar/dist/fullcalendar.min.js"></script>
 
   <link href="~/Plugin/fullcalendar/dist/fullcalendar.min.css" rel="stylesheet" />
 
   <script src="~/Plugin/angular-ui-calendar/src/calendar.js"></script>
 
   <script src="~/Plugin/fullcalendar/dist/gcal.js"></script>

Link to your Angular configurable file here, using whatever name you gave it.

<script src="~/App/App.module.js"></script>
 
<script src="~/App/App.config.js"></script>
 
<script src="~/App/CalController.js"></script>

Angular Module

You will need to include the module as a dependency of your application.

var uiroute = angular.module('uiroute', ['ui.router','ui.calendar']);

Work in Angular UI Calendar

You can access Angular UI by using the ui-calendar directive in your HTML.

HTML Code

<div ng-controller="CalendarController" >
 
    <div class="panel panel-default">
 
        <div class="panel-heading">
 
            <div class="row">
 
 
                <div class="col-md-6">
 
                    <div class="btn-switch mb btn-switch-purple">
 
                        <button  ng-click="toggleEventSource()" > Load Json</button>
 
                    </div>
 
                </div>
 
            </div>
 
        </div>
 
        <div class="panel-body">
 
            <div ng-model="eventSources"
 
                 calendar="myCalendar"
 
                 ui-calendar="uiConfig.calendar" class="calendar"></div>
 
        </div>
 
 
    </div>
 
</div>

Angular Controller

Simply initiate the events for the calendar.

$scope.today = new Date();
 
        var date = new Date();
 
        var d = date.getDate();
 
        var m = date.getMonth();
 
        vary  = date.getFullYear();
 
 
        $scope.calEventsPers = {
 
            id: 0,
 
            visible: true,
 
            className: ['fc-id-0'],
 
            events: [
 
                { id: 324, title: 'All Day Event', start: new Date(y, m, 1) },
 
                { title: 'Long Event', start: new Date(y, m, d - 5), end: new Date(y, m, d - 2) },
 
                { id: 999, title: 'Repeating Event', start: new Date(y, m, d - 3, 16, 0), allDay: false },
 
                { id: 999, title: 'Repeating Event', start: new Date(y, m, d + 4, 16, 0), allDay: false },
 
                { title: 'Birthday Party', start: new Date(y, m, d + 1, 19, 0), end: new Date(y, m, d + 1, 22, 30), allDay: false },
 
                { title: 'Click for Google', start: new Date(y, m, 28), end: new Date(y, m, 29), url: 'http://google.com/' }
 
            ]
 
        };
 
 
        $scope.eventSources = [$scope.calEventsPers];

Set the UI configuration using the code given below.

$scope.uiConfig = {
 
            calendar: {
 
                height: 400,
 
                editable: true,
 
                header: {
 
                    left: 'month,basicWeek,basicDay',
 
                    center: 'title',
 
                    right: 'prev,next today'
 
                },
 
                eventClick: $scope.alertOnEventClick,
 
                eventDrop: $scope.alertOnDrop,
 
                eventResize: $scope.alertOnResize,
 
                // Select options  
 
                selectable: true,
 
                selectHelper: true,
 
                unselectAuto: true,
 
                select: function (start, end) {
 
                    var title = prompt('Event Title:');
 
                    var eventData;
 
                    if (title) {
 
                        eventData = {
 
                            title: title,
 
                            start: start.format(),
 
                            end: end.format()
 
                        };
 
                        $scope.addEvent(eventData);
 
                    }
 
                }
 
            }          };

 

Click F5 button and run the application. Now it will appear in the browser and you will see the result.

Output 1

MVC

Yes, we got the result! Let’s start binding the JSON file data. So create one JSON file in the project explorer, and write code like I’ve given below.

JSON File

[
 
 
 {
 
   "type": "party",
 
   "title": "Lunch ",
 
   "start": "01/01/2015",
 
   "end": "01/02/2015",
 
   "allDay": "false"
 
 },
 
 {
 
   "type": "link",
 
   "title": "google.com",
 
   "start": "01/01/2015",
 
   "end": "01/02/2015",
 
   "url": "http://google.com/"
 
 }

If you need to, you can bind the data from the database side with JSON format.

Angular Controller

Retrieve the data in the controller using $http service.

$scope.toggleEventSource = function () {
 
            $http.get('server/calendar/external-calendar.json').success(function (data) {
 
 
                var calEventsExt = {
 
                    id: 2,
 
                    visible: true,
 
                    color: 'green',
 
                    textColor: '#fff',
 
                    className: ['fc-id-2'],
 
                    events: []
 
                };
 
 
                // -----------  
 
                // override dates just for demo  
 
                for (var i = 0; i < data.length; i++) {
 
                    data[i].start = new Date(y, m, d + i, 12, 0);
 
                    data[i].end = new Date(y, m, d + i, 14, 0);
 
                }
 
                // -----------  
 
 
                calEventsExt.events = angular.copy(data);
 
 
                $scope.eventSources.push(calEventsExt);
 
 
            });
 
        };

Output 2

MVC

The data will load when you click the load JSON button.

Let’s see the event in this calendar

Angular Controller

/* alert on eventClick */
 
        $scope.alertOnEventClick = function (event, allDay, jsEvent, view) {
 
            alert(event.title + ' was clicked ');
 
        };
 
        /* alert on Drop */
 
        $scope.alertOnDrop = function (event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view) {
 
            alert('Event Droped to make dayDelta ' + dayDelta);
 
        };
 
        /* alert on Resize */
 
        $scope.alertOnResize = function (event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view) {
 
            alert('Event Resized to make dayDelta ' + minuteDelta);
 
        };
 
 
        /* add custom event*/
 
        $scope.addEvent = function (newEvent) {
 
            if (newEvent) {
 
                $scope.calEventsPers.events.push(newEvent);
 
            }
 
        };
 
 
        /* remove event */
 
        $scope.remove =function  (index) {
 
            $scope.calEventsPers.events.splice(index, 1);
 
        };
 
        /* Change View */
 
        $scope.changeView = function (view, calendar) {
 
            $scope.myCalendar.fullCalendar('changeView', view);
 
        };
 
        /* Change View */
 
        $scope.renderCalender = function (calendar) {
 
            $scope.myCalendar.fullCalendar('render');
 
        };

After adding this code in your controller, just run the application.

Output 3

MVC

If you click on the above data, you can add the new event.

Conclusion

In this article, we have learned MVC using Angular UI Calendar with JSON files. If you have any queries, please tell me through the comments sectionbecause your comments are very valuable.

Happy Coding!

sadasdasdjdasdask