Angular Material 7 Datepicker: Disable multi year view

你。 提交于 2019-12-14 03:08:40

问题


I'm using the MatDatepicker from @angular/material ^7.0.0-rc.0, and I made a little complex filter that compares every visible day in the timepicker with every day of an array with about 200 or 300 values. It breaks the aplication every time I switch the datepicker to multi-year view mode.

I just want to disable or disallow the multi-year view mode, and let only month mode and year mode working.

HTML:

<mat-form-field (click)="picker.open()" class="date-field">
    <span>{{(dataFields['mosaic_'+REPORT_STAGES.PLANTING]['value'] | date: 'dd/MM/yyyy') || 'Não plantou'}}</span>
    <input matInput hidden [matDatepicker]="picker" placeholder="" [(value)]="dataFields['mosaic_'+REPORT_STAGES.PLANTING]['value']" (dateChange)="plantingDateChange($event)" [matDatepickerFilter]="datePickerFilter.bind(this)">
    <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

My filter method:

datePickerFilter(d: Date): boolean {

  const dAsDate = this.datePipe.transform(d, 'dd-MM-yyyy');

  const validDates = this.allowedDays.filter(e => {
    const eAsDate = this.datePipe.transform(new Date(e), 'dd-MM-yyyy');
    return eAsDate === dAsDate;
  });

  console.log(validDates);

  return (validDates.length > 0);
}

I don't found anything on A. Material manual that can helps me. Hope anyone can!

Thanks a lot!


回答1:


You can pass a customized component as the calendar header, this will allow you to override the button currently allowing the user to select multi year view.

This example does not have the ability to select multi year.

https://material.angular.io/components/datepicker/overview#customizing-the-calendar-header


HTML

reference the custom component on the mat-datepicker

<mat-datepicker #picker [calendarHeaderComponent]="exampleHeader"></mat-datepicker>


来源:https://stackoverflow.com/questions/53595066/angular-material-7-datepicker-disable-multi-year-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!