英文:
Disable past dates except for single date in mat-datepicker Angular
问题
我想禁用除了 2023 年 4 月 13 日之外的过去日期在 mat-datepicker 中。目前我使用自定义筛选函数创建了日期选择器,以禁用除 2023 年 4 月 13 日之外的过去日期,但这不起作用
myFilter = (date: Date | null): boolean => {
const today = new Date()
if(!date) { return false }
return date >= today && !this.isSameDate(date)
};
isSameDate(date1: Date) {
return date1.toDateString() === this.excludedDate.toDateString()
}
this.excludedDate = new Date(2023, 3, 13)
<input matInput formControlName="effectiveDate" [matDatepicker]="pickerFrom" [matDatepickerFilter]="myFilter"/>
<mat-datepicker-toggle matIconSuffix [for]="pickerFrom"></mat-datepicker-toggle>
<mat-datepicker #pickerFrom ></mat-datepicker>
我不能选择过去的日期,这很好,但我也不能选择 2023 年 4 月 13 日。
这有什么解决办法?
英文:
I want disable past dates except 13-04-2023 in mat-datepicker. Currently i create datepicker with custom filter function to disable past dates except for 13-04-2023 but this dont working:(
myFilter = (date: Date | null): boolean => {
const today = new Date()
if(!date) { return false }
return date >= today && !this.isSameDate(date)
};
isSameDate(date1: Date) {
return date1.toDateString() === this.excludedDate.toDateString()
}
this.excludedDate = new Date(2023, 3, 13)
<input matInput formControlName="effectiveDate" [matDatepicker]="pickerFrom" [matDatepickerFilter]="myFilter"/>
<mat-datepicker-toggle matIconSuffix [for]="pickerFrom"></mat-datepicker-toggle>
<mat-datepicker #pickerFrom ></mat-datepicker>
I cant choose past date, so its nice but i also can't pick 13-04-2023
Whats is the fix for this?
答案1
得分: 1
"matDatepickerFilter
谓词应返回对于启用的日期返回true
。
您的谓词说“日期必须大于或等于今天且不等于excludedDate
”。由于您的excludedDate
在过去,这两个部分是互斥的。
我假设您的目标是类似以下内容:
return date >= today || this.isSameDate(date)`
英文:
The matDatepickerFilter
predicate should return true
for dates that are enabled.
Your predicate says "date have to be greater or equal than today AND not equal excludedDate
". Since your excludedDate
is in the past, those two parts are mutually exclusive.
I assume you were aiming for something like:
return date >= today || this.isSameDate(date)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论