Reputation: 3
I currently have a HTML table, that is populated row by row from local storage using a for loop. I was wondering if anyone is able to help me get this working as an Angular Material table. I have tried all the suggestions and code people have recommended on forums & have had no success.
The only pages this components this works with in my example is the add & list component.
Here is a link to my stackblitz project.
Upvotes: 0
Views: 2968
Reputation: 620
The material documentation is quite clear but can always support you in the following example: https://stackblitz.com/angular/xamenaxmandm?file=src%2Fapp%2Ftable-http-example.ts
Upvotes: 0
Reputation: 1986
I am assuming you already have installed angular material in your project.
step 1 :- create interface for you data.
export interface PeriodicElement {
name: string;
position: number;
weight: number;
symbol: string;
}
step 2 :- then in you component you will have to add this.
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource: PeriodicElement[] = [];
displayedColumns represents columns name. step 3 :- then in you oninit method you can assign data to dataSource variable.
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 :- And in html you can display it like that.
<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>
Let me know if you still have doubts.
Updated Code
in list.component.ts
add these two variables.
displayedColumns: string[] = ['ID', 'Product Name', 'Manufacturer', 'Audit Interval', 'Action Buttons'];
dataSource = [];
then in ngOnInit add this.
this.dataSource = this.people;
Then in 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>
After code update I am able to render table, please check attached screenshot.
currently ID is not coming in your data, you can use current index as ID.
Hope this will help.
Upvotes: 1