Angular-Material DateTime Picker组件?

Kev*_*now 21 datepicker datetimepicker timepicker angular-material2 angular

我进口的一个,这些项目中,想知道是否有从的角度和材料,包括时间任何官方最新的组件以及日历.我在材料文档中看到了大量的时间选择,并研究了很多第三方,但它们看起来非常复杂.任何帮助都会很棒!

hqh*_*qho 28

我建议您查看@angular-material-components/datetime-picker。这是一个类似@angular/material Datepicker 的 DatetimePicker,它添加了对选择时间的支持。

在此处输入图片说明

  • 该库关于语言环境支持的文档非常差,并且示例代码中存在重大错误。它实际上是不可能使用的,除非您恰好想要默认设置(以及仅限美国的区域设置)。几个月前就有人提出修复请求,但没有明显的活动。我会避免。 (3认同)

Jer*_*myW 15

不幸的是,关于是否有正式的材料支持来选择时间的问题的答案是"否",但它目前是官方Material2 GitHub回购的公开问题:https://github.com/angular/material2/issues/ 5648

希望这很快就会改变,与此同时,你将不得不与你已经发现的第三方战斗.GitHub问题中有一些人提供了他们可以尝试的自制解决方法.


Mau*_*ice 13

matInput与以下类型一起使用时,您可以有一个日期时间选择器datetime-local

  <mat-form-field>
    <input matInput type="datetime-local" placeholder="start date">
  </mat-form-field>
Run Code Online (Sandbox Code Playgroud)

您可以单击占位符的每个部分来设置日,月,年,小时,分钟以及其上午还是下午。

  • 截至 2019 年 10 月,这在 Chrome 和 MS Edge 上运行良好。截至目前,Firefox 不支持 `type="datetime-local"`。它只是将其视为常规输入。[caniuse 统计信息](https://caniuse.com/#search=datetime-local) (24认同)
  • 有没有办法设置输入区域设置?即以 24 小时格式显示“dd/mm/yyyy --:--”? (4认同)
  • @mhodges 截至本周(2021 年 10 月 5 日),Firefox 支持 type="datetime-local" (3认同)

Cul*_*tes 7

只要从angular本身没有官方的日期时间选择器,我建议结合使用默认的角度日期选择器和此Angular Material Timepicker。我之所以选择该选项,是因为我这次发现的所有其他选项都缺乏对问题的支持,已过时或在最新的angular版本中无法正常运行。这个家伙似乎反应很快。

我将它们都包装在一个组件中,因此看起来好像是一个单元。您只需要确保做一些事情即可:

当尚未提供任何输入时,我建议:

  • 单击组件时,应始终先触发日期选择器。
  • 日期选择器关闭后,自动弹出时间选择器。
  • touchUi = true在日期选择器上使用,以便日期选择器和时间选择器都以对话框的形式出现。
  • 确保单击表单时日期选择器也出现(而不是仅显示默认图标)。
  • 使用此解决方案还可以以一种物质形式使用时间选择器,将它们彼此并置时,看起来就像是一种形式。

给定值后,很明显一部分包含时间,另一部分包含日期。在那一刻,很明显,用户必须单击时间来更改时间,并单击日期来更改日期。但是在此之前,因此,当两个字段都为空(并且彼此“附加”为一个字段)时,您应该通过执行上述建议来确保不会混淆用户。

我的组件尚未完成,我会尽力记住自己以便稍后共享代码。如果这个问题超过一个月左右,请发表评论。

编辑:结果

在此处输入图片说明

<div fxLayout="row">
  <div *ngIf="!dateOnly" [formGroup]="timeFormGroup">
    <mat-form-field>
      <input matInput [ngxTimepicker]="endTime" [format]="24"  placeholder="{{placeholderTime}}" formControlName="endTime" />
    </mat-form-field>
    <ngx-material-timepicker #endTime (timeSet)="timeChange($event)" [minutesGap]="10"></ngx-material-timepicker>
  </div>
  <div>
    <mat-form-field>
      <input id="pickerId" matInput [matDatepicker]="datepicker" placeholder="{{placeholderDate}}" [formControl]="dateForm"
             [min]="config.minDate" [max]="config.maxDate" (dateChange)="dateChange($event)">
      <mat-datepicker-toggle matSuffix [for]="datepicker"></mat-datepicker-toggle>
      <mat-datepicker #datepicker [disabled]="disabled" [touchUi]="config.touchUi" startView="{{config.startView}}"></mat-datepicker>
    </mat-form-field>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)
