【发布时间】:2021-07-20 05:51:18
【问题描述】:
我正在使用角度垫表来显示如下所示的数据数组,并进行简单排序。最后一列是一列按钮。我想在每一行中都有不同的按钮来路由到不同的组件。
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
<td mat-cell *matCellDef="let device"> {{device.name}} </td>
</ng-container>
<!-- SerialNo Column -->
<ng-container matColumnDef="serialNo">
<th mat-header-cell *matHeaderCellDef > SerialNo </th>
<td mat-cell *matCellDef="let device"> {{device.serialNo}} </td>
</ng-container>
<!-- Button Column -->
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef > Actions </th>
<td mat-cell *matCellDef="let row" >
<button mat-button routerLinkActive="list-item-active" routerLink="/device1" >View</button>
</td>
我的component.ts是这样的。
export interface Devices {
name: string;
position: number;
serialNo: number;
}
//An Array of device1
const DEVICES: Devices[] = [
{position: 1, name: 'Device 1', serialNo: 135421},
{position: 2, name: 'Device 2', serialNo: 125240},
{position: 3, name: 'Device 3', serialNo: 124350},
{position: 4, name: 'Device 4', serialNo: 124500},
{position: 5, name: 'Device 5', serialNo: 145620},
];
displayedColumns: string[] = ['position', 'name', 'serialNo', 'actions'];
dataSource = new MatTableDataSource(DEVICES);
@ViewChild(MatSort) sort: MatSort;
ngAfterViewInit() {
this.dataSource.sort = this.sort;
}
如何在每一行中添加不同的按钮,使用 routerLink 路由到不同的组件。
【问题讨论】:
标签: arrays angular button angular-material