Arter
Arter

Reputation: 2324

Angular material *ngFor inside <mat-cell> for search

is there any way to make *ngFor inside <mat-cell>?

I need to add a new column in material and hide it, I use it only for filtering purpose...

I have a table like this

<ng-container matColumnDef="email">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Email </mat-header-cell>
    <mat-cell *matCellDef="let user"> {{ user.email }} </mat-cell>
  </ng-container>
  <ng-container matColumnDef="phone">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Phone </mat-header-cell>
    <mat-cell *matCellDef="let user"> {{ user.phone }} </mat-cell>
  </ng-container>

But now I need another row with {{ user.book.name}}, and this way, of course, not working. And I need something like this

<ng-container *ngFor="let book of user.book" [matColumnDef]="book">
   <th mat-header-cell *matHeaderCellDef mat-sort-header> {{ book}} </th>
   <td mat-cell *matCellDef="let book"> {{ book.name }} </td>
</ng-container>

But this not working, I broke all my app with this...

here is an example on stackblitz, here where is OBJECT I need to get books names

Upvotes: 0

Views: 3117

Answers (2)

Akber Iqbal
Akber Iqbal

Reputation: 15031

Had to do 2 things - create a new array, where we stored the array of books as a string - hide this column in the HTML (you can remove the display:none to test

relevant HTML:

<!-- Books Column -->
  <ng-container matColumnDef="books" >
    <th mat-header-cell *matHeaderCellDef style='display:none'> Books </th>
    <td mat-cell *matCellDef="let element" style='display:none'> {{ element.books }}  </td>
  </ng-container>

relevant TS:

import {Component} from '@angular/core';
import {MatTableDataSource} from '@angular/material/table';

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

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H', books: [{name: "book4"},{name: "book2"}, {name: "book3"}]},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He', books: [{name: "book6"},{name: "book2"}, {name: "book3"}]},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li', books: [{name: "book5"},{name: "book2"}, {name: "book3"}]},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be', books: [{name: "book7"},{name: "book2"}, {name: "book3"}]},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
];

/**
 * @title Table with filtering
 */
@Component({
  selector: 'table-filtering-example',
  styleUrls: ['table-filtering-example.css'],
  templateUrl: 'table-filtering-example.html',
})
export class TableFilteringExample {
  newVar: PeriodicElement[] =[];
  constructor(){
    for(var i=0;i<ELEMENT_DATA.length; i++){
      this.newVar.push(
      {position: ELEMENT_DATA[i].position, name: ELEMENT_DATA[i].name, weight: ELEMENT_DATA[i].weight, symbol: ELEMENT_DATA[i].symbol, books: [JSON.stringify(ELEMENT_DATA[i].books)] }
      );
    }
  console.log(this.newVar);
  }

  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol', 'books'];
  dataSource = new MatTableDataSource(this.newVar);

  applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }
}

complete working stackblitz here

Upvotes: 1

StepUp
StepUp

Reputation: 38094

You can use *ngFor to iterate through your array and format as you want:

<td mat-cell *matCellDef="let element">
   <span *ngFor="let person of element.books;"> 
     {{ person?.name }}
  </span>
</td>

Please, see a workblitz example

Upvotes: 1

Related Questions