Utilizing the ngIf Statement with a Repeating Element: A Step-by-Step

I am working with an Angular Material table:

(HTML)

<table mat-table
        [dataSource]="dataSource" multiTemplateDataRows
        class="mat-elevation-z8">
  <ng-container matColumnDef="{{column}}" *ngFor="let column of displayedColumns">
    <ng-container *ngIf="!column=='slaStatus'">
      <th mat-header-cell *matHeaderCellDef> {{column}} </th>
      <td mat-cell *matCellDef="let element"&qt; {{element[column]}} </td>
    </ng-container>
    <ng-container *ngIf="column=='slaStatus'" matColumnDef="{{column}}">
        <th mat-header-cell *matHeaderCellDef mat-sort-header&qt; SLA</th>
        <td mat-cell *matCellDef="let element"&qt;
            <mat-chip-list aria-label="Fish selection">
                <mat-chip *ngIf="element.slaStatus=='On Time'" style="background-color: #B9F6CA; color: green&qt; On Time</mat-chip>
                <mat-chip *ngIf="element.slaStatus=='Late'" style="background-color: #B9F6CA; color: yellow&qt; Late</mat-chip>
                <mat-chip *ngIf="element.slaStatus=='Missing'" style="background-color: #ec9d9d; color: red&qt; Missing</mat-chip>
            </mat-chip-list&qt;
        </td>
    </ng-container&qt;
  </ng-container&qt;

An issue occurred while implementing this code

<ng-container *ngIf="!column=='slaStatus'">
error TS2367: This condition will always return 'false' since the types 'boolean' and 'string' have no overlap 

The main goal is to differentiate and format the 'slaStatus' column as a distinct chip in the Angular Material table. How can I adjust the ngIf condition to specifically target the 'slaStatus' column?

(Typescript)

  displayedColumns: string[] = [
    'id',
    'tradingPartnerTradingPartner',
    'fileFormatFileFormat',
    <mark>'slaStatus',</mark>
  ];

Answer №1

<table mat-table
        [dataSource]="dataSource" multiTemplateDataRows
        class="mat-elevation-z8">
  <ng-container matColumnDef="{{column}}" *ngFor="let column of displayedColumns">
    <ng-container *ngIf="column!=='slaStatus'">
      <th mat-header-cell *matHeaderCellDef> {{column}} </th>
      <td mat-cell *matCellDef="let element"> {{element[column]}} </td>
    </ng-container>
    <ng-container *ngIf="column=='slaStatus'" matColumnDef="{{column}}">
        <th mat-header-cell *matHeaderCellDef mat-sort-header> SLA</th>
        <td mat-cell *matCellDef="let element">
            <mat-chip-list aria-label="Fish selection">
                <mat-chip *ngIf="element.slaStatus=='On Time'" style="background-color: #B9F6CA; color: green">On Time</mat-chip>
                <mat-chip *ngIf="element.slaStatus=='Late'" style="background-color: #B9F6CA; color: yellow">Late</mat-chip>
                <mat-chip *ngIf="element.slaStatus=='Missing'" style="background-color: #ec9d9d; color: red">Missing</mat-chip>
            </mat-chip-list>
        </td>
    </ng-container>
  </ng-container>

There is an issue with the conditional statement:

*ngIf="column!=='slaStatus'"
. It needs to be fixed.

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

What is the reason behind taps in TypeScript only registering the first tap event?

One issue I am encountering is that only the first tap works when clicked, and subsequent taps result in an error message: Uncaught TypeError: Cannot read properties of undefined (reading 'classList') Here is the code I am using: https://codepen ...

How to stop parent event propagation in jQuery

I am facing a frustrating issue with my code. Every time I toggle a checkbox, the parent element's click event also triggers. I have experimented with various solutions like: event.stopImmediatePropagation(); event.stopPropagation(); event.preventD ...

Current target in Internet Explorer 10 with Javascript

Encountering a problem with event.currentTarget in IE 10. Two divs are present on the website. The first div contains an image that takes up the entire screen, while the second div is smaller and positioned over the first div. The smaller div has no conte ...

The type 'Observable' does not contain the properties found in type 'User'

I am trying to retrieve user data from Firestore, but encountering an error The type 'Observable' is missing the following properties from type 'User': email, emailVerified, uidts(2739) user.service.ts import { Injectable } from &apo ...

