1

Currently each column contains a single value from a single property of JSON. I want to concatenate several of these property values in a single column in an Angular Material Table. How should I approach this?

1 Answer 1

1

You need to create a custom field called name in the TS file , where name is the combination of firstName and lastName.

Then specify {{element.firstName}} {{element.lastName}} in the HTML file.

Example:

In TS file:

displayedColumns: string[] = ['name'];

In HTML file:

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef>Name</th>
    <td mat-cell *matCellDef="let element">{{element.firstName}}{{element.lastName}}</td>
  </ng-container>
  
  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  
 </table>
Sign up to request clarification or add additional context in comments.

3 Comments

how do you combine the properties into a custom field?
In TS file specify a custom name , then specify that name in HTML under <ng-container matColumnDef="name"> and then under ng-container tag add <td mat-cell *matCellDef="let element"> {{element.firstName}} }{{element.lastName}} </td> . This would do the trick.
Oh I mean in the TS file, so I just declare a name variable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.