"Utilizing the ng-class directive in AngularJS to apply multiple

I have been attempting to utilize the ng-class directive in AngularJS based on a certain condition: if a JSON value is 1, it should display as green; if it's 2, red; and if it's 0, white. However, I am unable to achieve the desired result for some reason. To better understand the issue, here is a link to my code on JSFiddle:

https://jsfiddle.net/0xsv2aq3/42/

Any assistance with this matter would be greatly appreciated. Thank you.

    <html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>


<script>
var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {

$scope.schedule =

{
  "days": [
    {
      "day": "2018-12-09T00:00:00",
      "entries": [
        {
          "name": "Adam D",
          "value": 1,
          "parts": [
            "EG"
          ]
        },
        ...
      ]
    },
    ...
}

}
</script>

<body>

    <div ng-app="myApp" ng-controller="MyCtrl">
        <table class="table" border=1>
          <thead>
            <tr>
              <th>Name</th>
              <th ng-repeat=" x in schedule.days" scope="col">{{x.day}}</th>
            </tr>
          </thead>

<style>
.red{background-color: red;}
.white{background-color: white;}
.green{background-color: green;}
</style>

          <tbody>
            <tr  ng-class="{'red': day.entries[$parent.$index].value === 1, 'green': day.entries[$parent.$index].value === 2, 'white': day.entries[$parent.$index].value === 0}"  ng-repeat="day1 in schedule.days[0].entries" scope="row">
               <td>{{day1.name}}</td>
              <td ng-repeat="day in schedule.days">{{day.entries[$parent.$index].parts[0]}}>{{day.entries[$parent.$index].value}}</td>
            </tr>

          </tbody>
        </table>
      </div>
      
</body>
</html> 

Answer №1

I have implemented a custom filter to assign a class to td elements

myApp.filter('color',function(){
return function(status){
console.log(status);
    switch(status){
            case 0:
            return 'white';
            break;
        case 1:
            return 'red';
            break;
        case 2:
            return 'green';
            break;

    }
}
});

To utilize the filter, add it to the td tag

<td class="{{day.entries[$parent.$index].value | color}}" ng-repeat="day in schedule.days">{{day.entries[$parent.$index].parts[0]}}>{{day.entries[$parent.$index].value}}</td>

Answer №2

I mistakenly applied the style to the tr tag instead of td.

'https://jsfiddle.net/uniqueuser/yT84qwpa/3/'       

After making this correction, my issue was resolved successfully.

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

adjust the webpage to be optimized for the size of the screen

Looking to adjust the webpage layout so that it fits perfectly on the screen without the need for vertical or horizontal scrolling. Additionally, we want the image to be centered. See below for the current code snippet: #copyright { top: 0px; right: ...

The term "angular" is not recognized or defined in

Upon executing gulp in the command line terminal, an error warning appears. I have included the angular.js file in the HTML code but can't pinpoint the issue. Here is the HTML code snippet: <!DOCTYPE html> <html lang="en" ng-app="confusionA ...

Create a sleek 2-column design featuring a bonus scroll bar exclusively using CSS styling

In my current project, I have a unique challenge. I am working with a list of flattened div elements and unfortunately, I am unable to edit the HTML markup to add any structures. The first div in the list is quite long, and I would like to place it in a se ...

Place an element in a higher position than a slideshow

I recently encountered an issue where I was trying to place a png image above my slideshow. Despite trying various solutions, the image always appeared behind or on top without transparency. Based on the erratic display, it seems like the problem might be ...

Despite following all the correct steps, the tailwind CLI remains unresponsive. Additionally, the default button fails to display any content

view the document and my packages (image) Although it worked with the cdn, I'm puzzled why I can't use it properly with the cli ...

Unable to send messages through contact form - Issue with HTML/CSS

I had successfully achieved the desired outcome before, but now I am facing difficulties replicating it. The linked image displays my intended result versus what is currently happening. https://i.stack.imgur.com/QUCuN.png What could be causing this issue ...

Restrictions on AngularJS animation for a specified class

Greetings! I am curious to know if there is a method to prevent Angular from automatically adding extra classes to an element for animation purposes. For example, when an element already has a CSS transition set and I apply another class using ng-class, A ...

Overflow of nested item within flex container

I'm trying to make the content under "foo" fully scrollable, but I've been unsuccessful using flexbox. I attempted it with min-height, yet it didn't work out. Check out this JSFiddle for reference $('.ui.dropdown') .dropd ...

Utilizing Jquery to automatically scroll to a specific div on page load by setting an

I am attempting to automatically scroll to the div specified in the URL. The URL may include one of 21 different IDs such as: url.com/test#lite1 url.com/test#lite2 url.com/test#lite3 This scrolling action should happen when the page loads (so that user ...

Adapting the column width to display or hide content with CSS styling

I have a row with 2 columns. The left column contains my main content and the right column is a chatroom. I would like users to be able to minimize and open the chatroom, which I already know how to do. However, when the chatroom is open, I want the left ...

Using PHP foreach loop to create bootstrap 4 cards

I am attempting to showcase all of my shop items on my webpage using Bootstrap 4 cards. I want them to be displayed in a grid format, three wide and however many deep (I may consider adding pagination later). Currently, I have a PHP foreach loop that succ ...

Is there a way to apply a custom CSS to Bootstrap without altering its appearance?

Check out this code snippet <link rel="stylesheet" href="css/bootstrap.css"> <link rel="stylesheet" href="css/sub-cat.css"> I am having trouble with my CSS. I tried to modify the "Caption" class in Bootstrap by adding some new styles in "sub- ...

absence of a scrollbar on smaller window display

My website is experiencing an issue: everything displays properly when the window size matches or exceeds the background image, but on a smaller window, there is no scroll bar which causes the footer to disappear. Here is an image showing what happens wit ...

Frustration ensues when working with the Angular + CoffeeScript combination in

Check out this JavaScript JSFiddle that is functional: http://jsfiddle.net/franklovecchio/E3YEt/ Unfortunately, the CoffeeScript JSFiddle provided seems to be broken: http://jsfiddle.net/franklovecchio/E3YEt/230/ Even after adding angular.bootstrap(docum ...

Tips for adjusting the color of multi-line text when hovering with the mouse

I'm facing a challenge in changing the color of text when someone hovers over it, but so far I've only been able to make it work if the mouse scrolls over the top border of the text. The text I'm testing is located in the top left corner an ...

Adjusting SVG size based on the position of the cursor for transforming origin

My goal is to scale an SVG circle using the trackpad (by moving two fingers up and down), with the origin of the transform being the cursor position. Initially, the scaling works as intended, but on subsequent attempts, the circle changes position, which s ...

Steps for styling the header of an Antd Collapse Panel

I'm attempting to modify the text color in the header of an ant panel. Below is the entire ant collapse component code: <Collapse accordion defaultActiveKey={defaultOpenPanel}> <StyledCollapsePanel key="tasksAssignedToMe" header={ ...

Having trouble with uib-typeahead in Angular 1.6.6?

Recently, I upgraded from Angular 1.4.8 to 1.6.6 and encountered a problem. <input type="select" class="form-control pull-right" placeholder="Enter IPC or Project Reference" ng-model="systemState.quickSearchTerm" uib-typeahead-on-select="selectQu ...

Ways to center items within a column using Bootstrap

I'm working on a React project with Bootstrap and I'm trying to align the contents of my second column to the bottom. However, everything I've tried so far hasn't been successful. I've seen others achieve this layout but for some r ...

Is it possible to conceal the controls on the slick carousel?

Is there a way to prevent slick from automatically adding next and previous buttons? I've tried using CSS to hide them but it doesn't seem to work. <button type="button" data-role="none" class="slick-prev" aria-label="previous" style="displ ...