简体   繁体   English

Angular 材料日期选择器设置为用作日期或月份

[英]Angular Material date picker set to use as date or month

I am using angular material datepicker in my angular 8 application and wanted to know if it can be used as a date picker and a month picker.我在我的 angular 8 应用程序中使用 angular 材料日期选择器,想知道它是否可以用作日期选择器和月份选择器。 I mean user can eighter selected the date/month/year or just month/year in the same date picker.我的意思是用户可以在同一日期选择器中选择日期/月/年或仅月/年。 The documentation has no mention of using both variants in the same date picker.该文档没有提到在同一日期选择器中使用这两种变体。 Can anyone help?任何人都可以帮忙吗? Here is my code which uses date/month/year.这是我使用日期/月/年的代码。 Can this be alterted to use month/year also?这也可以更改为使用月/年吗?

<mat-form-field>
    <input matInput [max]="maxDate"
        [matDatepicker]="picker" 
        [(ngModel)]="companyModel.incorporationDate"
        #incorporationDate="ngModel"
        incorporationDate>
        <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
        <mat-datepicker #picker disabled="false"></mat-datepicker>
</mat-form-field>

straight from Angular Material documentation直接来自 Angular 材料文档

<mat-form-field>
  <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>

and in the TS file:并在 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();
  }
}

In your case the chosen Month Handler might be something like this:在您的情况下,选择的月份处理程序可能是这样的:

chosenMonthHandler(normlizedMonth: Moment, datepicker: MatDatepicker<Moment>) {
    const month = moment(normlizedMonth).month();
    const date = moment(this.date.value);
    const ctrlValue = new Date(date.year() , month);
    this.date.patchValue(ctrlValue);
  }

so you can have both behaviours, as for month, and day picker.所以你可以有两种行为,比如月份和日期选择器。 good luck!祝你好运!

play it around on stackblitz here and online editor here此处的 stackblitz 和此处的在线编辑器上播放

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM