Alter the background hue of alternate div elements within a row

The table I have contains multiple div elements, each corresponding to an item in an array.

Within the table, there is a single tr, and the 1st and 2nd td are not directly related to the 3rd and 4th td.

Each td in the table repeats a div element for the length of an array. For example, the first column between Balance B/d and Income represents an array, and so does Income and subsequent elements.

Although I tried to apply a shaded-row style to every even row using ng-class, it didn't work as expected. Another method I considered was using .statement-item:nth-child(even), but this approach requires the removal of the parent statement-container. Is there a better solution?

    
    <!-- Table structure here -->

Answer №1

Consider checking the length of the previous collection in the 2nd repeater and based on whether it is even or odd, reverse your ng-class logic like this:

balanceArray.length % 2  ===  0 ? $index % 2 == 0 : $index % 2 != 0

By the way, if there is no specific reason for separating them with a container, it might be more efficient to keep all items together and manipulate the styling with CSS (using odd/even child selectors).

Answer №2

How about this concept: develop a function that stores an array of random colors and chooses one randomly. Check out the example below:

getRandomColor() {
    let colors = ['#ffffff', '#000000', '#ff0000', '#00ff00', '#0000ff'];

    let randomColor = colors[Math.floor(Math.random() * colors.length)];

    return randomColor;
}

Alternatively, you could establish a global variable array with objects structured like this:

var colorsArray = [{color: '#fff', used: false}, {color: '#000', used: true}];

Then you could create a function that iterates over that array:

let selectedColor = '';

for(let i = 0; i < colorsArray.length; i++) {
  if(!colorsArray[i].used) {
    colorsArray[i].used = true;
    selectedColor = colorsArray[i].color;
    break;
  }
}

return selectedColor;

You might be able to implement something like

ng-style="{'background-color' : getRandomColor()}"
(I'm not very knowledgeable about Angular, so please forgive any mistakes.)

I hope this suggestion helps kickstart your project.

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

Attempting to interact with the <Button> element, within selenium web driver utilizing python

I have attempted to interact with a button in the given HTML using Python (Selenium WebDriver). <div class="cssm-TopNav_printIcon_2VzB_"> <button type="button" aria-label="Print" title="Print" class="cssm-Button_button_2a549 cssm-Button_secondar ...

Hiding a div using swipe gestures in Angular

What am I trying to achieve? I aim to hide a div when swiped right. This specific action will close the pop-up that appears after clicking a button. What tools are at my disposal? I am utilizing Ionic framework for this task. Within my app.js, I have i ...

Creating multiple rectangles and filling them with a color chosen by the user in Angular can be achieved by following these steps

Looking for advice on angular coding. I'm working on a project that requires creating multiple rectangles and filling them based on selected values. Can anyone suggest a module that would offer this functionality? ...

Fixing the Jquery animation glitch triggered by mouseover and mouseout

In my project, I have implemented a small mouseover and mouseout functionality. The challenge I am facing is that I need to keep the mouseout function using animate() instead of css() for specific reasons. The issue arises when I quickly do a mouseover fo ...

Ways to have a function return a promise from the final "then" in a series of promises

I am delving into the world of test automation with Selenium and JavaScript. As a newcomer to both, along with functional programming and promises, I am facing a challenge in creating a function that performs three essential tasks: Click on an input Clea ...

What is the best way to show an on/off button in an HTML page when it loads, based on a value stored in a MySQL database?

Is there a way to display a toggle button onload based on a value from a MySQL database table? I need the button to switch between 0 and 1 when clicked. I've looked at several solutions but none of them seem to work for me. Any help would be greatly a ...

Tips on triggering an AJAX call to load additional content when a user reaches the bottom of the page for the first time

My goal is to dynamically append an HTML file to a div element when the user reaches the bottom of the page. However, I have encountered an issue where the script appends the content multiple times after refreshing the page. It seems like the Boolean varia ...

Best Practices for Handling Form State in ReactJS with Redux

Currently in ReactJS + Redux, I am utilizing Material-UI's TextField for a form where users input their firstName, lastName, birthMonth, birthDay, and birthYear. The existing setup works but appears redundant, especially when handling the birth date f ...

What is causing the container to overlap with my sidebar-like <nav>?

Why is the fluid-container overlapping in this way, while others are not? Does this look correct to you, or am I overlooking something? It seems incorrect to me. <nav class="nav flex-column float-left"> {links} </nav> <div class="conta ...

AngularJS fails to prioritize the following element

Here is my HTML code: <div ng-app="app"> <form> <div class="form-group"> <label >Username</label> <input focus type="text" class="form-control" id="loginUserName" ng-model="userForm.crn" required/> ...

Filtering of currency with no zero in Angular1.x

I have a specific requirement to exclude rendering a currency value when it is zero. For example: v=1 display $1 v=0 display nothing [ UPDATE ] :oops: After examining the core code more closely, I discovered that there is a custom filter available. a ...

Selecting elements by class in jQuery using a variable for the class name

Is there a way in jQuery to apply actions to all elements with a specific class, where the class name is determined by a variable? I want to select elements based on this dynamically generated class name. var x = $(this).attr('href').slice(1); ...

.then function not functioning properly in Axios DELETE request in a React project

I am currently facing an issue with calling a function to update the array of notes after deleting a note from the database. The function causing the error is called deleteNote, and the function I intend to call within the .then promise is getNotes. Here i ...

The error message javax.ws.rs.NotFoundException: HTTP 404 Not Found pertains to Angular 1.5

Currently, I am working with Angular 1.5 code for a client application: var getGroupForUser = function () { var deferred = $q.defer(); $http.get(env.domain+'/Voices/authorize').then( function successCallback(respo ...

Verify the validation of the text box

Checking a textbox control for validation is necessary. Validation Criteria: Value should be between 0 and 1000, with up to 2 decimal places (e.g. 1.00, 85.23, 1000.00). Once 2 decimal points are used, users should not be able to enter additional ze ...

Begin a new countdown timer upon clicking the next button

Currently, I am developing a bid auction website and I have implemented a countdown timer script. The timer works perfectly on the initial window load. However, when I click on the restart button, it fails to reset the countdown timer to a new value. < ...

Apply the transition property to all elements except for the text inside a specific tag

I'm wondering if it's possible to apply a hover transition effect to everything in this tag except the text within it. The desired effect is for the button to fade away while the text remains visible. Below is the code I have written so far: ul ...

Using Vue.js, you can set a method to return data directly to

Having just begun my journey with Vue, I find myself in a bit of a predicament. As part of my learning process, I am developing an app for tracking episodes in TV series. The initial step involves searching for series and adding them to a database. When co ...

I'm curious about the process behind this. Can I copy a Figma component from a website and transfer it into my

Check out this site for an example: Interested in how uikit.co/explore functions? By hovering over any file, a copy button will appear allowing you to easily paste it into your Figma artboard. Want to know how this works and how to implement it on your o ...

The div needs to be positioned above another div, not beneath it

Just returning to the world of coding and struggling with a basic problem. Any help would be greatly appreciated. I am trying to position the 'header-top' (red) div at the top, and the 'header-bottom' (blue) div at the bottom. However, ...