Update the background color of a table using AngularJS on the fly

Is it possible to dynamically change the background of a table cell in HTML using AngularJS?

In this scenario, I am aiming to display a table containing different names along with the status of an object. The status can either be VALID (displayed with a green background) or INVALID (displayed with a red background).

Can this issue be resolved directly within the <td> tag in HTML, or is it necessary to utilize CSS?

<table border="1">
   <tr ng-repeat="object in Cmd.items">
      <td>{{object.objectName}}</td>
      <td>{{object.objectStatus}}</td> //the background color here should vary
   </tr>
</table>

Answer №1

To achieve this outcome, utilize the ng-class directive:

<tr ng-repeat="item in Ctrl.items">
      <td>{{item.name}}</td>
      <td ng-class="{'red': item.status === 'invalid', 'green': item.status === 'valid'}">{{item.status}}</td> 
</tr>

JS Fiddle Example

Answer №2

If you want to dynamically apply CSS classes in Angular, you can utilize ng-class directive.

For example:

<div ng-class={'green-bg':isValid, 'red-bg':!isValid}> </div>

In this code snippet, green-bg and red-bg represent CSS classes that will be applied based on the value of the isValid property. This property determines whether the element should have the 'green-bg' class or the 'red-bg' class.

Answer №3

CSS Styling :

.invalid{
background-color:red;
}

.valid{
background-color:green;
}

HTML Table with AngularJS:

<table border="1">
   <tr ng-repeat="item in itemList">
      <td>{{item.name}}</td>
      <td ng-class="item.status">{{item.status}}</td> //color background based on status
   </tr>
</table>

Answer №4

This issue can be resolved either within the td tag itself or in your external CSS file.

In HTML

     <td ng-style="{'background': (object.objectStatus=='VALID') ? 'green' : 'red'}">
        {{object.objectStatus}}
      </td>

If using external CSS, you have the option to implement @oto lolua's solution.

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 best way to transmit a JSON object to REST services using Angular?

Whenever I attempt to send the JSON object to REST services, I encounter an error that looks like this: http://localhost:8080/api/v1/cardLimit 400 (Bad Request); JSON Object Example: public class GameLimit implements Serializable { private stati ...

Using Angular Directive to Process Array of Objects

I am facing an issue with passing an array of objects from my angular controller to a directive. The array looks something like this: [{ label: 'label1', data: [{ type: TimelineChart.TYPE.POINT, at: new Date([2015 ...

Executing a YUI function via HTMLJSGlobal called is feasible

Recently, I stumbled upon the Dial example on the official YUI website and was thoroughly impressed. I managed to get the function working perfectly in a JS file, but now I am facing issues trying to call it within the HTML page with specific parameters su ...

Achieving Precision: Input Field Accentuated by Focus

How can we target the placeholder pseudo-element of a focused element? Attempts to achieve this with the code snippets below have been unsuccessful. input:focus > ::placeholder { /* insert your code here */ } input:focus > input::placeholder { ...

What is the best way to position a <td> element within a table row underneath another <td>?

Looking for a solution to ensure Content 3 appears below Content 2 in this block of HTML. Content 1 is extensive and extends far down the page. <table> <tr> <td>(Content 1)</td> <td>(Content 2)</td> ...

Managing forms with Bootstrap

Hey there, I'm facing an issue with form handling in Bootstrap. Any suggestions to solve it? The problem lies with the spacing of elements, and unfortunately, I haven't been able to find a suitable solution yet. I've experimented with horiz ...

Is it possible to use Meteor with the latest version of Angular 2?

I'm currently using Meteor 1.4.1 with rc4 and wondering if there's a way to integrate the final version of Angular 2 with it. Should I wait for an updated version from Meteor, or is there a way to use it now? ...

Reduce the width of a div using Bootstrap

I am looking to adjust the width of the black background to match the image and buttons for better proportions. Below is the code for one component on the page, with a similar structure for other components as well. <link rel="stylesheet" href="http ...

Incorporate a CSS-styled Hyperlink Button into a Jquery Loopedslider

After spending the last 2 hours trying to figure out what seems to be a simple solution, I am determined to add a hyperlink button to each image/div in the jquery Loopedslider using CSS. Although I have used loopedslider multiple times before, I have neve ...

The information is not appearing in the dropdown menu

The select tag for chapters is not displaying the result from the query properly. Instead of showing in the select tag, the chapter names are being displayed as echo statements. <!doctype html> <html> <head> <meta charset="utf-8"> ...

Trouble with executing a jQuery dynamic loop for each item

I have a unique situation where I am dealing with a table containing multiple tables for various users. The number of users can vary, so I am aiming to create a dynamic solution. Below are two sample tables for reference. <div class="timecard"> < ...

UI-Bootstrap selectively adds modal backdrop to specific elements

I have encountered a unique situation with my application where I am displaying a UI-Bootstrap modal inside a div, positioned above a navigation bar. Typically, when the modal is launched, a backdrop (dimmer) covers the entire background. Interestingly, my ...

Vertical menu causing problem with smooth scrolling link effect

Currently, I am facing an issue with a template and trying to resolve it. The problem lies with the highlighting effect on the navigation menu, which only seems to work on a reduced browser height. When I resize the window to full screen and scroll down to ...

Bizarre Actions of a JavaScript Object

Currently, I am in the process of developing a Multiplayer game using Phaser and Eureca io. My main focus right now is on perfecting the authentication of players and their controls. To achieve this, I have implemented a method on the server that retrieves ...

What causes the 'Illegal invocation' error to be thrown in AngularJS when using promise construction?

When I call a promise function: return $http.post("anyCtrl").then(location.reload); An 'Illegal invocation' exception is thrown in Angular on the browser console. However, when I change it to: return $http.post("anyCtrl").then(function(){loca ...

Pressing the enter key in an AngularJS form does not trigger submission

Having trouble with a login form that won't submit when the user presses enter. While the form works fine when the "Login" button is clicked, hitting enter doesn't trigger submission and leads to some unexpected behavior: The ng-submit associat ...

Having trouble with ui-sref functionality within a Bootstrap dropdown menu

In the header.html file, I have the following angular code: <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav navbar-right"> &l ...

Alter the button's color seamlessly while staying on the same page

I have implemented a feature that allows me to change the category of photos without having to leave the page and it works perfectly. My next goal is to create a button system where the pre-defined category of a photo is indicated by a button with a green ...

Creating distinct identifiers for table cells utilizing the map function

Is there a way to assign a unique id to each MenuItem using the map() function nested within another one? <table className={classes.table}> <thead> <tr> <td /> {sit.sit.map(sit => ( <td className={ ...

Tips for arranging divs in CSS grid with grid-template-areas technique

I am attempting to organize similar groups of elements in a grid: #grid { position: absolute; left: 135px; top: 158px; width: 1080px; height: 1035px; display: grid; grid-template-rows: 99px 1fr 1fr; grid-template-column ...