【问题标题】:Angular Material 7 Datepicker: Disable multi year viewAngular Material 7 Datepicker:禁用多年视图
【发布时间】:2018-12-03 13:36:05
【问题描述】:

我正在使用来自@angular/material ^7.0.0-rc.0 的 MatDatepicker,并且我制作了一个稍微复杂的过滤器,将时间选择器中的每个可见日期与具有大约 200 或 300 个值的数组的每一天进行比较.每次我将日期选择器切换到多年视图模式时,它都会中断应用程序。

我只想禁用或禁止多年视图模式,只让月模式和年模式工作。

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>

我的过滤方法:

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);
}

我在 A. Material 手册上没有找到任何可以帮助我的东西。希望任何人都可以!

非常感谢!

【问题讨论】:

    标签: angular angular-material angular-material-datetimepicker


    【解决方案1】:

    您可以将自定义组件作为日历标题传递,这将允许您覆盖当前允许用户选择多年视图的按钮。

    此示例无法选择多年。

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


    HTML

    引用mat-datepicker上的自定义组件

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

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 2020-11-04
      相关资源
      最近更新 更多