angular - 如何动态显示/隐藏 mat-table 中的某些行?
问题描述
我在我的 Angular 应用程序中使用了 mat-table 组件,其中显示了一些体育赛事的结果。通常,我只想显示三个最佳结果,然后,当用户单击“省略号按钮”(如在相关图片上)时 - 表格应展开并显示其余隐藏行。我在标记中使用 *ngIf 语句时遇到问题,因为我不允许有多个带有 * 前缀的属性。如何以不同的方式做到这一点?提前致谢。
<table mat-table [dataSource]="qualifyingResults" class="exo-2">
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef>POS</th>
<td mat-cell *matCellDef="let result">{{ result.position }}</td>
</ng-container>
<ng-container matColumnDef="driver">
<th mat-header-cell *matHeaderCellDef class="center-header">DRIVER</th>
<td mat-cell *matCellDef="let result" class="text-center font-weight-bold">{{ result.Driver.code }}</td>
</ng-container>
<ng-container matColumnDef="time">
<th mat-header-cell *matHeaderCellDef>TIME</th>
<td mat-cell *matCellDef="let result">{{ result.time }}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
解决方案
你可以很容易地做到这一点。在您的数据源中,仅获取前 3 个结果。然后将省略号按钮单击事件绑定到获取其余数据的函数。然后更新数据源。
像这样的东西:
dataSource: MatTableDataSource<any[]>;
ngOnInit() {
this.getThreeResults.subscribe(
data => this.dataSource = new MatTableDataSource(data)
)
}
onEllipsisButtonClick() {
this.getRestOfTheData.subscribe(
data => this.dataSource.data = data
)
}
推荐阅读
- python - 将两个 pygame.Color 对象混合在一起
- android - 用 diffUtils 更新列表的最佳方法是什么
- ubuntu - 在 UBUNTU 16.04 上安装 jenkins 时遇到错误列表
- java - 如何修复“关系”
不存在”错误,即使使用 'IF EXISTS-THEN' pgsql 块? - python - 通过正则表达式获取两个模式之间的所有行
- batch-file - 如何将数千个图像文件移动到每个包含 400 个图像文件的子文件夹中?
- android - 为 IntentServices 定义意图的最简洁方法?
- debugging - 在“while-no-input”中调用的调试函数
- swiftui - WatchOS8 中的 SwiftUI NavigationLink
- python - 如何将熊猫数据框列中的所有值转换为具有计数的新列?