filterPredicate带有用于角形材料表的空过滤器字符串

时间:2020-10-15 05:52:30

标签: angular angular-material

我想让filterPredicate使用空的过滤器字符串。

html:

<mat-form-field>
    <mat-label>Filter</mat-label>
    <input matInput (keyup)="applyFilter($event)" placeholder="Ex. ium" #input>
</mat-form-field>

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

        <!-- Position Column -->
        <ng-container matColumnDef="position">
            <th mat-header-cell *matHeaderCellDef> No. </th>
            <td mat-cell *matCellDef="let element"> {{element.position}} </td>
        </ng-container>

        <!-- Name Column -->
        <ng-container matColumnDef="name">
            <th mat-header-cell *matHeaderCellDef> Name </th>
            <td mat-cell *matCellDef="let element"> {{element.name}} </td>
        </ng-container>

        <!-- Weight Column -->
        <ng-container matColumnDef="weight">
            <th mat-header-cell *matHeaderCellDef> Weight </th>
            <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
        </ng-container>

        <!-- Symbol Column -->
        <ng-container matColumnDef="symbol">
            <th mat-header-cell *matHeaderCellDef> Symbol </th>
            <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
        </ng-container>

        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;" [hidden]="row.disabled == true"></tr>

        <!-- Row shown when there is no matching data. -->
        <tr class="mat-row" *matNoDataRow>
            <td class="mat-cell" colspan="4">No data matching the filter "{{input.value}}"</td>
        </tr>
    </table>

    <div [hidden]="dataSource.paginator == null">
        <mat-paginator pageSize="10" [pageSizeOptions]="[3, 5, 10]" showFirstLastButtons></mat-paginator>
    </div>

ts:

import { Component, OnInit, AfterViewInit, ViewChild } from "@angular/core";
import { MatTableDataSource } from "@angular/material/table";
import { MatPaginator } from "@angular/material/paginator";

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
  disabled: boolean;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H', disabled: false},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He', disabled: false},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li', disabled: false},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be', disabled: false},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B', disabled: false},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C', disabled: false},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N', disabled: false},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O', disabled: true},  // should be hidden.
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F', disabled: true}, // should be hidden.
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne', disabled: true}, // should be hidden.

];

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit, AfterViewInit {
  displayedColumns: string[] = ["position", "name", "weight", "symbol"];
  dataSource = new MatTableDataSource(ELEMENT_DATA);
  @ViewChild(MatPaginator) paginator!: MatPaginator;

  ngOnInit(): void {

    // This does not run in case filter is empty.
    this.dataSource.filterPredicate = (
      data: PeriodicElement,
      filter: string
    ) => {
      if (data.disabled) {
        return false;
      }

      if (data.name.toLowerCase().indexOf(filter) > -1) {
        return true;
      }
      return false;
    };
  }

  ngAfterViewInit(): void {
    this.dataSource.paginator = this.paginator;
  }

  applyFilter(event: Event) {
    const filterValue = (event.target as HTMLInputElement).value;
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }
}

我想隐藏具有disabled:true的行(在上面的示例中,位置8、9、10)。

在我的previous post中,我首先尝试设置filterPredicate。但这在过滤器字符串为空的情况下不起作用。因此,我在html中设置了[hidden]参数,如下所示。

<tr mat-row *matRowDef="let row; columns: displayedColumns;" [hidden]="row.disabled == true"></tr>

看起来不错,但是我有问题。

当表格显示带有分页器时,分页器的行数与显示的行数不匹配。

enter image description here

这是Stackblitz sample。显示了7行,但分页器的行数为10。

我想如果filterPredicate可以与空过滤字符串一起应用,一切都会很好。

我有没有办法让filterPredicate即使是空字符串也可以工作?还是对此问题有其他解决方案?

0 个答案:

没有答案