Learn MVC Using Angular UI Calendar

Introduction

This article demonstrates MVC using Angular UI calendar with JSON file in Visual Studio 2017.

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 & project. Now it will open New Project Window

MVC

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

MVC

One more Window should be appearing. Select MVC Template in this popup & Click ok button. Now start cropping image.

Configure Angular UI Calendar

You can download the plug in from

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

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

Link your angular configurable file, whatever you given name

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

Angular Module

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

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

If you have any doubt about configuration, visit the following articles -

Work in Angular UI Calendar

You can set as Angular UI with the ui-calendar directive in the html 

  1. div ng-model="eventSources"   
  2.                  calendar="myCalendar"   
  3.                  ui-calendar="uiConfig.calendar" class="calendar"></div>   

Html Code 

  1. <div ng-controller="CalendarController" >  
  2.     <div class="panel panel-default">  
  3.         <div class="panel-heading">  
  4.             <div class="row">  
  5.   
  6.                 <div class="col-md-6">  
  7.                     <div class="btn-switch mb btn-switch-purple">  
  8.                         <button  ng-click="toggleEventSource()" > Load Json</button>  
  9.                     </div>  
  10.                 </div>  
  11.             </div>  
  12.         </div>  
  13.         <div class="panel-body">  
  14.             <div ng-model="eventSources"   
  15.                  calendar="myCalendar"   
  16.                  ui-calendar="uiConfig.calendar" class="calendar"></div>  
  17.         </div>  
  18.           
  19.     </div>  
  20. </div>    

Angular Controller

Simply initiate the events for calendar 

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

uySet to the ui configuration, like  the code given below 

  1. $scope.uiConfig = {  
  2.             calendar: {  
  3.                 height: 400,  
  4.                 editable: true,  
  5.                 header: {  
  6.                     left: 'month,basicWeek,basicDay',  
  7.                     center: 'title',  
  8.                     right: 'prev,next today'  
  9.                 },  
  10.                 eventClick: $scope.alertOnEventClick,  
  11.                 eventDrop: $scope.alertOnDrop,  
  12.                 eventResize: $scope.alertOnResize,  
  13.                 // Select options  
  14.                 selectable: true,  
  15.                 selectHelper: true,  
  16.                 unselectAuto: true,  
  17.                 select: function (start, end) {  
  18.                     var title = prompt('Event Title:');  
  19.                     var eventData;  
  20.                     if (title) {  
  21.                         eventData = {  
  22.                             title: title,  
  23.                             start: start.format(),  
  24.                             end: end.format()  
  25.                         };  
  26.                         $scope.addEvent(eventData);  
  27.                     }  
  28.                 }  
  29.             }          };   

Click F5 button and Run the Application. Now it will appear in 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 project explorer, write the code likes below.

JSON File

  1. [  
  2.    
  3.  {  
  4.    "type""party",  
  5.    "title""Lunch ",  
  6.    "start""01/01/2015",  
  7.    "end""01/02/2015",  
  8.    "allDay""false"  
  9.  },  
  10.  {  
  11.    "type""link",  
  12.    "title""google.com",  
  13.    "start""01/01/2015",  
  14.    "end""01/02/2015",  
  15.    "url""http://google.com/"  
  16.  }   

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

Angular Controller

Retrieve the data in controller using $http service. 

  1. $scope.toggleEventSource = function () {  
  2.             $http.get('server/calendar/external-calendar.json').success(function (data) {  
  3.   
  4.                 var calEventsExt = {  
  5.                     id: 2,  
  6.                     visible: true,  
  7.                     color: 'green',  
  8.                     textColor: '#fff',  
  9.                     className: ['fc-id-2'],  
  10.                     events: []  
  11.                 };  
  12.   
  13.                 // -----------  
  14.                 // override dates just for demo  
  15.                 for (var i = 0; i < data.length; i++) {  
  16.                     data[i].start = new Date(y, m, d + i, 12, 0);  
  17.                     data[i].end = new Date(y, m, d + i, 14, 0);  
  18.                 }  
  19.                 // -----------  
  20.   
  21.                 calEventsExt.events = angular.copy(data);  
  22.   
  23.                 $scope.eventSources.push(calEventsExt);  
  24.   
  25.             });  
  26.         };   

Output 2

MVC

Data will load when you click load Json button.

Let’s see the event in this calendar

Angular Controller 

  1. /* alert on eventClick */  
  2.         $scope.alertOnEventClick = function (event, allDay, jsEvent, view) {  
  3.             alert(event.title + ' was clicked ');  
  4.         };  
  5.         /* alert on Drop */  
  6.         $scope.alertOnDrop = function (event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view) {  
  7.             alert('Event Droped to make dayDelta ' + dayDelta);  
  8.         };  
  9.         /* alert on Resize */  
  10.         $scope.alertOnResize = function (event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view) {  
  11.             alert('Event Resized to make dayDelta ' + minuteDelta);  
  12.         };  
  13.   
  14.         /* add custom event*/  
  15.         $scope.addEvent = function (newEvent) {  
  16.             if (newEvent) {  
  17.                 $scope.calEventsPers.events.push(newEvent);  
  18.             }  
  19.         };  
  20.   
  21.         /* remove event */  
  22.         $scope.remove = function (index) {  
  23.             $scope.calEventsPers.events.splice(index, 1);  
  24.         };  
  25.         /* Change View */  
  26.         $scope.changeView = function (view, calendar) {  
  27.             $scope.myCalendar.fullCalendar('changeView', view);  
  28.         };  
  29.         /* Change View */  
  30.         $scope.renderCalender = function (calendar) {  
  31.             $scope.myCalendar.fullCalendar('render');  
  32.         };   

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 section, because your comments are very valuable.

Happy Coding!...

Up Next
    Ebook Download
    View all
    Learn
    View all