Angular Material 过滤器 Table

Angular Material Filter Table

我正在尝试为 Angular 中的 Material Table 添加过滤器选项,但我收到了这两个错误,它们在 'value' 上指定:

Object is possibly 'null'.
Property 'value' does not exist on type 'EventTarget'.

这是我的 .html 文件:

<mat-form-field>
    <input (keyup)="applyFilter($event.target.value)" matInput placeholder="Filter">
</mat-form-field>

和 .ts 文件:

  applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }

我做错了什么/我应该做些什么不同的事情?

解决方案 1:

添加 id (#filter) 以使用 filter.value

输入和检索值
<input matInput #filter (keyup)="applyFilter(filter.value)" placeholder="Filter">

解决方案 2:

添加另一个函数来处理事件并将值解析为 applyFilter

.component.ts

fireFilterEvent(event: Event) {
  const input = (event.target as HTMLInputElement).value;
  this.applyFilter(input);
}

对于 input 元素,您在 (keyup) 事件上调用 fireFilterEvent($event)

.component.html

<input matInput (keyup)="fireFilterEvent($event)" placeholder="Filter">