【问题标题】:Filter mat-table by specific column按特定列过滤 mat-table
【发布时间】:2021-04-05 02:10:52
【问题描述】:

我一直在尝试按特定列过滤 mat-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) 函数时,它还说“值”未解析。

【问题讨论】:

    标签: angular filter angular-material mat-table


    【解决方案1】:

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

    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);
          };
    

    【讨论】:

      猜你喜欢
      • 2022-09-30
      • 1970-01-01
      • 2019-10-10
      • 2018-07-08
      • 2019-06-22
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      • 2019-12-26
      相关资源
      最近更新 更多