Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use "data-label" in mat-table if I use *ngFor and directives?

So, I have a problem with using data-label in my mat-table. I have a table with lots of columns with data and want to make it responsive. I google how can I make it and found that I can use data-label attribute. The problem is that (as I understand) the data-label can't support angular {{code}}.

It works only like this (static data only). If I'm trying to write something in curly braces in console I have an exception "Can't bind to 'label' since it isn't a known property of 'mat-cell'. 1. If 'mat-cell' is an Angular component and it has 'label' input, then verify that it is part of this module. 2. If 'mat-cell' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message."

Can someone explain me how to resolve this problem, thank you!

static data here working

<div class="example-header" fxLayout="row">
  <mat-form-field>
    <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
  </mat-form-field>
</div>
<div class="example-container mat-elevation-z8">
  <mat-table [dataSource]="dataSource" matSort>
    <!-- ID Column -->
    <ng-container matColumnDef="id">
      <mat-header-cell *matHeaderCellDef mat-sort-header> ID </mat-header-cell>
      <mat-cell *matCellDef="let row" data-label="id"> {{row.cnt}} </mat-cell>
    </ng-container>
    <!-- Progress Column -->
    <ng-container matColumnDef="progress">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Progress </mat-header-cell>
      <mat-cell *matCellDef="let row" data-label="progress"> {{row.progress}}% </mat-cell>
    </ng-container>
    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Name </mat-header-cell>
      <mat-cell *matCellDef="let row" data-label="name"> {{row.name}} </mat-cell>
    </ng-container>
    <!-- Color Column -->
    <ng-container matColumnDef="color">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Color </mat-header-cell>
      <mat-cell *matCellDef="let row" [style.color]="row.color" data-label="color"> {{row.color}} </mat-cell>
    </ng-container>
    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;">
    </mat-row>
  </mat-table>
  <mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
</div>
like image 297
MargeKh Avatar asked Oct 21 '25 12:10

MargeKh


1 Answers

it will solve your problem:

<mat-cell *matCellDef="let row" [attr.data-label]="row"> {{element[row]}}</mat-cell>
like image 87
Fuad Avatar answered Oct 23 '25 04:10

Fuad