Modify the color of the table map based on specific conditions in a React.js application

To modify the table entry's color based on the current balance, follow these conditions: If the current balance is less than 100, it should be shown in red. If the current balance is between 100 and 200, display it in yellow. Otherwise, show it in green. Remember that the values are hardcoded, so if you change them, the colors will adjust accordingly.

See below for the code snippet:

import React from 'react';
import * as Icon from 'react-feather';
import PropTypes from "prop-types";
import { Link } from "react-router-dom";
import { Dropdown, Table, Badge } from 'react-bootstrap';

class ChurnCustomer extends React.Component {
    constructor(props) {
        super(props);
        this.state =
        {
            title: "New Users",
            users: [
                {
                    id: 1,
                    name: "Addison Mary",
                    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8b9b5b9aaa1989faab9b5b5b9aa94b1abacf6bbb7b5">[email protected]</a>",
                    phoneNo: '724313577059',
                    CurrentBalance: 356
                },
                {
                    id: 2,
                    name: "Rosemary Joe",
                    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="16647965737b77646f387c797356717b777f7a3875797b">[email protected]</a>",
                    phoneNo: '003135770259',
                    CurrentBalance: 125,
                },
                {
                    id: 3,
                    name: "Scarlett Skyler",
                    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c2f2f372530392e1c3b313d3530723f3331">[email protected]</a>",
                    phoneNo: '933135770134',
                    CurrentBalance: 49
                },
                {
                    id: 4,
                    name: "Victoria B.",
                    email: "victoriaBgmail.com",
                    phoneNo: '003357577009',
                    CurrentBalance: 195
                },
                {
                    id: 5,
                    name: "Philip",
                    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8dfde5e4e1e4fdcdeae0ece4e1a3eee2e0">[email protected]</a>",
                    phoneNo: '005613570459',
                    CurrentBalance: 249
                },
                {
                    id: 6,
                    name: "Zoe Nelms",
                    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b51444e6b4c464a424705484446">[email protected]</a>",
                    phoneNo: '923135770459',
                    CurrentBalance: 99
                }
            ]
        }
    }
    render() {
        const { users } = this.state;
        return (
            <div className='Customer-tables-div'>
                <div className="card mb-4">
                    <div className="card-body">
                        <div className="card-header d-flex">
                            <h5 className="card-title w-50 float-left">Churn Customer</h5>

                        </div>

                        <div className="height-310">
                            <Table className="m-0" responsive>
                                <thead>
                                    <tr>
                                        <th>Customer Name</th>
                                        <th>Email</th>
                                        <th>Phone No.</th>
                                        <th>Current Balance</th>
                                    </tr>
                                </thead>

                                <tbody>
                                    {users.map((user, idx) => (
                                        <tr key={idx} style={{color: user.CurrentBalance < 100 ? 'red' : (user.CurrentBalance >= 100 && user.CurrentBalance <= 200) ? 'yellow' : 'green'}}>
                                            <td>{user.name}</td>
                                            <td>{user.email}</td>
                                            <td>{user.phoneNo}</td>
                                            <td>
                                                {user.CurrentBalance}
                                            </td>

                                        </tr>
                                    ))}
                                </tbody>
                            </Table>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

export default ChurnCustomer;

https://i.sstatic.net/Hu2hu.png

You can update the code above to dynamically change the color of the "Current Balance" field based on the specified criteria.

Answer №1

Consider using inline styling in this way:

   {accounts.map((account, index) => {
        let background = account.Balance < 100 ? 'red' : (account.Balance <= 200 ? 'yellow' : 'green');

        return <tr key={index}>
            <td>{account.name}</td>
            <td>{account.email}</td>
            <td>{account.phoneNumber}</td>
            <td style={{background: background}}>
                {account.Balance}
            </td>

        </tr>
    }}

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

"Utilize AJAX to submit the value of the text box input from a jQuery slider when the Submit Button

Whenever I adjust the sliders, the value is shown in an input textbox. However, when I move the slider and check the values echoed from the textboxes on another PHP page, they are always displaying as 0. Even after clicking the submit button, it still echo ...

Is it possible to set a different default page index other than 0 in a material table using reactjs?

I have noticed that the default page index in the material table is set to '0', but the API I am currently using begins with a page index of '1'. Is there a way to adjust the default page index of the table to match that of the API? ...

What is the process for clearing the input value of a View from another View?

My situation seems simple and straightforward, yet I am struggling to achieve the desired output. In this scenario, I have two HTML pages named Index.htm and Main.htm, as well as a script page named mscript.js. Index.htm contains a button (Clear), and Mai ...

Guide to deploying a NextJs SSR React application on Microsoft Azure

Recently, I attempted to deploy a Server-side rendered react app that I developed using NextJS on Azure. Despite successfully setting up the Azure pipeline and release process, the app failed to load when I accessed the Azure website URL. One of the challe ...

Tips for embedding Javascript code within the window.write() function

I have a JavaScript function that opens a new window to display an image specified in the data variable. I want the new window to close when the user clicks anywhere on it. I've tried inserting some JavaScript code within window.write() but it doesn&a ...

Error encountered: Attempting to use a class as a function in vue-socket.io is not permitted

I am developing a Vue chrome extension where I am attempting to implement web sockets using vue-socket.io. I have followed the basic instructions on deploying a node server with express and socket.io on Heroku, but I am encountering issues with the conne ...

Modify a quartet of divs simultaneously

I am currently working on a project that involves 4 input fields, each accompanied by a dropdown element for selecting currency. My goal is to create a JavaScript function that will update all input fields when one of them is changed. For instance, if I s ...

Tips for initiating a component within a loop and terminating it after completing all 3 iterations

Looking for a solution to open and close tags in a loop every 3 iterations. The objective is to create a grid using container, row, and column elements. However, I am unsure how to achieve this. Example: render(){ const arrayName = ["john", " ...

utilizing the entire string rather than just a portion

I was attempting to create a JavaScript jQuery program that vocalizes numbers based on some previously saved data. However, I encountered an issue where only the last number in the sequence was being played (the final character in the string). Below is t ...

steps for linking a directive variable to a controller

Encountering an issue with 2-way binding in Angular where changes made to the input do not reflect in the controller. However, the initial value set by the controller does affect the directive. In the screenshot, a value was changed but vm.date still hold ...

How can I create a custom AppBar transition using Material-UI?

Is there a way to incorporate transitions into the AppBar element within Material-UI? I have tried adjusting the class properties, but unfortunately, I'm not seeing any animation. Can anyone pinpoint what the issue might be? To see the code in action ...

Achieving optimal vertical alignment with CSS and DIVs

I am struggling to vertically center my comments. Despite trying various methods, I have not been successful. Below is the code snippet along with a screenshot showcasing the issue. Can you identify what is causing the problem? .comment { min-height: ...

The matter concerning the intricacies of Rails, JQuery, Prototype, and RJS

I am exploring the integration of Jquery and ajax in rails 3.0.7, but I'm unclear on the current landscape regarding their usage together. There seems to be an abundance of hacks, plugins, and scripts available for utilizing JQuery. So: Is there an ...

Exploring the potential of utilizing functions in express.js to take advantage of the "locals"

Having experience with Rails/Sinatra, I am accustomed to using helper functions in my view files. However, incorporating this functionality into express.js has proven to be quite challenging. You can include locals automatically like this... app.set("nam ...

Sending data from jQuery modal to the final input field

In my latest project, I have developed a modal window that features a table with rows of input boxes and buttons: <table class="datatable tablesort selectable paginate full" width="100%"> <tbody> ...

Adjusting the color of a value in justGage requires a few simple steps to

Is it possible to modify the color and design of the text in the Value parameter of justGage after creating the gauge? My goal is to change the text color to blue with an underline to give it a link-like appearance. Appreciate your assistance. ...

What is the best way to distinguish the compiled files from the source code, while still being able to test and view Express views directly from the source?

I am embarking on a new project using node. I have chosen to organize my directory structure by keeping all source files under ./src and the files intended for server upload under ./dist. The semi-complete directory layout is displayed below. Once built, t ...

`Is there a way to retrieve the ID of an element upon clicking on it?`

Can you help me with a JavaScript query? I have two HTML div elements with ids of 'div1' and 'div2'. When I click on any of the divs, I want to display the id of the clicked div. Any thoughts? <div id="div1"></div> <div ...

Tips on sending an array to material-ui dataSource props

Currently utilizing material-ui for a component. I am facing an issue with the autocomplete component in material-ui where I intend to display a list of icon names along with the icons. When only passing MenuItem to dataSource, it results in an empty input ...

Receiving communication without the need for port forwarding

My goal is to establish a system where a server can send messages to clients at any given time, ensuring that when a message is sent, it is received almost immediately (ideally within 1 second or less). I am looking for a way to achieve this without having ...