禁用过去的日期,除了在mat-datepicker Angular中选择单个日期。

huangapple go评论60阅读模式
英文:

Disable past dates except for single date in mat-datepicker Angular

问题

我想禁用除了 2023 年 4 月 13 日之外的过去日期在 mat-datepicker 中。目前我使用自定义筛选函数创建了日期选择器,以禁用除 2023 年 4 月 13 日之外的过去日期,但这不起作用 禁用过去的日期,除了在mat-datepicker Angular中选择单个日期。

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 日。禁用过去的日期,除了在mat-datepicker Angular中选择单个日期。

这有什么解决办法?

英文:

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 =&gt; {
    const today = new Date()
    if(!date) { return false }

    return date &gt;= today &amp;&amp; !this.isSameDate(date)
  };

  isSameDate(date1: Date) {
    return date1.toDateString() === this.excludedDate.toDateString()
  }

  this.excludedDate = new Date(2023, 3, 13)


  &lt;input matInput formControlName=&quot;effectiveDate&quot; [matDatepicker]=&quot;pickerFrom&quot; [matDatepickerFilter]=&quot;myFilter&quot;/&gt;
  &lt;mat-datepicker-toggle matIconSuffix [for]=&quot;pickerFrom&quot;&gt;&lt;/mat-datepicker-toggle&gt;
  &lt;mat-datepicker #pickerFrom &gt;&lt;/mat-datepicker&gt;

I cant choose past date, so its nice but i also can't pick 13-04-2023

禁用过去的日期,除了在mat-datepicker Angular中选择单个日期。

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 &gt;= today || this.isSameDate(date)

huangapple
  • 本文由 发表于 2023年5月17日 20:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定