Hide several rows in Angular Datatable

有些话、适合烂在心里 提交于 2020-12-15 04:37:19

问题


I would like to hide several rows which meet some condition (such as disabled: true).

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;"></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>

ts:

import { Component, OnInit} from '@angular/core';
import {MatTableDataSource} from '@angular/material/table';

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 hide.
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F', disabled: true}, // should be hide.
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne', disabled: true}, // should be hide.
];

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

  ngOnInit(): void {
    this.dataSource.filterPredicate = (data: PeriodicElement, filter: string) => {
      if (data.disabled) {
        return false;
      }

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

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

This is stackblitz demo.

I would like to hide rows which have disabled: true. (In the example above, position 8, 9, 10). I have set dataSource.filterPredicate so that return false when disabled: true.

this.dataSource.filterPredicate = (data: PeriodicElement, filter: string) => {
      if (data.disabled) {
        return false;
      }
      :

This works when filter string is not empty. But when filter string is empty, the position 8, 9, 10 is displayed.

How should I fix this so that the rows which have disabled: true should always be hidden?


回答1:


Self resolved.

I should set [hidden] in HTML.

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


来源:https://stackoverflow.com/questions/64236246/hide-several-rows-in-angular-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!