Angular Mat Table with Nested objects
See code below and full working example here:
HTML
<table mat-table class="text-list" [dataSource]="dataSource">
<ng-container *ngFor="let column of displayedColumns; let first = first; let i = index" [matColumnDef]="column">
<th mat-header-cell *matHeaderCellDef>{{ column }}</th>
<ng-container *ngIf="first">
<td mat-cell *matCellDef="let row">{{ row[column] }}</td>
</ng-container>
<ng-container *ngIf="!first">
<td mat-cell *matCellDef="let row">|{{ row.Houses[i-1].Purchased }}</td>
</ng-container>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky:true"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
Class
export class AppComponent {
name = 'Angular ' + VERSION.major;
data = [
{
"UserId": "00000000-0000-0000-0000-00000000",
"FullName": "Person one",
"Houses": [
{
"State": "Colorado",
"City": "Denver",
"Code": "C_D",
"Purchased": true
},
{
"State": "Texas",
"City": "Austin",
"Code": "A_D",
"Purchased": true
},
]
},
{
"UserId": "00000000-0000-0000-0000-00000000",
"FullName": "Person Two",
"Houses": [
{
"State": "Colorado",
"City": "Denver",
"Code": "C_D",
"Purchased": true
},
{
"State": "Texas",
"City": "Austin",
"Code": "A_D",
"Purchased": false
},
]
}
];
displayedColumns = ['FullName', 'Denver', 'Austin'];
dataSource = new MatTableDataSource<any>([]);
ngOnInit(): void {
this.dataSource.data = this.data;
}
How do I bind angular material table values apart from their names?
You can do something like this
columnsToDisplay2 = [
{ label: 'Weird name label', value: 'name' },
{ label: 'Weird weight label', value: 'weight' },
{ label: 'Weird symbol label', value: 'symbol' },
{ label: 'Weird position label', value: 'position' },
];
and on the the html:
<ng-container
matColumnDef="{{column.value}}"
*ngFor="let column of columnsToDisplay2"
>
<th mat-header-cell *matHeaderCellDef>{{column.label}}</th>
<td mat-cell *matCellDef="let element">{{element[column.value]}}</td>
</ng-container>
Here is a stackblitz with it running https://stackblitz.com/edit/angular-6b8rmt?file=src%2Fapp%2Ftable-expandable-rows-example.html
Property 'table' has no initializer and is not definitely assigned in the constructor
Table is not initialized, only typed for the moment. So to clean this error, you can add a "!" to the property to say to the linter: " Hey, i know it does not hav a value yet, but it will be done later.
@ViewChild('dataTable') table!: MatTable<PeriodicElement>;
Render html in Angular Material Table cell
You can use innerHTML
attribute binding
<td mat-cell
*matCellDef="let element"
[innerHTML]="element.name">
</td>
TS
const ELEMENT_DATA: PeriodicElement[] = [
{
position: 1,
name: '<b>Hydrogen</b> <br>Test',
...
},
...
];
Output
Position | Name |
---|---|
1 | Hydrogen Test |
Related Topics
Filter: Progid:Dximagetransform.Microsoft.Gradient Is Not Working in Ie7
How to Get The Cursor to Change to The Hand When Hovering a <Button> Tag
CSS3 Transitions Want to Add a Colour and Fade It Away
Using Proper CSS Media Queries in Angular
Background Image Not Showing Up in Heroku
How to Position a Background Image in Reveal.Js
Make Bootstrap Carousel Responsive on Height
How to Keep The Width of The Parent Element When Position: Fixed Is Applied
Why Won't Opera (11.00) Display Custom (@Font-Face) Fonts
Twitter Bootstrap & Google Maps
How to Increase Bootstrap 3 Navbar Height While Keeping Menu Height Small When Collapsed
Rails Bootstrap How to Format Form_For (Width Grid Collapses)
Slide Out Panel with Bootstrap
Adding CSS Class to Multiple Elements
Image Paths in CSS to Support Cdn
Defining Keyframe Animations Inside Media Queries
Bootstrap Container with Position:Absolute Loses Layout Inside