0

i am using angular ui-calendar for displaying a series of leaves.The data for this is fed from the REST services.I can add a leave to the database also.The problem is that when i add a leave detail to the database it does not automatically reflect in the calendar.If i refresh the page the data will be reflected. Basically what I want to do is when I submit the form (closing the modal) the data to be displayed in the calendar. Thank you in advance.Following is my code My controller

app.factory('calendarSer', ['$http', '$rootScope', 'uiCalendarConfig', function($http, $rootScope, uiCalendarConfig) {

    return {
        displayCalendar: function($scope) {

            $http.get("rest/leave/holidayList", {}).success(function(data, status, headers, config) {
                $scope.holidayList = data;
                $calendar = $('[ui-calendar]');
                var date = new Date(),
                    d = date.getDate(),
                    m = date.getMonth(),
                    y = date.getFullYear();
                $scope.changeView = function(view) {
                    $calendar.fullCalendar('changeView', view);
                };
                var m = null;
                if ($scope.selectable == "Yes") {
                    m = true;
                } else {
                    m = false;
                }
                /* config object */
                $scope.uiConfig = {
                    calendar: {
                        lang: 'da',

                        height: 400,
                        editable: true,
                        selectable: m,
                        header: {
                            left: 'month basicWeek basicDay',
                            center: 'title',
                            right: 'today prev,next'
                        },
                        eventClick: function(date, jsEvent, view) {
                            $scope.alertMessage = (date.title + ' was clicked ');
                            alert("clicked" + date.title);
                        },

                        select: function(start, end, allDay) {

                            var obj = {};
                            obj.startDate = start.toDate();
                            obj.endDate = moment(end - 1 * 24 * 3600 * 1000).format('YYYY-MM-DD');

                            $rootScope.selectionDate = obj;
                            $("#modal1").openModal();
                            //    calendar.fullCalendar('unselect');
                        },
                        dayRender: function(date, cell) {
                            var today = new Date();
                            today = moment(today).toDate();
                            var end = new Date();
                            end = moment(end).toDate();
                            end.setDate(today.getDate() + 7);
                            date = moment(date).toDate();
                            angular.forEach($scope.holidayList, function(value) {

                                if (((moment(value.holiday_date).format("YYYY-MM-DD")) == moment(date).format("YYYY-MM-DD"))) {
                                    cell.css("background-color", "#2bbbad");
                                    //$('.date').text('Today');
                                    cell.prepend("<span style=\"max-width:200px;word-wrap:break-word;margin-top:10px;\">" + value.description + "</span>");
                                    cell.prepend("<br>")
                                }
                            });
                        },
                        eventRender: $scope.eventRender,
                    }
                };
                console.log($scope.holidayList);
            }).error(function(data, status, headers, config) {
                alert("error from holidaylist");
            });

            $scope.events = [];
            $scope.eventSources = [$scope.events];
            $http.get($scope.url, {
                cache: true,
                params: {}
            }).then(function(data) {

                console.log(data);
                $scope.events.slice(0, $scope.events.length);
                angular.forEach(data.data, function(value) {

                    console.log(value.title);
                    if (value.approvalStatus == "Approved") {
                        var k = '#114727';
                    } else {
                        k = '#b20000'
                    }
                    $scope.events.push({
                        title: value.signum,
                        description: value.signum,
                        start: value.startDate,
                        end: value.endDate,
                        allDay: value.isHalfDay,
                        stick: true,
                        status: value.approvalStatus,
                        backgroundColor: k
                    });
                });
            });
        }
    }

}]);

g-include

<div id="controllerid">
   <div class="row" >
      <div class="col s10 m10 l10">
         <div id="calendar" ui-calendar="uiConfig.calendar" ng-model="eventSources" calendar="myCalendar"></div>
      </div>
   </div>

