按特定列过滤 mat-table

Filter mat-table by specific column

我一直在尝试按特定列过滤 mat-table,在我的例子中按“日期”列过滤 table。但它仍然按所有列过滤 table 。指出我应该更改或添加哪些内容才能按预期进行过滤工作。

.component.ts

import { Component, OnInit } from '@angular/core';
import {MatTableDataSource} from "@angular/material/table";
import {AllPlayedMatchesService} from "./all-played-matches.service";

export interface AllMatches {
  team1: string;
  team2: string;
  team1Score: number;
  team2Score: number;
  date: string;
}

@Component({
  selector: 'app-all-played-matches',
  templateUrl: './all-played-matches.component.html',
  styleUrls: ['./all-played-matches.component.css']
})
export class AllPlayedMatchesComponent implements OnInit {

  displayedColumns: string[] = ['club_1', 'club_2', 'club_1_score', 'club_2_score', 'date'];
  dataSource = new MatTableDataSource<AllMatches>();
  columns: string[];

  constructor(private tableService: AllPlayedMatchesService) { }

  ngOnInit(): void {
    this.tableService.getMatches().subscribe((data) => {
      this.dataSource = new MatTableDataSource<AllMatches>(data['response']);
      this.columns = data['response'].date;
    })

    this.dataSource.filterPredicate = function(data, filter: string): boolean {
      return data['response'].date.includes(filter);
    };
  }

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

}

.component.html

<h1>All Played Matches</h1>
<mat-form-field style="padding-right: 20px;">
  <mat-label>Enter date to search</mat-label>
  <input matInput (keyup)="applyFilter($event.target.value)" #input placeholder="eg: 26-12-2020">
</mat-form-field>

<div class="container">
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

    <ng-container matColumnDef="club_1">
        <th mat-header-cell *matHeaderCellDef> CLUB 1 </th>
        <td mat-cell *matCellDef="let element"> {{element.team1}} </td>
    </ng-container>


    <ng-container matColumnDef="club_2">
        <th mat-header-cell *matHeaderCellDef> CLUB 2 </th>
        <td mat-cell *matCellDef="let element"> {{element.team2}} </td>
    </ng-container>


    <ng-container matColumnDef="club_1_score">
        <th mat-header-cell *matHeaderCellDef> CLUB 1 SCORE </th>
        <td mat-cell *matCellDef="let element"> {{element.team1Score}} </td>
    </ng-container>


    <ng-container matColumnDef="club_2_score">
        <th mat-header-cell *matHeaderCellDef> CLUB 2 SCORE </th>
        <td mat-cell *matCellDef="let element"> {{element.team2Score}} </td>
    </ng-container>


    <ng-container matColumnDef="date">
        <th mat-header-cell *matHeaderCellDef> DATE </th>
        <td mat-cell *matCellDef="let element"> {{element.date}} </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    <tr class="mat-row" *matNoDataRow>
      <td class="mat-cell" colspan="4">No matches have been played on "{{input.value}}"</td>
    </tr>
</table>
</div>

当我在 HTML.

中调用 applyFilter($event.target.value) 函数时,它还说“值”未解析

您需要移动指令:this.dataSource.filterPredicate = function(data, filter: string): boolean... 在您的订阅函数中 - 创建数据源后 - 此外,您在定义函数时出错

this.tableService.getMatches().subscribe((data) => {
      this.dataSource = new MatTableDataSource<AllMatches>(data['response']);
      this.columns = data['response'].date;
      //move here
      this.dataSource.filterPredicate = (data:any, filter: string): boolean=> {
          //see that "data" here is the value of the "row"
          return data.date.includes(filter);
      };
    })

注意:我喜欢扁平箭头,但你也可以使用(我按行更改函数数据的参数名称)

      this.dataSource.filterPredicate = function (row:any, filter: string): boolean {
          return row.date.includes(filter);
      };