DatePickerView选择器
DatePickerView 的功能类似于 DatePicker ,但它是直接渲染在区域中,而不是弹出窗口。
代码演示
选择器
import { Component } from '@angular/core';
@Component({
selector: 'demo-date-picker-view-basic',
template: `
<div class="sub-title">Start datetime {{ getDate(value1) }}</div>
<DatePickerView [(ngModel)]="value1" (onValueChange)="onValueChange($event)"></DatePickerView>
`,
styles: [
`
.sub-title {
margin: 8px;
}
`
]
})
export class DemoDatePickerViewBasicComponent {
name1 = '选择';
name2 = '选择';
mode = 'date';
value1 = new Date(2018, 8, 15, 8, 0);
value2 = new Date();
currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM:ss'): any {
const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
return format
.replace('yyyy', date.getFullYear())
.replace('mm', pad(date.getMonth() + 1))
.replace('dd', pad(date.getDate()))
.replace('HH', pad(date.getHours()))
.replace('MM', pad(date.getMinutes()))
.replace('ss', pad(date.getSeconds()));
}
getDate(result) {
let format = 'yyyy-mm-dd';
if (this.mode === 'year') {
format = 'yyyy';
} else if (this.mode === 'month') {
format = 'yyyy-mm';
} else if (this.mode === 'date') {
format = 'yyyy-mm-dd';
} else if (this.mode === 'datetime') {
format = 'yyyy-mm-dd HH:MM';
} else {
format = 'HH:MM';
}
this.value1 = result;
return this.currentDateFormat(result, format);
}
onValueChange(event) {
console.log(event);
}
formatIt(date: Date, form: string) {
const pad = (n: number) => (n < 10 ? `0${n}` : n);
const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
if (form === 'YYYY-MM-DD') {
return dateStr;
}
if (form === 'HH:mm') {
return timeStr;
}
return `${dateStr} ${timeStr}`;
}
}
API
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
[mode] | 日期选择的类型 | ‘year’ | ‘month’ | ‘date’ | ‘time’ | ‘datetime’ | ‘date’ |
[minDate] | 最小可选日期 | Date | 2000-1-1 |
[maxDate] | 最大可选日期 | Date | 2030-1-1 |
[locale] | 国际化,可覆盖全局LocaleProvider 的配置 | {DatePickerLocale: {year, month, day, hour, minute, am?, pm?}, okText, dismissText } | - |
[disabled] | 是否不可用 | boolean | false |
[indicatorStyle] | indicator的样式 | object | - |
[showErrorToast] | 显示Toast错误信息 | boolean | true |
[showErrorToastInterval] | Toast错误信息显示时间 | number | 2000 |
[(ngModel)] | 当前选中时间 | Date | new Date() |
(onChange) | 时间发生变化的回调函数 | EventEmitter<{date: object}> | - |
(onValueChange) | 每列 picker 改变时的回调 | EventEmitter<{date: object, index: string}> | - |