import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { DateAdapter, MatDatepickerInputEvent } from '@angular/material';

import * as moment_ from 'moment';

const moment = moment_;

import { MAT_MOMENT_DATE_ADAPTER_OPTIONS } from '@angular/material-moment-adapter';

class DateConfig {
  startView: 'month' | 'year' | 'multi-year';
  touchUi: boolean;
  minDate: moment_.Moment;
  maxDate: moment_.Moment;
}

@Component({
  selector: 'cb-datetimepicker',
  templateUrl: './cb-datetimepicker.component.html',
  styleUrls: ['./cb-datetimepicker.component.scss'],
})
export class DatetimepickerComponent implements OnInit {

  @Input() disabled: boolean;
  @Input() placeholderDate: string;
  @Input() placeholderTime: string;
  @Input() model: Date;
  @Input() purpose: string;
  @Input() dateOnly: boolean;

  @Output() dateUpdate = new EventEmitter<Date>();

  public pickerId: string = "_" + Math.random().toString(36).substr(2, 9);

  public dateForm: FormControl;
  public timeFormGroup: FormGroup;
  public endTime: FormControl;

  public momentDate: moment_.Moment;
  public config: DateConfig;

  //myGroup: FormGroup;


  constructor(private adapter : DateAdapter<any>) { }

  ngOnInit() {

    this.adapter.setLocale("nl-NL");//todo: configurable
    this.config = new DateConfig();
    if (this.purpose === "birthday") {
      this.config.startView = 'multi-year';
      this.config.maxDate = moment().add('year', -15);
      this.config.minDate = moment().add('year', -90);
      this.dateOnly = true;
    } //add more configurations
    else {
      this.config.startView = 'month';
      this.config.maxDate = moment().add('year', 100);
      this.config.minDate = moment().add('year', -100);
    }


    if (window.screen.width < 767) {
      this.config.touchUi = true;
    }



    if (this.model) {
      var mom = moment(this.model);
      if (mom.isBefore(moment('1900-01-01'))) {
        this.momentDate = moment();
      } else {
        this.momentDate = mom;
      }
    } else {
      this.momentDate = moment();
    }

    this.dateForm = new FormControl(this.momentDate);
    if (this.disabled) {
      this.dateForm.disable();
    }
    this.endTime = new FormControl(this.momentDate.format("HH:mm"));

    this.timeFormGroup = new FormGroup({
      endTime: this.endTime
    });


  }


  public dateChange(date: MatDatepickerInputEvent<any>) {

    if (moment.isMoment(date.value)) {
      this.momentDate = moment(date.value);
      if (this.dateOnly) {
        this.momentDate = this.momentDate.utc(true);
      } 
      var newDate = this.momentDate.toDate();
      this.model = newDate;
      this.dateUpdate.emit(newDate);
    }

    console.log("datechange",date);
  }

  public timeChange(time: string) {

    var splitted = time.split(':');
    var hour = splitted[0];
    var minute = splitted[1];

    console.log("time change", time);
   this.momentDate = this.momentDate.set('hour', parseInt(hour));
    this.momentDate = this.momentDate.set('minute', parseInt(minute));

    var newDate = this.momentDate.toDate();
    this.model = newDate;
    this.dateUpdate.emit(newDate);
  }
}
Run Code Online (Sandbox Code Playgroud)

一个重要的来源:https : //github.com/Agranom/ngx-material-timepicker/issues/126

我认为它仍然值得一些调整,因为当我有更多时间创建它时,它可以工作得更好。最重要的是,我也尝试解决UTC问题,因此所有日期都应以当地时间显示,但应以UTC格式发送到服务器(或至少保存时添加正确的时区)。