我当前有一个 HTML 表,它是使用 for 循环从本地存储中逐行填充的。我想知道是否有人能够帮助我将其作为 Angular Material 表工作。我已经尝试了人们在论坛上推荐的所有建议和代码,但没有成功。
在我的示例中,该组件唯一使用的页面是添加和列表组件。
最佳答案
我假设您已经在项目中安装了 Angular Material 。
第 1 步:- 为您的数据创建接口(interface)。
export interface PeriodicElement {
name: string;
position: number;
weight: number;
symbol: string;
}
第 2 步:- 然后在您的组件中您必须添加此内容。
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource: PeriodicElement[] = [];
displayedColumns 表示列名称。 步骤 3:- 然后在 oninit 方法中,您可以将数据分配给 dataSource 变量。
this.dataSource = [
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];
stpe 4 :- 在 html 中你可以这样显示它。
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- 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>
</table>
如果您仍有疑问,请告诉我。
更新代码
在list.component.ts中
添加这两个变量。
displayedColumns: string[] = ['ID', 'Product Name', 'Manufacturer', 'Audit Interval', 'Action Buttons'];
dataSource = [];
然后在 ngOnInit 中添加这个。
this.dataSource = this.people;
然后在list.component.html
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- ID Column -->
<ng-container matColumnDef="ID">
<th mat-header-cell *matHeaderCellDef> ID </th>
<td mat-cell *matCellDef="let element"> 1 </td>
</ng-container>
<!-- Product Name Column -->
<ng-container matColumnDef="Product Name">
<th mat-header-cell *matHeaderCellDef> Product Name </th>
<td mat-cell *matCellDef="let element"> {{element.fName}} </td>
</ng-container>
<!-- Manufacturer Column -->
<ng-container matColumnDef="Manufacturer">
<th mat-header-cell *matHeaderCellDef> Manufacturer </th>
<td mat-cell *matCellDef="let element"> {{element.lName}} </td>
</ng-container>
<!-- Audit Interval Column-->
<ng-container matColumnDef="Audit Interval">
<th mat-header-cell *matHeaderCellDef> Audit Interval </th>
<td mat-cell *matCellDef="let element"> {{element.aDays}} </td>
</ng-container>
<!-- Action Column-->
<ng-container matColumnDef="Action Buttons">
<th mat-header-cell *matHeaderCellDef> Action Buttons </th>
<td mat-cell *matCellDef="let element">
<button mat-button mat-raised-button color="accent" [routerLink]="['/edit', i]">edit </button>
<button mat-button mat-raised-button color="warn" (click) = openDialog(i) > delete </button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
代码更新后,我可以渲染表格,请检查随附的屏幕截图。
关于javascript - 将 html 表转换为 mat 表( Angular ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61304938/