Use the CSS class

To create tables with 3 rows and 2 columns using divs, I have utilized the ng-repeat directive. Within this setup, there are two CSS classes - red and green, that need to be applied in the following manner:

- Red class for the 1st column of the 1st row

- Green class for the 2nd column of the 1st row

- Green class for the 1st column of the 2nd row

- Red class for the 2nd column of the 2nd row

- Red class for the 1st column of the 3rd row

- Green class for the 2nd column of the 3rd row

I am looking for a way to efficiently implement these CSS rules. Any suggestions on how to achieve this setup?

Answer №1

To accomplish this, you can utilize the variables $even and $odd, along with the directive ng-class.

var app=angular.module('plunker', []);
app.controller('ListCtrl',ListCtrl);
function ListCtrl($scope) {
  
  $scope.items = [
    {name: 'foo', value: 'foo value',car:'chevy',model:'vette',year:'2013'},
    {name: 'bar', value: 'bar value'},
    {name: 'baz', value: 'baz value'}
  ];
  
  
}
.red{color:red;}
.green{color:green;}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!doctype html>
<html ng-app="plunker">
  <head>

  </head>
  <body>
    <div ng-controller="ListCtrl">
      <table class="table table-bordered">
        <thead>
        <tr>
            <th>Name</th>
            <th>Value</th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="item in items">
          <td ng-class="{'red':$even,'green':$odd}">{{item.name}}</td>
          <td ng-class="{'red':$odd,'green':$even}">{{item.value}}</td>
        </tr>
        </tbody>
      </table>
    </div>
  </body>
</html>

Answer №2

Give this a shot:

<tbody ng-repeat="info in responseData">                    
    <tr align="left" ng-if="$even">                     
        <td class="yellow">{{info.id}}</td>
        <td class="blue">{{info.first_name}}</td>
        <td>{{info.last_name}}</td>
        <td>{{info.email}}</td>                         
    </tr>
    <tr align="left" ng-if="$odd">                      
        <td class="blue">{{info.id}}</td>
        <td class="yellow">{{info.first_name}}</td>
        <td>{{info.last_name}}</td>
        <td>{{info.email}}</td>                         
    </tr>
</tbody>

<style>
    .blue {
         background-color: blue;
    }
    .yellow {
        background-color: yellow;
    }
</style>

Output similar to the image below:

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

How can the size of the font in a <div> be adjusted based on the number of characters present?

I am attempting to create a basic HTML and CSS layout with two div blocks, each 1000px wide, within a parent container that is 3000px wide. <div class="blocks"> <div class="block-a">text 1</div> <div class="block-b">text 2& ...

Retain the spaces within a string in Java and JavaScript programming languages

My Java code sends data to the browser via an HTTP request, and the output looks something like this: JAVA CODE OUTPUT | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | | 0 | SELECT STATEMENT | | | | 3 ...

Remove the model from operation

I'm fairly new to angularjs and have a working service. However, I want to move the patient model out of the service and into a separate javascript file. I believe I need to use a factory or service for this task, but I'm not entirely sure how to ...

Customize the List Box Appearance for a Specific HTML Item (Option)

I am working on achieving a specific behavior. When using a listBox (or comboBox), the first element (such as "choose one") should have a unique style. Here is an example of code to test: <style type="text/css"> .testX {font-style: italic;} </ ...

Is it possible for an Angular controller to utilize more than one $resource?

Currently, I have an angular controller that is set up with a $resource (e.g. /rest/book) and it's functioning properly. Now, I am considering extending the functionality of the controller to work with another $resource (e.g. /rest/recommendedTitle), ...

Turn off hover effect using solely CSS

This is my first time posting on stackoverflow, so please excuse me if I overlook something obvious. I tried searching for a solution beforehand but couldn't find one that seemed relevant. In the following jsfiddle example, I have a div that acts as ...

Encountering the error message 'Invalid cast specified' while using SqlDataReader in MVC

I kindly request not to categorize this inquiry as a DUPLICATE QUESTION. Despite my efforts to follow all the suggested solutions, I am still encountering this error. Is there anyone who can provide insight on where this error originates from? Below is th ...

Modify the parent div's background color on mouseover of the child li

I want to update the background image of a parent div that contains a series of ul li elements. Here is the HTML code I am working with: <section class="list" id="services"> <div class="row"> <ul> <li>& ...

Is It Best to Override Behavior in a Directive?

Having two directives that display lists of documents, one for user-favorited documents and the other for user-pinned documents. The criteria for displaying these documents depend on the values of "pinned" and "favorite" within each document object: docum ...

Using ng-click with multiple plus and minus operators in the same controller in AngularJS: A guide

Hey there, I'm a beginner in AngularJS. I'm working on a shopping cart app that has three text fields for selecting quantities, all of which are loaded from the server. However, I'm having trouble setting different ng-click methods for each ...

Tips for incorporating property into an object using ng-model in an HTML page with AngularJS

During my ng-repeat iteration, I am trying to add each field to another object using ng-model. //html <div ng-repeat="field in forms.fields track by $index"> <span ng-if="field.type=='textbox'"> <span style="width:115px; ...

App.jsx line 11 is throwing an error due to an undefined property 'TodoComponent'

While attempting to style a ReactJS component, I encountered an error in my browser's development console: App.jsx:11 Uncaught TypeError: Cannot read property 'TodoComponent' of undefined at App.render (App.jsx:11) I have tried imp ...

Why does the <div> element still have an offset even though its width and height are set to 100px and padding, margin, and border are set to 0px?

After styling my div element with specific CSS rules, I'm encountering an issue. The div has a set width and height of 100px along with padding, border, and margin all set to 0px. However, the elements are not being offset from the edges of the browse ...

Vertical Alignment of Navigation Bar in Bootstrap 4

I am trying to align the center of my navigation bar using the "Cover" Bootstrap 4 template. Please find the current code provided below. <div class="cover-container d-flex h-100 p-3 mx-auto flex-column"> <header class="masthead mb-auto"> ...

Grid Property Equivalent to Flex-Grow

I stumbled upon a bug in the way Chrome version 75 handles flexbox. Therefore, I am curious to explore if the issue persists with grid, but for my test to be accurate, I need a grid property that functions similarly to flex-grow considering the current str ...

Troubleshooting a navigation problem with DNN and Bootstrap-3 specifically for mobile devices

Here are the steps to demonstrate this issue: 1. Visit the following page of the website: 2. Resize your browser window to mobile size and close the menu using the icon. After the animation finishes, the navigation items reappear. I have been struggling ...

Rendering SVG graphics on browsers for Android devices

I've been struggling with an issue for quite some time now and I can't seem to find a solution. The problem lies in having an SVG image as a CSS background on the top left corner of a div. Instead of seamlessly merging with the div background, i ...

What is the best way to show an image within a div using HTML and fetching data from an API response?

I am attempting to showcase an image within a div using HTML. I utilized fetch to retrieve the JSON data from the following API: . The response contains JSON data with the image URL labeled "primaryImage". While I can display it as text, I am encounterin ...

Content enclosed by directive markers

If I have a directive called my-directive, How can I access or modify the text between the directive tags within the directive code, for example: <my-directive> Custom Text </my-directive> Here is my directive code: app.directive('myDi ...

What is the best way to conceal a sticky footer using another element?

I have a footer that remains fixed at the bottom of both long and short pages. Below is the CSS code for this footer: .footer{ position: absolute; bottom: 0; width: 100%; height: 100px; background-color: white; } My goal is to achieve a 'r ...