材料日期选择器过滤

问题描述 投票:0回答:2

我正在尝试让过滤工作。我修改了sample,并且无法弄清楚为什么这不起作用。我想禁用所有日子,除了testDates数组中的那些日子。 “找到有效日期”已正确注销,但日期不可选。

  myFilter = (d: Date): boolean => {
    const testDates: Date[] = [
      new Date('2018-08-30T00:00:00+02:00'),
      new Date('2018-08-28T00:00:00+02:00'),
      new Date('2018-08-21T00:00:00+02:00'),
      new Date('2018-08-23T00:00:00+02:00')
    ]
    testDates.forEach(item => {
      if (item.toDateString() == d.toDateString()) {
        console.debug("found valid day:" + d);
        return true;
      }
    });
    console.debug("invalid day:" + d);
    return false;
  }

这是在stackblitz:https://stackblitz.com/edit/angular-6fgvsx?embed=1&file=app/datepicker-filter-example.ts

angular datepicker angular-material
2个回答
2
投票

使用myFilter作为以下代码:

myFilter = (d: Date): boolean => {
    const testDates: Date[] = [
      new Date('2018-08-30T00:00:00+02:00'),
      new Date('2018-08-28T00:00:00+02:00'),
      new Date('2018-08-21T00:00:00+02:00'),
      new Date('2018-08-23T00:00:00+02:00')
    ]

    return testDates.findIndex(testDate => d.toDateString() == testDate.toDateString()) >= 0;
  }

1
投票

将返回值保存到变量,然后它将起作用。

let x = false;
testDates.forEach(item => {
    if (item.toDateString() == d.toDateString()) {
        x = true;
    }
})
return x;

StackBlitz Example

© www.soinside.com 2019 - 2024. All rights reserved.