Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put buttons in Angular mat-table?

Tags:

angular

I've sucessfully used Angular mat-table to display data from database: enter image description here

<mat-form-field>
  <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Search">
</mat-form-field>

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

  <ng-container matColumnDef="iccid">
    <mat-header-cell *matHeaderCellDef mat-sort-header> ICCID </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.iccid}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="euicc">
    <mat-header-cell *matHeaderCellDef mat-sort-header> EUICC </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.euicc}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="msisdn">
    <mat-header-cell *matHeaderCellDef mat-sort-header> MSISDN </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.msisdn}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="status_paket_data">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Status paket data </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.status_paket_data}}  </mat-cell>
  </ng-container>

  <ng-container matColumnDef="status_sms">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Status SMS </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.status_sms}}  </mat-cell>
  </ng-container>

<ng-container matColumnDef="active_profile">
  <mat-header-cell *matHeaderCellDef mat-sort-header> Active profile </mat-header-cell>
  <mat-cell *matCellDef="let element"> {{element.active_profile}}  </mat-cell>
</ng-container>

<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;" (click) = "rowClicked(row)" ></mat-row>
</mat-table>

<mat-paginator [length]="5" [pageSize]="3" [pageSizeOptions]="[5, 10, 25]">
</mat-paginator>

Now I want to add another column, which contains buttons. So in app.component.ts, I add a new column called actions:

 displayedColumns = ['iccid', 'euicc', 'msisdn', 'status_paket_data', 'status_sms', 'active_profile', 'actions'];

And in app.component.html, I add this before displayedColumns:

<ng-container matColumnDef="actions">
  <mat-header-cell > Actions </mat-header-cell>
  <mat-cell *matCellDef="let row" >
       <button mat-button >Edit</button>
  </mat-cell>
</ng-container>

When the page is refreshed, I get this:

AppComponent.html:5 ERROR TypeError: Cannot read property 'template' of undefined at MatHeaderRowDef.push../node_modules/@angular/cdk/esm5/table.es5.js.CdkHeaderRowDef.extractCellTemplate (table.es5.js:280) at table.es5.js:1452 at Function.from () at MatTable.push../node_modules/@angular/cdk/esm5/table.es5.js.CdkTable._getCellTemplates (table.es5.js:1447) at MatTable.push../node_modules/@angular/cdk/esm5/table.es5.js.CdkTable._renderRow (table.es5.js:1395) at table.es5.js:1289 at Array.forEach () at MatTable.push../node_modules/@angular/cdk/esm5/table.es5.js.CdkTable._forceRenderHeaderRows (table.es5.js:1289)

How to properly add column with buttons in it?

like image 633
anta40 Avatar asked Jun 04 '18 10:06

anta40


People also ask

What is Mat button in angular?

The <mat-button>, an Angular Directive, is used to create a button with material styling and animations. In this chapter, we will showcase the configuration required to draw a button control using Angular Material.


3 Answers

Add the cell column definition matHeaderCellDef for the mat header cell

<ng-container matColumnDef="actions">
  <mat-header-cell  *matHeaderCellDef > Actions </mat-header-cell>
  <mat-cell *matCellDef="let row" >
       <button mat-button >Edit</button>
  </mat-cell>
</ng-container>
like image 74
Sachila Ranawaka Avatar answered Oct 20 '22 20:10

Sachila Ranawaka


This answer helped me a lot. however, I just want to add that in the matColumnDef="actions" you refer to the column actions, which should also be included in the component displayedColumns variable.

displayedColumns = ['id','name','price'...etc., 'actions']
like image 35
Mario Estrada Avatar answered Oct 20 '22 19:10

Mario Estrada


I would like to thank Sachila Ranawaka for his answer. For those using Material 11+, I would only suggest adding the th and td elements, as they render better looking results (border, padding, elevation, etc).

<ng-container matColumnDef="actions">
  <th mat-header-cell  *matHeaderCellDef > Actions </th>
  <td mat-cell *matCellDef="let row" >
    <button mat-button >Edit</button>
  </td>
</ng-container>
like image 4
Stefano Mozart Avatar answered Oct 20 '22 18:10

Stefano Mozart