3

I have this column config for material table:

columns = [
    { columnDef: 'firstName', header: 'FirstName',    cell: (user) => `${user.firstName}` },
    { columnDef: 'lastName',     header: 'LastName',   cell: (user) => `${user.lastName}`     },
    { columnDef: 'mobile',   header: 'Mobile', cell: (user) => `${user.mobile}`   },
    { columnDef: 'email',   header: 'Email', cell: (user) => `${user.email}`   },
  ];

And i want to make mat table throught ngFor cycle, like this:

<mat-table #table [dataSource]="dataSource">

    <ng-container *ngFor="let column of columns" [cdkColumnDef]="column.columnDef">
      <mat-header-cell *cdkHeaderCellDef>{{ column.header }}</mat-header-cell>
      <mat-cell *cdkCellDef="let row">{{ column.cell(row) }}</mat-cell>
    </ng-container>
    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
  </mat-table>

Everything is fine, but what if i want to pass some html template or component in table cell, for example if i want column, where will be button to open user-edit dialog.

I've tried in this way:

  <mat-cell *cdkCellDef="let row" [innerHTML]="<test-component></test-component> | safeHTML"></mat-cell>

and the component:

@Component({
  selector: 'test-component',
  template: `  
      <button (click)="openEditDialog()">test</button>
  `,
})
export class TestComponent implements OnInit {

And it works, but i can't use data binding and, for example put some data to component through @Input. What am doing wrong or maube there is another approach to achieve it? Would be appreciate for any advice)

Alex Bryanskiy
  • 395
  • 1
  • 3
  • 19
  • 1
    Ran into this same problem and I was able to figure it out using the method I went through [here](https://stackoverflow.com/questions/53505088/creating-instance-of-component-and-passing-to-another-component-rendering-as-ob/53562026#53562026) – TabsNotSpaces Nov 30 '18 at 17:16
  • check this out, it might help you: https://stackblitz.com/edit/dynamic-columns-mat-table?file=app%2Ftable-pagination-example.ts – IsraGab May 20 '21 at 20:51

0 Answers0