Switch up colors in Angular Material table rows

Is there a way to dynamically change row colors based on the date order in a table?

In my mat table, I have a date field and I'm looking to highlight rows with the closest date in red and gradually transition to green for the furthest dates.

Answer №1

Within this Stack Overflow thread, you can find a method for blending two colors together. If you create a function like this:

  blendColors(colorA:string, colorB:string, amount:number) {
    const [rA, gA, bA] = (colorA.match(/\w\w/g) || []).map((c) => parseInt(c, 16));
    const [rB, gB, bB] = (colorB.match(/\w\w/g) || []).map((c) => parseInt(c, 16));
    const r = Math.round(rA + (rB - rA) * amount).toString(16).padStart(2, '0');
    const g = Math.round(gA + (gB - gA) * amount).toString(16).padStart(2, '0');
    const b = Math.round(bA + (bB - bA) * amount).toString(16).padStart(2, '0');
    return '#' + r + g + b;
  }

You can apply the function to set the background color of HTML elements like so:

  <tr mat-row 
    [style.background-color]="blendColors('#FF0000','#00FF00',i/dataSource.length)"
      *matRowDef="let row; let i=index; columns: displayedColumns;">
  </tr>

This will result in a gradient effect from red to green going downwards on each row. To customize the color transition further, consider adjusting the formula within the blendColors function by changing the value passed in place of i/dataSource.length.

For a practical demonstration, check out the code on StackBlitz.

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

Following the migration to Typescript, the React component is having trouble locating the redux store props and actions

Here is the structure of my app: export default class App extends Component { render() { return ( <Provider store={store}> <Router> <Header/> ...

What steps can I take to create a responsive jQuery UI buttonset?

I am currently facing a challenge with my web app (http://www.tntech.edu/cafe-menu.php) which is being iframed into a mobile application developed by ATT. The button sizes vary on different phone models, and I am seeking a solution to make them responsiv ...

Combining Multiple .ts Files into a Single File: A Simplified Application Structure with TypeScript 1.8

Currently, I am in the process of developing an Electron application and I have decided to implement TypeScript for this project. While TypeScript essentially boils down to JavaScript in the end, my familiarity with it makes the transition seamless. As of ...

htmlEnhance your webpage with a native-like combobox

<!DOCTYPE html> <html> <body> <form action="demo_form.asp" method="get"> <input list="browsers" name="browser" placeholder="choose browser"> <datalist id="browsers"> <option value="Internet Explorer"> ...

Loading identical code in two different div elements

I am in the process of designing a comprehensive resource database featuring a side-scrolling container. Once a user clicks on a thumbnail within the container, the contents of a corresponding div will fade in and display specific category content. Each di ...

Remove old photo when uploading new one in Django

I'm currently working on a user profile feature that allows users to upload profile pictures. The uploading process is functioning correctly, but I'm encountering an issue where the old photo is not replaced when a new one is uploaded. I'm u ...

Having issues with Angular CLI functionality

Recently, I have been diving into learning Angular 4. To start off, I installed Node.js and executed the following commands in the Node.js command prompt: npm install -g typescript npm install -g @angular/<a href="/cdn-cgi/l/email-protection" class="__ ...

The timer is malfunctioning

I am new to JavaScript and looking to create a simple countdown. I came across this script: http://codepen.io/scottobrien/pen/Fvawk However, when I try to customize it with my own settings, nothing seems to happen. Thank you for any assistance! Below is ...

Switch between showing the Font Awesome TitleText and its associated Class with a single click

Can the value of the title attribute for <i> be toggled? For example, if the title is set to <i title="Favorite This Post" class="fa fa-star-o" aria-hidden="true"> within <div class="postoption"> I would like to toggle both the title te ...

Ways to emphasize the four edges of a table cell using border-collapse collapse

I am trying to emphasize the borders of cells with the class active. The challenge arises from the fact that the table's border-collapse property is set to collapse, causing the top and left borders of cells (except for the leftmost and top row cells ...

The name 'XXX' is nowhere to be found

I encountered an error stating "Cannot find name 'Calendar Component'" while attempting to add a route to a component from another module in my app.module.ts file. Below is the content of my app.module.ts file: // Importing Modules // import {B ...

Tips for accessing and manipulating an array that is defined within a Pinia store

I have set up a store to utilize the User resource, which includes an array of roles. My goal is to search for a specific role within this array. I've attempted to use Array functions, but they are not compatible with PropType<T[]>. import route ...

angular2 fullCalendar height based on parent element

Currently, I am using angular2-fullcalendar and encountering an issue with setting the height to 'parent'. The parent element is a div but unfortunately, it does not work as expected. The navigation bar appears fine, however, the calendar itself ...

"Is there a way to target an element within another element that has inline styles using

What is the best CSS strategy for targeting elements with inline styles? How can I focus on the first span but exclude the second using only CSS? If that's not achievable, is it possible to accomplish this using jQuery? http://jsfiddle.net/TYCNE/ & ...

Content from ajax request is invalid

Currently, I am facing an issue with loading a comment list and replacing the existing list on my HTML page with the new one. Previously, I used to load a simple XML list, iterate over its elements, and generate content. Although this method worked fine, i ...

Determine the status of the menu in React select by checking on key press events if it is open

In my project, I am utilizing react-select for the select elements. I'm curious if there's a way to detect the "open" or "closed" state of the menu through a keydown event in the select (either on the select input or within the menu). The versi ...

Tips on implementing conditional validators in Angular's formBuilder

Is it possible in Angular 7.x to use formBuilder and ReactiveForms to apply a validator to a form based on the user's role? For instance, if the user has a specific role, they would be required to input certain fields. The user information is stored i ...

What are the steps for integrating angular2 starter with express?

Can someone explain how to integrate the following Angular starter template into an Express framework? https://github.com/gdi2290/angular-starter I am able to start the webpack dev server without any issues, but I would like to utilize additional librari ...

Creating a dynamic routing system for passing data easily

I am interested in sending the filters property from search-products.component.ts to ProductsListComponent using routing. Can this be done? Currently, I can pass static data but how about dynamic data? Below is a snippet of my demo code: app.routing.ts ...

Find all elements located in between two headers with varying ids

I am trying to select a specific range of elements in between two headers, excluding everything after the second header. For example, I want to select elements 1-5 from the following code snippet: <!DOCTYPE html> <html> <head> <link r ...