在 Angular 数据表中隐藏几行 [英] Hide several rows in Angular Datatable
问题描述
我想隐藏满足某些条件(例如disabled: true
)的几行.
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();
}
}
这是stackblitz演示.
我想隐藏 disabled: true
的行.(在上面的示例中,位置 8、9、10).我已经设置了 dataSource.filterPredicate
以便在 disabled: true
时返回 false
.
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;
}
:
这在过滤器字符串不为空时有效.但是当过滤字符串为空时,则显示位置8、9、10.
This works when filter string is not empty. But when filter string is empty, the position 8, 9, 10 is displayed.
我应该如何解决这个问题,以便始终隐藏 disabled: true
的行?
How should I fix this so that the rows which have disabled: true
should always be hidden?
推荐答案
自行解决.
我应该在 HTML 中设置 [hidden]
.
I should set [hidden]
in HTML.
<tr mat-row *matRowDef="let row; columns: displayedColumns;" [hidden]="row.disabled == true"></tr>
这篇关于在 Angular 数据表中隐藏几行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!