Last active
February 11, 2020 08:13
-
-
Save omerkaya1/ad754ecf127721bf65c6a6fcea0571aa to your computer and use it in GitHub Desktop.
Create dynamic table out of an arbitrary Object (which may as well be a JSON Object)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table mat-table [dataSource]="tableData" style="width: 100%"> | |
<ng-container [matColumnDef]="col" *ngFor="let col of tableData[0] | keys"> | |
<th mat-header-cell *matHeaderCellDef> {{ col }} </th>s | |
<td mat-cell *matCellDef="let element"> {{ element[col] }} </td> | |
</ng-container> | |
<tr mat-header-row *matHeaderRowDef="tableData[0] | keys"></tr> | |
<tr mat-row *matRowDef="let row; columns: tableData[0] | keys;"></tr> | |
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-custom-tables', | |
templateUrl: './custom-tables.component.html', | |
styleUrls: ['./custom-tables.component.scss'] | |
}) | |
export class CustomTablesComponent implements OnInit { | |
public tableData = [ | |
{A: "test", B: "super", C: "shit"}, | |
{A: "test", B: "super", C: "shit"}, | |
{A: "test", B: "super", C: "shit"}, | |
]; | |
constructor() { } | |
ngOnInit() { | |
// NOTE: Here we should send a request to retrieve the tables' data (any json object will do) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Pipe, PipeTransform} from "@angular/core"; | |
@Pipe({name: 'keys'}) | |
export class KeysPipe implements PipeTransform { | |
transform(value) : any { | |
return Object.keys(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment