Learn how to utilize NGIF to dynamically display elements in the footer of a datatable

I am trying to display the footer element in my row Datatable only when I am in a specific column. I usually know how to do this in normal cases, but here I am using programming row def (which I need for grouping).

This is the code I have tried:

Template:

<mat-table [dataSource]="dataSource" class="mat-elevation-z8">

            <ng-container *ngFor="let column of columns; let i = index" matColumnDef="{{ column.field }}">
              <mat-header-cell *matHeaderCellDef (click)="SortWith($event,column)">{{ column.field }}


              </mat-header-cell>
            <mat-cell *matCellDef="let row">
                <div >
                    {{ row[column.field] }}
                </div>
            </mat-cell>
            <mat-footer-cell *matFooterCellDef *ngIf="column.field == Category"> <b><font color=red>Total: </font></b></mat-footer-cell> // I need to dispaly this just in column of category

        </ng-container>

              <mat-header-row mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
              <mat-row *matRowDef="let row; columns: displayedColumns;" [className]="row.RemainingQuantities == 0 ? 'red' : 'redBack'"></mat-row>
              <mat-row *matFooterRowDef="displayedColumns; sticky: true"></mat-row>

              <!-- Group header -->
              <ng-container matColumnDef="groupHeader">
                  <mat-cell colspan="999" *matCellDef="let group">
                <mat-icon *ngIf="group.expanded">expand_less</mat-icon>
                <mat-icon *ngIf="!group.expanded">expand_more</mat-icon>
                      <strong>{{groupByColumns[group.level-1]}} = {{group[groupByColumns[group.level-1]]}} ({{group.totalCounts}})</strong>
                </mat-cell>
            </ng-container>

My component.ts:

this.columns = [{
      field: 'Category'
    },  {
      field: 'Model'
    },  {
      field: 'Reference'
    },  {
      field: 'Name'
    },  {
      field: 'RemainingQuantities'
    },  {
      field: 'Department.Name'
    },  {
      field: 'Supplier.Name'
    }];
    this.displayedColumns = this.columns.map(column => column.field);
    this.groupByColumns = ['Category'];
 columnsToDisplay: string[] = ['Category', 'Model', 'Reference', 'Name', 'RemainingQuantities', 'Department.Name', 'Supplier.Name'];
   SortedColumns: string[] = [];
    this.service.get_product().subscribe((result : any)=>{
      this.listdata = result;

      this.decompersed = decrypt(result);
      console.log(this.decompersed);
      this.ProductList = this.decompersed;
      this.dataSource.data = this.addGroups(this.ProductList, this.groupByColumns);
      this.dataSource.filterPredicate = this.customFilterPredicate.bind(this);
      this.dataSource.filter = performance.now().toString();

However, I encountered an error:

Uncaught Error: Template parse errors: Can't have multiple template bindings on one element. Use only one attribute prefixed with * (" ]*ngIf="column.field == Category"> Total: "):

When I remove the NgIf, I get this result:

https://i.sstatic.net/L1aAg.png

I only need the first total label

Answer №1

To solve this issue, implement the following code:

[className]="column.field == 'Category' ? 'nothing' : 'hidetool'"
. The hidetool class includes attributes for hiding elements.

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

A guide on crafting a type definition for the action parameter in the React useReducer hook with Typescript

In this scenario, let's consider the definition of userReducer as follows: function userReducer(state: string, action: UserAction): string { switch (action.type) { case "LOGIN": return action.username; case "LOGOUT": return ""; ...

I am looking to update the URL in a React application without triggering a screen redraw

I’m looking to implement a feature where users can scroll through videos seamlessly, similar to the experience on YouTube or TikTok. One key aspect is changing the URL to match the currently displayed video using "https://example.com/{id}" when ...

typescript is throwing an error because it cannot read the property 'filter' of an undefined variable

Whenever this code is executed, the following error is displayed: Cannot read property 'filter' of undefined contrat: Contrat[]; gpss: GPS[]; homeboxp: HomeboxP[]; sensors: Sensors[]; homebox: Homebox[]; getProductName(productid: s ...

What is the reason I am unable to asign a reference to a subclass to a variable when the reference type is a superclass with Generics?

What is the reason behind not being able to assign a reference to a derived class to a variable if the reference type is a parent class with Generics? interface IModel { id?: number; } class Model<T extends IModel> { public data: T; con ...

Create an array form with the first input at index 0 filled out

Currently, I am in the process of developing a form that allows users to update, add, delete, and edit entries using Angular 2 and reactive forms. To achieve this, I have utilized a form builder array to dynamically add input controls to the form array. Ev ...

What is preventing me from being able to use object spread results in TypeScript casting?

Uniqueness in Question My inquiry was not aimed at identifying the type or class name of an object. Rather, it delved into the concept of "casting" an object to a specific type, which arose from a misconception about TypeScript and its functionality. This ...

When using the TypeScript && operator, the resulting type is not determined by the second operand

Several past discussions on SO have touched upon the concept that the inferred type from && is based on the last expression. TypeScript’s failure to detect union type with an && operator Uncovering the reason behind the && opera ...

ngx-bootstrap modal failing to access the properties of my component

I am currently utilizing ngx-bootstrap with bootstrap 3.3.7 and facing an issue with the modal service. Although the modal dialog opens successfully, only the static content is displayed. None of the dynamic content that I pass to the service seems to work ...

Show the values of an array if the array name matches the string name

Is there a way to dynamically display the values of an array based on a matching string in Angular 4 or 6? Let's say we have two arrays, A and B: A = ['aaa','arrr','aeee','aqqqq','awwww','axxxx& ...

What is the default behavior for an invalid select option in Angular 2 model-driven forms?

Is it possible to create an invalid default select option in an Angular 2 model-driven form? I want to do something like the following: <select name="DateExpiryMonth" [(ngModel)]="model.DateExpiryMonth" ngControl="dateExpiryMonth" required> < ...

You may encounter an error stating "Property X does not exist on type 'Vue'" when attempting to access somePropOrMethod on this.$parent or this.$root in your code

When using VueJS with TypeScript, trying to access a property or method using this.$parent.somePropOrMethod or this.$root.somePropOrMethod can lead to a type error stating that Property somePropOrMethod does not exist on type 'Vue' The defined i ...

Press the inactive button to view the continuing effects

Currently, I am utilizing mat-icon for my buttons and would like to disable certain ones: <button mat-button [disabled]="disabledCondition()"> <mat-icon [routerLink]="['./settings']"> settings </mat-icon> </b ...

How can we correctly extract a value from the callback of an asynchronous function within another asynchronous function in Angular 6?

I am currently facing an issue with my HTTP provider where I need to access a token value stored in local storage during a GET request. The token value is retrieved using another provider through the getToken function in the HTTP provider. However, I am un ...

Enhance the efficiency of writing the *.d.ts file

I have recently started diving into TypeScript with React and encountered a new concept called a .d.ts file. I am wondering if there is an established best practice for writing this file or if it's more of a developer preference. Can someone verify if ...

Import Firebase User in Typescript v8 definition

In the previous versions of Firebase, I used to import the Typescript definition of the User like this: import {User} from 'firebase'; However, with the introduction of v8, this import no longer works and gives the following error message: Mod ...

Resolving callback definition issue: Can be assigned to constraint, yet may be instantiated with a distinct subtype. ( TS 2345 )

Seeking insight on the typing issue causing a compiler error in the code snippet below. Any suggestions for maintaining type-safety without resorting to any or as? Avoiding these workarounds is important to me. The challenge lies in the evidence() call, c ...

Pause for Progress - Angular 6

I'm looking for a solution to solve the following issue. I need to access a property that will store data from an http request. Therefore, I want to verify this property only after the transaction is completed. validateAuthorization(path: string): ...

A Guide to Filtering MongoDB Data Using Array Values

I am trying to extract specific data from a document in my collection that contains values stored in an array. { "name": "ABC", "details": [ {"color": "red", "price": 20000}, {" ...

What is the technique for retrieving the value of the "this" variable within a for-of loop in TypeScript?

How can I retrieve the value of this from a for-of loop in TypeScript? Check out this example for-of loop: //for each author qs param for (let author of qsParams.authors) { //check for match by id var matches = this.vm.Authors. ...

Investigating issues with console errors and variables within callback functions in Angular 2 unit tests using Jasmine

I need assistance in achieving full coverage for this simple function, but I am struggling to do so. name-list.service.ts import { Injectable } from '@angular/core'; import { Http, Response } from '@angular/http'; import { Observable ...