QWERTY
QWERTY

Reputation: 2315

Angular 2 Disable Specific Dates in Date Picker

I was having some problem when trying to disable certain dates in date picker. My customized date picker in typescript as per following:

import { DateFormatter } from './ng2-bootstrap/date-formatter';
import { DatePickerComponent } from './ng2-bootstrap/datepicker.component';
import * as moment from 'moment-timezone';

@Component({
    selector: '[acn-datepicker-popup]',
    templateUrl: './datepicker-popup.component.html',
    inputs: [],
    host: {}
})
export class DatepickerPopupComponent implements OnInit {
    @Input() datepickerDirective: any;
    @Input() minDate: moment.Moment;
    @Input() maxYears: number;
    @Input() alignRight: boolean = false;
    @Input() disabledDates: string;

    private dateFormat: string = 'DD/MM/YYYY';
    private earliestDate: string = '01/01/1970';
    
    datePickerCmp: DatePickerComponent;
    datepickerFormControl: FormControl;
    selectedDate: moment.Moment;
    dateFormatter: DateFormatter = new DateFormatter();
    
    constructor(private _datePickerCmp: DatePickerComponent, @Inject('datepickerFormControl') private _datepickerFormControl: FormControl) {
        this.datePickerCmp = _datePickerCmp;
        this.datepickerFormControl = _datepickerFormControl;
        if (this.datepickerFormControl.value && this.dateFormatter.isValid(this.datepickerFormControl.value, this.dateFormat)) {
            this.selectedDate = moment(this.datepickerFormControl.value, this.dateFormat);
        }    
    }

    ngOnInit() {
        var splits = this.disabledDates.toString().split(";");
        for(let i = 0; i < splits.length; i++){
            console.log("Dates" + splits[i]);
        }
    }

My date picker component in HTML:

<div class="col-sm-6 form-group has-feedback">
        <label for="testDte">TEST DATE<span class="text-danger">*</span></label>
        <div class="input-group" [(datepicker-popup)]="bookingModel.controls.testDte" [disabledDates]="bkDatesListStr">
            <input type="text" [formControl]="bookingModel.controls.testDte" class="form-control"  maxlength="10" style="border-right:transparent">
            <span class="input-group-btn" align="right">
                <button class="btn btn-square btn-white-primary form-control" type="button"><i class="fa fa-calendar"></i></button>
            </span>
        </div>
    </div>

I managed to passed in the dates to be disabled in ngOnInit(). The splits variable contains the list of dates to be greyed out in date picker. However, I have no idea how to set the dates to be disabled in the date picker. Any ideas?

Thanks!

Upvotes: 2

Views: 3913

Answers (1)

Sakthi Bala K.S
Sakthi Bala K.S

Reputation: 177

I will give you simple example of how to disable specific dates.

First we need to just install material design theme in our angular application. so let's add as like bellow:

ng add @angular/material

Ts File:

import { Component } from '@angular/core';
  
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'my-app';
  
    myHolidayDates = [
                    new Date("12/1/2020"),
                    new Date("12/20/2020"),
                    new Date("12/17/2020"),
                    new Date("12/25/2020"),
                    new Date("12/4/2020"),
                    new Date("12/7/2020"),
                    new Date("12/12/2020"),
                    new Date("12/11/2020"),
                    new Date("12/26/2020"),
                    new Date("12/25/2020")
                ];
  
    myHolidayFilter = (d: Date): boolean => {
        const time=d.getTime();
        return !this.myHolidayDates.find(x=>x.getTime()==time);
    }
}

Now in view file, we will write code of input element with datepicker as like bellow:

<h1>Angular Material Disable specific dates</h1>
     
<mat-form-field>
  <input matInput [matDatepicker]="picker" [matDatepickerFilter]="myHolidayFilter" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker ></mat-datepicker>
</mat-form-field>

Try this method.

Upvotes: 1

Related Questions