<!-- Modal Structure -->
<div id="modal1" class="modal" ng-controller="MyAddController">
<div class="modal-content">
   <h4>Apply Leave</h4>
   <div class="row">
      <form class="col s12" id="form1">
         <div class="row modal-form-row">
            <div class="input-field col s6">
               <input id="num" type="text" class="validate" ng-model="test.num"> <label for="num">num</label>
            </div>
            <div class="input-field col s6">
               <input id="ename" type="text" class="validate" ng-model="test.title"> <label for="ename">Employee Name</label>
            </div>
         </div>
         <div class="row">
      <form class="col s12">
      <div class="row modal-form-row">
      <div class="input-field col s5">
      <input id="startDate" type="text" class="validate" value="{{selectionDate.startDate | date}}" readonly  >
      </div>
      <div class="input-field col s5">
      <input id="endDate" type="text" class="validate"  value="{{selectionDate.endDate | date}}" readonly>
      </div>
      <div class="input-field col s1">
      <p>
      <input type="checkbox" id="test6" ng-model="test.isHalfDay" /> <label for="test6">Half Day</label>
      </p>
      </div>
      </div>
      <div class="row">
      <div class="input-field col s12">
      <input id="description" type="text" class="validate" ng-model="test.description"> <label for="description">Description</label>
      </div>
      </div>
      </form>
      </div>
   </div>
   <div class="modal-footer">
      <button class="btn waves-effect waves-light" type="submit" ng-click="add()" name="action"> Submit <i class="material-icons right">send</i>
      </button>
   </div>
</div>
and my add controller
app.controller("MyAddController", function($scope, $http,$rootScope,calendarSer) {
 $scope.test = {};
    $scope.add = function() {
     $("#modal1").closeModal();
     $scope.test1=$rootScope.selectionDate;
  
     var jsonData = JSON.stringify($.extend({}, $scope.test, $scope.test1));
     console.log(""+jsonData);
     
     $http({
         url: "rest/leave/create",
         method: "POST",
         data: jsonData,
         headers: {'Content-Type': 'application/json'}
        
        }).success(function(data, status, headers, config) {
            if (data) {
             console.log("Entered in the add controller");
                $scope.data = data;
                $scope.url="rest/leave/list";
               $scope.selectable="Yes";
                calendarSer.displayCalendar($scope);
                $("#popupmodal").openModal();
                console.log("Exited in the add controller");
                
            }
        }).error(function(data, status, headers, config) {
            alert("error from create leave");
        })
    }
});

ANy help would be appreciated

Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
Vikhyath Maiya
  • 3,122
  • 3
  • 34
  • 68
  • "when i add a leave detail to the database it does not automatically reflect in the calendar." How are you adding new "leave" data to the database? Is it via the same page where the calendar is shown, or some other way? There's no obvious code for it on the page. If you're doing it via the same page, then you can easily add the event to the calendar simultaneously to adding it to the database, using this: https://fullcalendar.io/docs/event_rendering/renderEvent/ – ADyson Jun 05 '17 at 15:30
  • actually i am using select callback function to open a modal when the user clicks on any date.Modal has form with fields regarding leaves. – Vikhyath Maiya Jun 06 '17 at 07:01
  • since that is the area that you have a problem with, can you please show that code? – ADyson Jun 06 '17 at 08:21
  • it's the "add" function that would be the most important thing. – ADyson Jun 06 '17 at 09:42
  • added the code again – Vikhyath Maiya Jun 06 '17 at 09:56
  • I've added an answer with example code. You may need to adjust it to your exact circumstances (I don't know angular, only fullCalendar), but hopefully you get the idea. – ADyson Jun 06 '17 at 10:06

1 Answers1

0

In your "success" function after you run the "create" function, you can simply add the event to fullCalendar using the same data, via the built-in "renderEvent" function.

Something like this (I don't know Angular, so you may need to adjust this slightly to get your calendar element into context, but hopefully you understand the idea). I am also assuming that jsonData contains all the relevant event data which we can use for this:

.success(function(data, status, headers, config) {
            if (data) {
                console.log("Entered in the add controller");
                $scope.data = data;
                //add the event to the calendar UI without refreshing the events
                $('[ui-calendar]').fullCalendar("renderEvent", 
                  {
                    start: jsonData.startDate,
                    end: jsonData.endDate,
                    title: jsonData.title
                  },
                  true //make it stick even when the view changes
               );
                $scope.url="rest/leave/list";
                $scope.selectable="Yes";
                calendarSer.displayCalendar($scope);
                $("#popupmodal").openModal();
                console.log("Exited in the add controller");
            }

You may need to add more fields, or you may need to get momentJS to parse the values in startDate / endDate, depending exactly what those fields output.

The "renderEvent" method can be found in the fullCalendar documentation here: https://fullcalendar.io/docs/event_rendering/renderEvent/

ADyson
  • 57,178
  • 14
  • 51
  • 63