Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I project content to mat-table column

Link to stackblitz

I have common components with angular material table (the structure is totally the same). So I want to use ng-content and change some columns:

@Component({
  selector: 'table-basic-example',
  styleUrls: ['table-basic-example.css'],
  templateUrl: 'table-basic-example.html',
})
export class TableBasicExample {
  displayedColumns = ['test', 'name'];
  dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);
}

const ELEMENT_DATA: { name: string } = [
  {name: 'Hydrogen'},
  {name: 'Helium'},
  {name: 'Lithium'},
  {name: 'Beryllium'},
  {name: 'Boron'},
];
<div class="example-container mat-elevation-z8">
  <mat-table #table [dataSource]="dataSource">

    <!-- Position Column -->
    <ng-container matColumnDef="test">
      <mat-header-cell *matHeaderCellDef>Test</mat-header-cell>
      <mat-cell *matCellDef="let element">

        <ng-content select="[test]"></ng-content>

      </mat-cell>
    </ng-container>

    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
    </ng-container>

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

That is why I want to use it like a layout and project some data. So I have another component:

<table-basic-example>

  <div test>
    test
  </div>

</table-basic-example>

But the result is quite strange. I got projection just in the last row.

enter image description here

Link to stackblitz

like image 964
Nataliya U Avatar asked Dec 15 '17 15:12

Nataliya U


1 Answers

You should use ng-template with ngTemplateOutlet. Below worked for me.

Here is why ng-content may not work angulars-content-projection-trap-and-why-you-should-consider-using-template-outlet-instead

@Component({
  selector: 'table-basic-example',
  styleUrls: ['table-basic-example.css'],
  templateUrl: 'table-basic-example.html',
})
export class TableBasicExample {
  @Input() templateRef: TemplateRef<any>;
  displayedColumns = ['test', 'name'];
  dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);
}

const ELEMENT_DATA: { name: string } = [
  {name: 'Hydrogen'},
  {name: 'Helium'},
  {name: 'Lithium'},
  {name: 'Beryllium'},
  {name: 'Boron'},
];

<div class="example-container mat-elevation-z8">
  <mat-table #table [dataSource]="dataSource">

    <!-- Position Column -->
    <ng-container matColumnDef="test">
      <mat-header-cell *matHeaderCellDef>Test</mat-header-cell>
      <mat-cell *matCellDef="let element">

        <!-- using ng-template with ngTemplateOutlet instead of ng-content -->
        <ng-template [ngTemplateOutlet]="templateRef"></ng-template>

      </mat-cell>
    </ng-container>

    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
    </ng-container>

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

<table-basic-example>

  <div test [templateRef]="columnTemplateRef">
    <ng-template #columnTemplateRef>
      test
    </ng-template>
  </div>

</table-basic-example>
like image 122
cjkumaresh Avatar answered Nov 07 '22 03:11

cjkumaresh