有没有一种方法可以自定义角度日期选择器? [英] Is there a way to customize angular date picker?
问题描述
我需要自定义角度材料日期选择器的帮助,即 https://material.angular.io/components/datepicker/overview .一旦单击日期,日历就不会关闭.我应该能够有2个按钮,一个是取消",另一个是保存".单击保存"按钮后,应关闭日历.
I need help in customizing the angular material date picker i.e,https://material.angular.io/components/datepicker/overview. The calendar should not close once we click on the date. I should be able to have 2 buttons, one is 'cancel' and another is 'save'. Once the save button is clicked, the calendar should be closed.
推荐答案
I don't know if it's a better solution, but based on this SO answer, I think that the solution is use a menu with a calendar inside.
当我们需要输入时,我们至少需要两个函数,一个函数将输入的值解析为Date对象,另一个函数是在拥有Date对象时将输入的值格式化.
As we need an input we need at least two functions, one to parse the value of the input to a Date Object, and another to format the value of the input when we has a Date Object.
我希望这个特殊"日期选择器可以用几种方式格式化日期,YYYY-MM-DD和DD/MM/YYYY我需要树变量
As I want this "special" datepicker can format the date in severals ways, YYYY-MM-DD and DD/MM/YYYY I need tree variables
placeHolder:string="DD/MM/YYYY"
separator:string;
order:number[]=[];
一个函数给我们分隔符和顺序"的值
A function give us the value of "separator and order"
init(placeholder:string)
{
this.separator=placeholder.replace(/[YMD]/g,'').substr(0,1)
const parts=placeholder.replace(/[.\/]/g,'-').split('-')
this.order[0]=parts.indexOf('YYYY')
this.order[1]=parts.indexOf('MM')
this.order[2]=parts.indexOf('DD')
}
所以,我们的函数解析和格式变得像
So, our function parse and formats becomes like
format(date:any)
{
if (!date)
return null;
const parts=[''+date.getFullYear(),
("00" + (date.getMonth() + 1)).slice(-2),
("00" + date.getDate()).slice(-2)]
return parts[this.order[0]]+this.separator+
parts[this.order[1]]+this.separator+
parts[this.order[2]]
}
parse(value:string)
{
const parts=value?value.replace(/[.\/]/g,'-').split('-'):[]
const date:any=(parts.length==3)?
new Date(parts[this.order[0]]+'-'+parts[this.order[1]]+'-'+parts[this.order[2]]):
null
return date && date.getTime && date.getTime()?date:null
}
(模糊)重新设置值时,辅助功能仍在我们身旁
An auxiliar function aloow us in (blur) reformatear the value
tryFormat(value:string)
{
const date=this.parse(value)
this.date=date?this.format(date):value
}
还有一个功能,当菜单"打开时,允许更改在ViewChild中获得的日历的活动日期
And a function when the "menu" is open, allow change the active date of the calendar that get in a ViewChild
@ViewChild('calendar',{static:false}) calendar:any
onOpen()
{
if (this.date)
{
const date=this.parse(this.date);
if (date)
{
this.calendar.activeDate=date;
}
}
}
最后,.html就像
<mat-form-field class="example-full-width">
<mat-label>Date</mat-label>
<input matInput placeholder="{{placeHolder}}" [(ngModel)]="date" (blur)="tryFormat(date)" >
<button matSuffix mat-icon-button [matMenuTriggerFor]="appMenu" (menuOpened)="onOpen()">
<mat-icon>calendar_today</mat-icon>
</button>
</mat-form-field>
<mat-menu #appMenu="matMenu" class="drop-calendar" >
<div (click)="$event.stopPropagation()">
<mat-calendar #calendar
(selectedChange)="date=format($event)"
[selected]="parse(date)">
</mat-calendar>
</div>
</mat-menu>
完整代码位于 查看全文