0

I'm trying to show the sum at bottom of my table.

I followed exactly the same steps as mentioned in the documentation but still not working.

Below is my template code

<table mat-table [dataSource]="transactions" matSort class="table table-hover">
    <ng-container *ngFor="let columnName of displayedColumns" [matColumnDef]="columnName">
        <th mat-header-cell *matHeaderCellDef mat-sort-header class="text-danger">{{ columnName }}</th>
        <td mat-cell *matCellDef="let row">{{ row[columnName] }}</td>
        <td mat-footer-cell *matFooterCellDef> Total </td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns" (click)="getRecord(row)"></tr>
    <td mat-footer-cell *matFooterCellDef>{{ total }}</td>
</table>

In component

  total = this.transactions.map(t => t.cost).reduce((acc, value) => acc + value, 0);

Below is my working example. Please tell me what Im doing wrong here

https://stackblitz.com/edit/angular-ehc6fn?file=src%2Fapp%2Ftable-footer-row-example.ts

MasterX
  • 81
  • 2
  • 11
  • Have a closer look at the difference in the example and the code you are trying to write. Should be easy. https://stackblitz.com/angular/jampglnqxyyx?file=src/app/table-footer-row-example.html – Nikhil Sahu Apr 17 '20 at 19:36

1 Answers1

1

try with this.

<table mat-table [dataSource]="transactions" class="mat-elevation-z8">
  <!-- Item Column -->
  <ng-container matColumnDef="item">
    <th mat-header-cell *matHeaderCellDef> Item </th>
    <td mat-cell *matCellDef="let transaction"> {{transaction.item}} </td>
    <td mat-footer-cell *matFooterCellDef> Total </td>
  </ng-container>

  <!-- Cost Column -->
  <ng-container matColumnDef="cost">
    <th mat-header-cell *matHeaderCellDef> Cost </th>
    <td mat-cell *matCellDef="let transaction"> {{transaction.cost | currency}} </td>
    <td mat-footer-cell *matFooterCellDef> {{getTotalCost() | currency}} </td>
  </ng-container>

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

check working demo

Updated

<table mat-table [dataSource]="transactions" matSort class="table table-hover">
    <ng-container *ngFor="let columnName of displayedColumns" [matColumnDef]="columnName">
        <th mat-header-cell *matHeaderCellDef mat-sort-header class="text-danger">{{ columnName }}</th>
        <td mat-cell *matCellDef="let row">{{ row[columnName] }}</td>
        <td mat-footer-cell *matFooterCellDef>
          {{columnName === 'item' ? 'Total': getTotalCost()}}
        </td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns" (click)="getRecord(row)"></tr>
    <tr mat-footer-row *matFooterRowDef="displayedColumns"></tr>
</table>

updated demo

Piyush Jain
  • 1,895
  • 3
  • 19
  • 40