What is the reason for my algorithm's inability to work with this specific number?

I'm currently working on creating an algorithm to compute the sum of prime numbers that are less than or equal to a specified number. Below is my attempt: function calculatePrimeSum(num) { // initialize an array with numbers up to the given num let ...

Using TypeScript along with the "this" parameter

Hi there, I'm encountering an issue with the code snippet below. It keeps throwing an error message that says "Property 'weatherData' does not exist on type 'XMLHttpRequest'." The purpose of this code is to display weather informat ...

Store the HttpRequest value within the component

I am encountering an issue with the Http Request value. I am sending an Http request to an Express API rest, and I want to display the value throughout my component. The data is available in the observable but not in other functions of my component. Can ...

How do you use CSS and Bootstrap to create a background image that covers one-third of the screen's size?

I have attempted to implement the following using inline CSS along with Bootstrap, but unfortunately it is not functioning correctly. <div class="container" class="img-responsive" class="img-fluid" style="background-image: url('img/projects_cover. ...

Center text with font awesome symbols

I'm looking for a way to vertically align text next to my FontAwesome icons without using manual padding. Is there a better method for achieving this? https://i.stack.imgur.com/PWwSJ.jpg <div class="row"> <div id="tb-testimonial" class="tes ...

Achieve vertical alignment in unordered lists with Bootstrap 4

Bootstrap4 documentation states that the predefined classes align-items-center and justify-content-center can be used to align contents vertically and horizontally. While testing the example provided in the Bootstrap4 document, it worked correctly. However ...

Angular Material has support for displaying nested optgroups within select components

Upon reviewing a question on Stack Overflow, I learned that nested optgroups are not supported in the HTML5 spec. While it is possible to manually style them, I am curious if Angular Material offers a solution. Angular Material internally swaps many eleme ...

What are some solutions for repairing unresponsive buttons on a webpage?

My task is to troubleshoot this webpage as the buttons are not functioning correctly. Here’s a snippet of the source code: <!DOCTYPE html> <html lang="en"> <head> ... </head> <body> <div id="container" ...

Managing JSON data retrieval and manipulation techniques

My code is set up to display the image, title, and summary for all entries in a JSON file. However, I only want to display the image, title, and summary for the first entry, and only show the title for the rest of the entries. Please advise. <html> ...

Make the HTML element expand to the remaining height of the viewport

Need help with creating a section that automatically fills the remaining viewport height below the navigation bar. The section includes a centered div and background image, along with a button at the bottom for scrolling down by one full viewport height. H ...

What is the best method for ensuring that cheese rises to the top?

Is there a way to increase the value of the variable cheese? I suspect it has something to do with how the variable cheese is defined each time the JavaScript is activated, but I'm not sure how to go about it. Can you offer some guidance on this? & ...

I'm stuck trying to figure out all the parameters for the MapsPage component in Angular 2

Currently, I am utilizing Angular2 with Ionic2 for my mobile app development. Everything was working flawlessly until I decided to incorporate a new module for Google Maps navigation. Specifically, I am using phonegap-launch-navigator for this purpose. The ...

Resolving route data in Angular 2 and above

When retrieving data from an API in an Angular route resolver, I encountered a problem where if there was an error due to a stale token, I needed to refresh the token and then retry the API call. Below is the code snippet illustrating this scenario: impor ...

The client PC running the Ionic app encountered an error while trying to load chunk 'XY'

I'm currently working with Ionic 3 and lazy loading. Everything is running smoothly on 12 different PCs. However, I encountered an issue on one PC where it started displaying the error message "Loading chunk 7 failed," sometimes with numbers like 43 o ...

Is the parent component not triggering the function properly?

Hey there, I'm working with the code snippet below in this component: <app-steps #appSteps [menuSteps]="steps" [currentComponent]="outlet?.component" (currentStepChange)="currentStep = $event"> <div appStep ...

Changing the order of a list in TypeScript according to a property called 'rank'

I am currently working on a function to rearrange a list based on their rank property. Let's consider the following example: (my object also contains other properties) var array=[ {id:1,rank:2}, {id:18,rank:1}, {id:53,rank:3}, {id:3,rank:5}, {id:19,r ...