Customizing Background Color in React Bootstrap Table Based on Value

Having a little issue that's causing me some trouble. I have a React Bootstrap Table displaying data from an API, and I'm looking to enhance it by changing the row color to green if a specific value is greater than zero. Here is an example:

const TableComponent = ({ fixtures }) => {
    return (
        <Table>
            <tbody>
                {fixtures.map((fixture) => (
                    <tr
                        key={fixture.id}
                        style={{
                            backgroundColor: 'green'
                        }}
                    >
                        <td> {fixture.value1} </td>
                    </tr>
                ))}
            </tbody>
        </Table>
    );
};

Currently, the row's backgroundColor is set to green by default. Is it feasible to create a function so that if fixture.value2 or fixture.value3 is greater than zero, the row's backgroundColor remains green, but reverts to default otherwise?

Answer №1

This solution has been successful for me. Here is an alternative approach to consider.

const DisplayData = ({ data }) => {
    return (
        <Table>
            <tbody>
                {data.map((item) => (
                    <tr
                        key={item.id}
                         style={item.value2>0|| item.value3>0?{backgroundColor:'green'}:{}}
                    >
                        <td> {item.value1} </td>
                    </tr>
                ))}
            </tbody>
        </Table>
    );
};

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

Create a generic function that retrieves a specific property from an array of objects using the select method

Currently, I have implemented four functions that select entries from an array based on different properties. if ($scope.filters.filter1) $scope.filteredEntries = $scope.filteredEntries.filter(function (o) { return o.field1 === $scope.filt ...

Error: The command you are looking for does not exist in

Every time I attempt to execute npm create-react-app, I encounter an error stating it is an unknown command. How can I resolve this issue? Microsoft Windows [Version 10.0.22631.3527] (c) Microsoft Corporation. All rights reserved. G:\Project\f ...

Attempting to add additional content in the "prepareHeaders" method within React Toolkit Query

After spending a considerable amount of time struggling with a problem, I've turned to seeking help here. The issue revolves around populating the extra property of the prepareHeaders function, and I'm unsure about where to define it. I've ...

What is the best way to pass the value from one textfield to another using material UI in a react application?

I'm looking to transfer the content from a text field in a dialog window to another text field that is not embedded. However, instead of transferring the actual text field value, I'm getting an output of "object Object". Can you help me figure ou ...

Retrieve the .blob file stored on your device

While working on crafting a birthday gift for a friend, I came across an IPA file which I unzipped, only to discover that the picture assets I needed were packed inside a .blob file. While analyzing the hex representation of the file, it appears that the i ...

Tips for avoiding the display of concealed forms on a webpage

Hey there, I'm just starting out with html forms and currently experimenting with Jquery to hide forms upon loading the page. However, I've encountered an issue where some forms briefly appear before hiding after the page finishes loading. Here& ...

Utilizing CSS in Angular applications

I am currently working on an Angular 2 application and I am fairly new to Angular. I am facing an issue where my CSS does not seem to be applying properly. There are three key files involved: landing.component.html landing.component.scss landing.compone ...

Unforeseen outcomes of JavaScript when using the let and var keywords

In JavaScript, when using the var keyword to declare a variable, the JS engine assigns a default value of "undefined" at creation stage. console.log(message); // undefined var message = "My message"; However, with the let keyword: console.log(message); ...

Directive for integrating Amcharts with Angular JS

I have created a custom directive specifically for displaying a single chart using Amcharts. angular.module("App").directive('myElem', function () { return { restrict: 'E', replace:true, temp ...

Retrieve the id of the clicked hyperlink and then send it to JQuery

<a class = "link" href="#" id = "one"> <div class="hidden_content" id = "secret_one" style = "display: none;"> <p>This information is confidential</p> </div> <a class = "link" href="#" id = "two" style = "display: non ...

Unable to retrieve value from a hidden input field using JavaScript

My goal is to retrieve a value from a hidden inputbox using JavaScript. However, I am encountering issues where sometimes I receive an "undefined" error and other times there is no output at all. When I utilize alert(document.getElementById('hhh& ...

What is the best way to handle multiple responses in Ajax within a single function?

Here is a simple code snippet: $.ajax({ url:'action.php', method: 'POST', data:{getcart:1}, success:function(response){ $('#getcart').html(response);//want to ...

Preventing specific time intervals from being selected in the MUI time picker component of a ReactJS application

Can MUI time picker disable specific minutes within certain hours? For instance, I'd like to prevent the selection of minute 30 in hour 5 only. Users should not be able to choose the time 5:30, but they should still have the option to select the 30th ...

Is there a way to determine if jQuery lightslider has been initialized, and if so, how can I effectively remove the instance?

Currently, I have integrated the JQuery lightSlider into my project. Despite some code adjustments, it is functioning well. My goal is to dynamically replace the lightSlider content with data returned via AJAX, which I have successfully achieved. After r ...

What is the method to modify the background color of el-pagination?

I am using el-pagination on a dark page and I want to make its background color transparent. When I do not use the 'background' prop, the background color of el-pagination is white. Here is how it looks: (sorry I can't add an image) htt ...

user interface grid element in Materia

After writing this code, I encountered the following error: No overload matches this call. Overload 1 of 2, '(props: { component: ElementType<any>; } & SystemProps<Theme> & { children?: ReactNode; classes?: Partial<GridClasses>; .. ...

Leveraging window.print() in a React application: the best practices for implementation without triggering deprecation warnings

I'm currently utilizing window.print() within a React JS app. My goal is to have the print modal trigger after the data has been downloaded and the content rendered. When using Chrome, I noticed a warning in the console: The use of 'print()&a ...

Modify the text or background shade of the Bootstrap date picker

I have successfully integrated the bootstrap date picker (view figure below) and it is functioning as expected. The code appears like this: <div class="input-group date"> <input name="departure" type="text" class="form-control" id="departure" ...

Send a request to the uClassify API using the Node request module

I'm currently working on integrating the uClassify API into my Node project, but I'm encountering some issues with my code. Here's what I have so far: const req = JSON.stringify('Hello, my love!'); const options = { body: ...

Tips for deciding on the appropriate CSS for an Angular 6 navbar component

I am currently working on an angular 6 application where users are assigned different roles that require distinct styling. Role 1 uses Stylesheet 1, while Role 2 uses Stylesheet 2. The Navbar component is a crucial part of the overall layout structure of ...