Skip to content

Instantly share code, notes, and snippets.

@omerkaya1
Last active February 11, 2020 08:13
Show Gist options
  • Save omerkaya1/ad754ecf127721bf65c6a6fcea0571aa to your computer and use it in GitHub Desktop.
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)
<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>
@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)
}
}
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