角材料禁用月份

Posted

技术标签:

【中文标题】角材料禁用月份【英文标题】:Angular Material disable Month 【发布时间】:2021-10-31 16:53:06 【问题描述】:

我正在将我的 Angular 项目用于 Angular 材料年月选择器。任何人都知道如何仅显示年份作为选择。

stackblitz here

.html

<mat-form-field appearance="fill">
  <mat-label>Month and Year</mat-label>
  <input matInput [matDatepicker]="dp" [formControl]="date">
  <mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
  <mat-datepicker #dp
                  startView="multi-year"
                  (yearSelected)="chosenYearHandler($event)"
                  (monthSelected)="chosenMonthHandler($event, dp)"
                  panelClass="example-month-picker">
  </mat-datepicker>
</mat-form-field>

.ts

import Component from '@angular/core';
import FormControl from '@angular/forms';
import MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS from '@angular/material-moment-adapter';
import DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE from '@angular/material/core';
import MatDatepicker from '@angular/material/datepicker';

// Depending on whether rollup is used, moment needs to be imported differently.
// Since Moment.js doesn't have a default export, we normally need to import using the `* as`
// syntax. However, rollup creates a synthetic default module and we thus need to import it using
// the `default as` syntax.
import * as _moment from 'moment';
// tslint:disable-next-line:no-duplicate-imports
import default as _rollupMoment, Moment from 'moment';

const moment = _rollupMoment || _moment;

// See the Moment.js docs for the meaning of these formats:
// https://momentjs.com/docs/#/displaying/format/
export const MY_FORMATS = 
  parse: 
    dateInput: 'MM/YYYY',
  ,
  display: 
    dateInput: 'MM/YYYY',
    monthYearLabel: 'MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY',
  ,
;

/** @title Datepicker emulating a Year and month picker */
@Component(
  selector: 'datepicker-views-selection-example',
  templateUrl: 'datepicker-views-selection-example.html',
  styleUrls: ['datepicker-views-selection-example.css'],
  providers: [
    // `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
    // application's root module. We provide it at the component level here, due to limitations of
    // our example generation script.
    
      provide: DateAdapter,
      useClass: MomentDateAdapter,
      deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
    ,

    provide: MAT_DATE_FORMATS, useValue: MY_FORMATS,
  ],
)
export class DatepickerViewsSelectionExample 
  date = new FormControl(moment());

  chosenYearHandler(normalizedYear: Moment) 
    const ctrlValue = this.date.value;
    ctrlValue.year(normalizedYear.year());
    this.date.setValue(ctrlValue);
  

  chosenMonthHandler(normalizedMonth: Moment, datepicker: MatDatepicker<Moment>) 
    const ctrlValue = this.date.value;
    ctrlValue.month(normalizedMonth.month());
    this.date.setValue(ctrlValue);
    datepicker.close();
  

【问题讨论】:

这能回答你的问题吗? How to select only year on mat-datepicker @YongShun 我指的是,但它不能正常工作 【参考方案1】:

概念(简而言之)

    MY_FORMATS dateInput 设置为YYYY 以仅显示年份。 胶水:为NG_VALUE_ACCESSOR 添加提供者和多提供者以扩展现有提供者。 (请参阅参考资料2) Machinery:将ControlValueAccessor 实现到组件以将自定义表单控件值与Angular Reactive Forms 集成。 (请参阅参考资料 2 和 3)

解决方案

datepicker-views-selection-example.html

<mat-form-field appearance="fill">
  <mat-label>Month and Year</mat-label>
  <input matInput
        [matDatepicker]="dp"
        [formControl]="date"
         (click)="_openDatepickerOnClick(dp)"
         (keydown.arrowdown)="_openDatepickerOnClick(dp)"
         (keydown.enter)="_openDatepickerOnClick(dp)">
  <mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
  <mat-datepicker #dp 
                  startView="multi-year" 
                  (yearSelected)="chosenYearHandler($event, dp)"
                  (monthSelected)="chosenMonthHandler($event, dp)"
                  panelClass="example-month-picker">
  </mat-datepicker>
</mat-form-field>

datepicker-views-selection-example.ts

import  Component, forwardRef, Input, ViewChild  from '@angular/core';
import 
  ControlValueAccessor,
  FormControl,
  NG_VALUE_ACCESSOR
 from '@angular/forms';
import 
  MomentDateAdapter,
  MAT_MOMENT_DATE_ADAPTER_OPTIONS
 from '@angular/material-moment-adapter';
import 
  DateAdapter,
  MAT_DATE_FORMATS,
  MAT_DATE_LOCALE
 from '@angular/material/core';
import  MatDatepicker  from '@angular/material/datepicker';

