0
votes

Is it possible to show selected holidays in angular material datepicker. https://material.angularjs.org/latest/demo/datepicker.

It can be done in jquery date-picker easily.MVC Datepicker for both holidays and weekends

Please let me know is it can be done using angular material (md-datepicker)

var holidays_dates = ["11-25-2015", "11-27-2015", "11-29-2015","10-9-2015"];
1
Not possible at the moment.kuhnroyal

1 Answers

0
votes

You can decorate the mdCalendarMonthBody directive, which is the directive that creates the cells in the datepicker table.

In the example I have replaced the buildDateCell function with my own function. First thing in the new function I am calling the old function to create the cell and then I'm adding a class to the cell if the date is a holiday.

require('angular-material');
    angular.module('myApp', [])
           .config([ '$provide', function($provide) {

                $provide.decorator('mdCalendarMonthBodyDirective', function($delegate) {
                    var originalDirective = $delegate[0];

                    var oldBuildDateCell = originalDirective.controller.prototype.buildDateCell;
                    originalDirective.controller.prototype.buildDateCell = function(opt_date) {
                        var cell = oldBuildDateCell.apply(this, [opt_date]);

                        var holidays = [{'holidayType':'holiday_type_newyear','date':'2015-12-31T23:00:00.000Z'},{'holidayType':'holiday_type_prayer_day','date':'2016-04-21T22:00:00.000Z'},{'holidayType':'holiday_type_easter_monday','date':'2016-03-27T22:00:00.000Z'},{'holidayType':'holiday_type_pentecost_day','date':'2016-05-14T22:00:00.000Z'},{'holidayType':'holiday_type_good_friday','date':'2016-03-24T23:00:00.000Z'},{'holidayType':'holiday_type_christmas_day','date':'2016-12-24T23:00:00.000Z'},{'holidayType':'holiday_type_boxing_day','date':'2016-12-25T23:00:00.000Z'},{'holidayType':'holiday_type_maundy_thursday','date':'2016-03-23T23:00:00.000Z'},{'holidayType':'holiday_type_ascension_day','date':'2016-05-04T22:00:00.000Z'},{'holidayType':'holiday_type_whit_monday','date':'2016-05-15T22:00:00.000Z'},{'holidayType':'holiday_type_palm_sunday','date':'2016-03-19T23:00:00.000Z'},{'holidayType':'holiday_type_easter_day','date':'2016-03-26T23:00:00.000Z'}];
                        if (opt_date) {
                            for (var i=0; i<holidays.length; i++) {
                                if (this.dateUtil.isSameDay(opt_date, new Date(holidays[i].date))) {
                                    cell.classList.add('datepicker-holiday');
                                }
                            }
                        }
                        return cell;
                    };

                    return $delegate;

                });
        }]);

Then all there's left is create a style for the holidays in the calendar.

.datepicker-holiday .md-calendar-date-selection-indicator {
    background-color: #00C853;
}