Deathrattle
Deathrattle

Reputation: 49

Display a nested object in a dynamic material table template

I want to display weight.name in the table. I was able to do it with transforming the data. However, I do not want to manipulate the data in any shape. I tried changing weightto weight.name in the row template ( displayedColumns ) but got back no data at all.

App.component.html

<table mat-table [dataSource]="dataSource">
  <ng-container *ngFor="let col of displayedColumns" [matColumnDef]="col">
    <th mat-header-cell *matHeaderCellDef> {{col}} </th>
    <td mat-cell *matCellDef="let element"> {{element[col]}} </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

App.component.ts

import { Component } from '@angular/core';

export interface PeriodicElement {
  name: string;
  position: number;
  weight: any;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: {name: 5.55}, symbol: 'H'},
  {position: 2, name: 'Helium', weight: {name: 5.55}, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: {name: 5.55}, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: {name: 5.55}, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: {name: 5.55}, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: {name: 5.55}, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: {name: 5.55}, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: {name: 5.55}, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: {name: 5.55}, symbol: 'F'},
  {position: 10, name: 'Neon', weight: {name: 5.55}, symbol: 'Ne'},
];

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  dataSource = ELEMENT_DATA;
}

This is the stackblitz link for reference. https://stackblitz.com/edit/angular-mat-table-dynamic-columns-x36dvr?file=src%2Fapp%2Fapp.component.ts

Upvotes: 4

Views: 6899

Answers (4)

KARTHIKEYAN.A
KARTHIKEYAN.A

Reputation: 20088

we can use use dot(.) notation to refer nested object value from data set.

const lookUpClients = [
    {shortName: 'KA', entityStatus: { code: 'ACTIV'}}
]

<MaterialTable 
    onRowClick={useCallback((event, data) => setSelectedRow(data))}
    options={{
        exportButton: false,
        filtering: false,
        paging: false,
        search: true,
    }}
    columns={[
        { field: 'shortName', title: 'Short Name' },
        { field: 'entityStatus.code', title: 'Status'}, // we can able to mention in the following way to display data
    ]}
    data={lookUpClients}
    title={''}
/>

Upvotes: 0

Prince Francis
Prince Francis

Reputation: 3097

I had faced the similar issue, and I solved it as follows.

  1. Update displayedColumns: string[] = ['position', 'name', 'weight.name', 'symbol'];
  2. Write a method in app.component.ts as follows
findColumnValue = (element:unknown, column:string):string => <string>column.split('.').reduce((acc, cur) => acc[cur], element);
  1. Update html file as follows
<ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
    <th mat-header-cell *matHeaderCellDef mat-sort-header> {{column.split('.')[0]}} </th>
    <td mat-cell *matCellDef="let element"> {{findColumnValue(element, column)}} </td>
</ng-container>
  1. If you are OK with eval method, you could use it as follows
findColumnValue = (element:unknown, column:string):string => eval(`element.${column}`);

Upvotes: 3

Nathan
Nathan

Reputation: 371

You can access nested objects using dot notation in your displayedColumns array

Example:

  displayedColumns: string[] = ['position', 'name', 'weight.name', 'symbol'];

In the table HTML you need to reference the dot notation weight.name in your matColDef

Your HTML:

  <ng-container matColumnDef="weight.name"> 
     <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
     <mat-cell *matCellDef="let row">{{row.weight.name}}</mat-cell>
  </ng-container>

Upvotes: 0

dspies
dspies

Reputation: 1543

Your issue is that you are displaying the weight column as row[col] or rowInstance['weight'] which displays the toString() of the weight object resulting in [object object].

  1. modify your table template to something like:

    <table mat-table [dataSource]="dataSource">
         <ng-container matColumnDef="position"> 
             <mat-header-cell *matHeaderCellDef> Position </mat-header-cell>
             <mat-cell *matCellDef="let row">{{row.position}}</mat-cell>
         </ng-container> 
         <ng-container matColumnDef="name"> 
             <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
             <mat-cell *matCellDef="let row">{{row.name}}</mat-cell>
         </ng-container>
         <ng-container matColumnDef="weightName"> 
             <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
             <mat-cell *matCellDef="let row">{{row.weight.name}}</mat-cell>
         </ng-container>
         <ng-container matColumnDef="symbol"> 
             <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
             <mat-cell *matCellDef="let row">{{row.symbol}}</mat-cell>
         </ng-container>
    
         <mat-header-row *matHeaderRowDef="displayedColumns">
         </mat-header-row>
         <mat-row *matRowDef="let row; columns: displayedColumns;">
         </mat-row>
     </table>
    

and in your ts file, modify the display columns to:

  `displayedColumns: string[] = ['position', 'name', 'weightName', 'symbol'];`

https://stackblitz.com/edit/angular-mat-table-dynamic-columns-w7do4c?file=src/app/app.component.html

  1. transform/flatten your data model upstream to be something like the following and leave your template unchanged.

    [{position: 1, name: 'Hydrogen', weightName: 5.55, symbol: 'H'},...]

Upvotes: 3

Related Questions