What is the best way to modify the color of a table cell in Typescript with *ngFor loop?

I have an image located at the following link:

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

My goal is to have cells with different colors, where main action=1 results in a green cell and action=0 results in a red cell.

Below is the HTML code I am working with:

  <div class="row" colum>
    <div class="grid-container">
      <table *ngFor="let item of notification">
        <tr style="text-align:center">
          <ul> {{item.alarmdesc}}</ul>
          <ul> {{getProductName(item.device_serial)}}</ul>
          <ul> {{item.datetime_device | date:'d/MM/yyyy'}}</ul>
          <ul>
            <button style="border-style: groove; 
          width:70%; 
          height:30%; 
          margin: 15%;
          border: 1px solid rgb(198, 199, 198);
          padding: 10px;cursor: pointer;" (click)="OnAced(item.id)">main action: {{item.acted}}
        </button> 
        </ul>
        </tr>
      </table>
    </div>
  </div>

Answer №1

One way to implement a condition using the conditional operator is by checking if an action exists within your item.

<div [style.border-color]="item.action == 1 ? 'green' : 'red'"></div>

Answer №2

One essential point to remember is that using inline styling is not recommended; it is better to utilize the corresponding CSS file for the component.

Consider implementing the following approach:

CSS file:

.cell {
   text-align: center;
}

.green {
   color: "green";
}

Component file:

isGreen(main): boolean {
  return main === 1
}

You can then apply the CSS class to the element in this manner:

<tr class="cell" [class.green]="isGreen(main)"></tr>

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

The function Interceptor.intercept is not defined

For a while now, I've been working on implementing an application-wide interceptor. However, no matter what I do, I keep encountering the same frustrating error: TypeError: this.interceptor.intercept is not a function After countless hours of debugg ...

Is there a way to carry out tests on keydown events within Jasmine by specifying the keyCode within an Angular2+

I am working on a project where I need to trigger keydown events on an <input> field. Tools Used Karma v1.7.1 as the test runner Tests running on Chrome browser Using Angular 5 framework Any insights on how I can achieve this? ...

Tips on organizing all property options into an array of objects

I need to identify the array type of a specific property and use TypeScript typing for autocompletion. const arr = [{test:'option 1'},{test: 'option 2'}] type test = arr[number]['test'] let t:test = '' // will equal ...

Challenges encountered with wrapper component functionality in Angular 6

I am currently working on creating a wrapper component for the saturn-datepicker in Angular. My plan is to use this wrapper component in multiple applications now and potentially switch it out with another datepicker in the future. Although new to Angular, ...

Reacting to the dynamic removal of spaces

Is there a way to dynamically remove the space between removed chips and older chips in Material-UI when deleting one of them? <div className="row contactsContainer"> {contacts.map((contac ...

The specified property cannot be found within the type 'JSX.IntrinsicElements'. TS2339

Out of the blue, my TypeScript is throwing an error every time I attempt to use header tags in my TSX files. The error message reads: Property 'h1' does not exist on type 'JSX.IntrinsicElements'. TS2339 It seems to accept all other ta ...

Managing component composition in React/TypeScript: What's the best way to approach it?

I am brand new to the world of typescript, so please be patient with me. My objective is to transform this react component: interface ButtonProps {...} const Button: React.FC<ButtonProps> = ({ children, href, value as = 'button', ...

What is the best way to import multiple classes from a module folder in Angular2 using TypeScript?

In my Angular2 application, I have organized my modules as follows: /app /content /models resource.ts container.ts entity-type.ts index.ts /services /whatever ...

The dynamically rendered component cannot be assigned to the type 'IntrinsicAttributes & ContentOutlineProps & ContentBrainstormProps'

I am facing an issue on my page where a <SideBar /> component is causing a Typescript error with the setActivePage hook. The error message points to a specific line in my code: const Content: (({ question_blocks, }: ContentBrainstormProps) => JSX. ...

Ways to retrieve particular items/properties from the JSON response string

Is there a way to extract and display only the first 3 items from my JSON data instead of the entire string? Below is the code I am currently using to loop through and display my records: <tr v-for="(list) in myData" v-bind:key="list.ema ...

Steps for resolving the error message: "An appropriate loader is required to handle this file type, but there are no loaders currently configured to process it."

I am encountering an issue when working with next.js and trying to export a type in a file called index.ts within a third-party package. Module parse failed: Unexpected token (23:7) You may need an appropriate loader to handle this file type, current ...

Troubleshooting Node.js TypeScript breakpoints in Visual Studio Code

I've attempted multiple solutions, but none seem to be working for me. Although the code is running, I'm having trouble setting breakpoints and debugging it. Can you offer any assistance? Below is the configuration script I've tried in VSCo ...

Adjusting the date format in Angular Material date picker dynamically

I am facing a challenge with my angular material date picker. I have set the default date format to dd/mm/yyyy for the entire app, but in certain instances, I need it to display as dd/mm/yy instead. Instead of creating a new date picker component just for ...

What is the best way to store a small number of files in the state

I have recently implemented Drag and Drop functionality, and now I am facing an issue where I need to save a few files in state. const [uploadedFiles, setUploadedFiles] = useState<any[]>([]); const onDropHandler = async (e: React.DragEvent<HTMLDi ...

Executing multiple HTTP requests in Angular using the HttpClient

Recently, I came across a concerning issue in my Angular 5 App. It all started with this simple call in my service: interface U{ name:string; } ... constructor(private http : *Http*, private httpC:HttpClient) // Http is deprecated - its a compare test ...

Issue occurred while trying to deploy Firebase functions following GTS initialization

Objective: I am aiming to integrate Google's TypeScript style guide, gts, into my Firebase Functions project. Desired Outcome: I expect that executing the command firebase deploy --only functions will successfully deploy my functions even after init ...

What causes the Cassandra client driver to provide more detailed information compared to cqlsh?

I'm currently utilizing the datastax nodejs-driver to retrieve information about a keyspace from cassandra. const results = await client.execute( ` DESC KEYSPACE ${keyspace} ` ); The method client.execute provides a comprehensive object containin ...

The Highcharts Angular library is struggling to access the 'series' property due to it being undefined

Encountered an error message that has puzzled me. I am eager to explore highstock.js and its associated files, but I'm uncertain about the best approach. Despite the chart displaying correctly, this error consistently arises. https://i.sstatic.net/p ...

Integrate child component properties within the parent component

Looking to create a wrapper component that selects specific props from an inner component and includes additional custom props. The issue is that using pick will generate a type rather than an interface, limiting the ability to add more keys. How can I wor ...

Employing a provider within a different provider and reciprocally intertwining their functions

I'm currently facing an issue with two providers, which I have injected through the constructor. Here's the code for my user-data.ts file: @Injectable() export class UserDataProvider { constructor(private apiService: ApiServiceProvider) { ...