Kev*_*now 21 datepicker datetimepicker timepicker angular-material2 angular
我进口的一个,这些项目中,想知道是否有从的角度和材料,包括时间任何官方最新的组件以及日历.我在材料文档中看到了大量的时间选择,并研究了很多第三方,但它们看起来非常复杂.任何帮助都会很棒!
hqh*_*qho 28
我建议您查看@angular-material-components/datetime-picker。这是一个类似@angular/material Datepicker 的 DatetimePicker,它添加了对选择时间的支持。
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)
您可以单击占位符的每个部分来设置日,月,年,小时,分钟以及其上午还是下午。
只要从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格式发送到服务器(或至少保存时添加正确的时区)。
| 归档时间: |
|
| 查看次数: |
38746 次 |
| 最近记录: |