1

我在我的项目中使用 ui-calendar 和 angularjs 进行休假管理项目。如果用户在某天休假,我想限制他再次选择该日期。即,如果用户在 6 月 23 日休假2017 年,当用户使用 ui-calendar 选择多个选项选择从 2017 年 6 月 21 日到 2017 年 6 月 24 日的日期时,我应该收到一个警报,说已经休假。如何解决这个问题。请帮忙。信息:我正在使用 REST Web 服务从我的数据库中获取休假事件。我正在使用 fullcalendar.js 文件,它说版本是 v2.4.0

我的代码

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,
                        displayEventTime: false,
                        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) {
                        	 
                        	 
                        	 if(start < new Date())
                        	    {
                        	        alert("You cannot apply for leave on this day")
                        	    }
                        	    else
                        	    {
                        	        // Its a right date
                        	        // Do something
                        	    	 var obj = {};
                                     obj.startDate = start.toDate();
                                     obj.endDate = end.toDate();

                                     $rootScope.selectionDate = obj;
                                     $("#modal1").openModal();
                        	    }
                        	
                  
                        },
                        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: {signum:$scope.signum}
            }).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


                    });
                });

            });



        }
    }

}]);

4

1 回答 1

1

在“选择”回调中,获取现有事件的列表,如果其中任何一个事件的日期与选择重叠,则意味着用户已经为全部或部分选择预订了假期。所以你可以阻止它继续,就像你在检查它是否在今天之前一样(ps你应该使用momentJS的日期比较函数以获得更强大的结果 - 请参阅momentjs.com/docs/#/query)。此外,当您向服务器提交数据时,服务器应再次验证此规则,因为客户端数据总是可能被恶意用户操纵。

要获取事件,您不会从数据库中获取它们 - 使用此:https ://fullcalendar.io/docs/event_data/clientEvents获取当前显示在日历中的事件。您可以使用过滤器回调仅返回与您选择的开始/结束日期相关的回调

于 2017-06-16T08:23:33.077 回答