// Depending on whether rollup is used, moment needs to be imported differently.
// Since Moment.js doesn't have a default export, we normally need to import using the `* as`
// syntax. However, rollup creates a synthetic default module and we thus need to import it using
// the `default as` syntax.
import * as _moment from 'moment';
// tslint:disable-next-line:no-duplicate-imports
import  default as _rollupMoment, Moment  from 'moment';

const moment = _rollupMoment || _moment;

// See the Moment.js docs for the meaning of these formats:
// https://momentjs.com/docs/#/displaying/format/
export const MY_FORMATS = 
  parse: 
    dateInput: 'YYYY'
  ,
  display: 
    dateInput: 'YYYY',
    monthYearLabel: 'MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY'
  
;

/** @title Datepicker emulating a Year and month picker */
@Component(
  selector: 'datepicker-views-selection-example',
  templateUrl: 'datepicker-views-selection-example.html',
  styleUrls: ['datepicker-views-selection-example.css'],
  providers: [
    // `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
    // application's root module. We provide it at the component level here, due to limitations of
    // our example generation script.
    
      provide: DateAdapter,
      useClass: MomentDateAdapter,
      deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
    ,
     provide: MAT_DATE_FORMATS, useValue: MY_FORMATS ,
    
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => DatepickerViewsSelectionExample),
      multi: true
    
  ]
)
export class DatepickerViewsSelectionExample implements ControlValueAccessor 
  @ViewChild('dp') dp: MatDatepicker<Moment>;

  date = new FormControl(moment());

  _max: Moment | undefined;
  @Input()
  get max(): Moment | undefined 
    return this._max ? this._max : undefined;
  
  set max(max: Moment | undefined) 
    if (max) 
      const momentDate: Moment =
        typeof max === 'number' ? moment([max, 0, 1]) : moment(max);
      this._max = momentDate.isValid() ? momentDate : undefined;
    
  

  _min: Moment | undefined;
  @Input()
  get min(): Moment | undefined 
    return this._min ? this._min : undefined;
  
  set min(min: Moment | undefined) 
    if (min) 
      const momentDate =
        typeof min === 'number' ? moment([min, 0, 1]) : moment(min);
      this._min = momentDate.isValid() ? momentDate : undefined;
    
  

  // Function to call when the date changes.
  onChange = (year: Date) => ;

  // Function to call when the input is touched (when a star is clicked).
  onTouched = () => ;

  writeValue(date: Date): void 
    if (date && this._isYearEnabled(date.getFullYear())) 
      const momentDate = moment(date);
      if (momentDate.isValid()) 
        momentDate.set( date: 1 );
        this.date.setValue(moment(date),  emitEvent: false );
      
    
  

  registerOnChange(fn: any): void 
    this.onChange = fn;
  

  registerOnTouched(fn: any): void 
    this.onTouched = fn;
  

  // Allows Angular to disable the input.
  setDisabledState(isDisabled: boolean): void 
    isDisabled ? (this.dp.disabled = true) : (this.dp.disabled = false);

    isDisabled ? this.date.disable() : this.date.enable();
  

  chosenYearHandler(normalizedYear: Moment, datepicker: MatDatepicker<Moment>) 
    datepicker.close();

    if (!this._isYearEnabled(normalizedYear.year())) 
      return;
    

    normalizedYear.set( date: 1 );

    this.date.setValue(normalizedYear,  emitEvent: false );
    this.onTouched();
  

  chosenMonthHandler(
    normalizedMonth: Moment,
    datepicker: MatDatepicker<Moment>
  ) 
    datepicker.close();

    this.date.setValue(normalizedMonth.month());
    this.onTouched();
  

  _openDatepickerOnClick(datepicker: MatDatepicker<Moment>) 
    if (!datepicker.opened) 
      datepicker.open();
    
  

  // disable if the year is greater than maxDate lower than minDate
  private _isYearEnabled(year: number) 
    if (
      year === undefined ||
      year === null ||
      (this._max && year > this._max.year()) ||
      (this._min && year < this._min.year())
    ) 
      return false;
    

    return true;
  

Sample Solution on StackBlitz


参考文献

    @Tushar's answer on How to select only year on mat-datepicker Connect your custom control to ngModel with Control Value Accessor. ControlValueAccessor

【讨论】:

以上是关于角材料禁用月份的主要内容,如果未能解决你的问题,请参考以下文章

在 datepicker 中仅显示当前月份,其余所有月份应使用 angularjs 禁用

如何在monthpicker中禁用未来的月份和年份

如何在 UIDatePicker 中禁用未来的日期、月份和年份?

有没有办法在 bootstrap-datetimepicker 中禁用月份/视图模式按钮?

datepicker disabledate 禁用当前年之外的年份 和当前月之后的月份

面试丨诺欧商学院2020年DBA3月份面试时间安排