Footer Cell isn't showing up as expected within *ngFor loop in Mat-Table

I'm having trouble displaying the total sum at the bottom of my table.

Despite following the steps outlined in the documentation exactly, it still doesn't seem to be working for me.

Below you can find the code from my template:

<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>

This is how I have implemented the calculation in my component:

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

Unfortunately, the total sum is not appearing as expected. Here is a link to a working example that might help pinpoint the issue:

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

Answer №1

give this a try.

<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>

view the live demo

Latest Update

<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 link

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to retrieve the following element in a JavaScript array

I am a beginner in JavaScript and I am attempting to access the next element of an array using an onclick function but so far I have not been successful. var i, len; function quiz() { var quiz_questions = [ "who is the founder of Fa ...

Issue with Initializing MongoDB Database

Attempting to start a MongoDB database server is resulting in an error where the server fails to start. To address this, I have executed the mkdir -p data/db command in the project directory. The command I am running is: mongod --dbpath=data/ --port 27017 ...

React redux - unable to load store without encountering any errors

I am currently working on a project inspired by the code example in the Learning React book, which focuses on using react redux and react router for a single page application. You can find my project here, where I have tried to replicate the example. To r ...

Ways to retrieve data from a URL and pass it to JavaScript code

I am currently experiencing difficulties in obtaining the values from an object requested through a link on my website. The issue arises from the fact that I have created a PHP method to retrieve data from the database for the values of a particular objec ...

What is the best way to display PHP files as the main landing page on my Express server?

I am currently using php for my home page, but I have come to realize that Node.js does not support .php files. So, I'm looking for a solution on how to address this issue. Below is the code I am using: var app = require('express')(); var h ...

Select state and city options similar to the national breakdown page

I'm attempting to replicate a state and city selection box similar to the one on the NTTS Breakdown website. You can see it in action here: When you select a state on the left side of the webpage, the city selection box displays "loading data" and th ...

What causes my ready function to consistently execute upon controller or directive reinitialization?

Every time my controller or directive reinisilize, the angular.element(document).ready(function(){...}); function is executed. Why does this happen? In my scenario, I have two controllers and one directive. I update a value in the parent controller which ...

What are the best ways to stop jQuery events from propagating to ancestor elements?

I have a collection of nested UL's that follow this structure: <ul class="categorySelect" id=""> <li class="selected">Root<span class='catID'>1</span> <ul class="" id=""> <li>First Cat<span ...

Share your ES module (.mjs) on NPMJS with support for Node versions older than 8.5.0 (Dual Package)

In the past, publishing a module written in ES6 to NPMJS was a simple process: transpile the code using Babel and publish the resulting `lib` directory to NPMJS while keeping the original `src` files on GitHub. However, with Node v8.5.0's experimenta ...

Implementing HTML page authentication with Identity ADFS URL through JavaScript

I have a basic HTML page that displays customer reports using a JavaScript function. The JavaScript makes an ajax call to retrieve the reports from a backend Spring REST API. In the Spring REST API, I have set up an endpoint "/api/saml" for authentication ...

Error: We are facing an issue with new.mongoose.Schema as it is not functioning properly and

I am experiencing an issue with my product.js file. It keeps throwing an error about an unidentified identifier, and whenever I try to fix one problem, another one pops up. I have been struggling to locate the root cause of this error. ...

Fixed-top navigation bar in Bootstrap

Currently working on constructing a Single Page Application (SPA) using Bootstrap, where the navbar is fixed-top. Encountering an issue where content gets cut off by the navbar unless setting the element margin-top to 58px. Although using the specified sty ...

TypeScript stack trace not displayed in Angular stack trace when in development mode

Currently, I am working on a project using a 'fuse angular template' in order to speed up development and avoid the need to write numerous components. To get started, clone the repository from https://github.com/deanhiller/ts-prototype Please no ...

Issue: The 'draggable' property is not found on the 'JQuery<HTMLElement>' type

When using Angular 2 and Typescript in conjunction with JQuery and JQueryUI, I encountered the following error: Property 'draggable' does not exist on type 'JQuery<HTMLElement>' I am aware that .draggable() is a function that rel ...

After filling a Set with asynchronous callbacks, attempting to iterate over it with a for-of loop does not accept using .entries() as an Array

Encountering issues with utilizing a Set populated asynchronously: const MaterialType_Requests_FromESI$ = SDE_REACTIONDATA.map(data => this.ESI.ReturnsType_AtId(data.materialTypeID)); let MaterialCollectionSet: Set<string> = new Set<s ...

The interplay between React Bootstrap responsive design, React hooks, and Alan AI is not producing the desired results

As a newcomer to react hooks, I've only been using them for a few weeks now. I'm currently working with my react card component, which ideally should be divided into 3 columns in one row. However, they are stacking on top of each other vertically ...

Generating a new Blob through assembling MediaRecorder blob sections leads to a blank Blob

I’ve encountered a peculiar issue with the new Blob constructor. My code returns an array of Blobs from the MediaRecorder: https://i.sstatic.net/X8Z7c.png However, when trying to work with this blob in my code, calling new Blob(audioChunks) results in ...

Angular obscured the referencing pointer

After updating Angular, I encountered issues with my code. Previously, the following code worked fine: @Component({ templateUrl: './some.component.html', styleUrls: ['./some.component.scss'] }) export class SomeComponent { ... p ...

The plugin called typescript from Rollup is throwing an error message with code TS2307. It says it cannot locate the module named 'App.svelte' or its related type declarations

I'm encountering a specific issue with my svelte project main.ts import App from './App.svelte'; const app = new App({ target: document.body, }); export default app; The first line is triggering a warning message Plugin typescript: @ ...

Regular pattern with Kubernetes cluster endpoint utilizing either IP address or fully qualified domain name

In my Angular/typescript project, I am working on building a regex for a cluster endpoint that includes an IP address or hostname (FQDN) in a URL format. For instance: Example 1 - 10.210.163.246/k8s/clusters/c-m-vftt4j5q Example 2 - fg380g9-32-vip3-ocs.s ...