Ways to customize the background color of child cells in a table

I've implemented material-table in this UI and I'm currently working on customizing the style of specific cells only when the onTreeExpandChange is true.

Essentially, my goal is to change the background color for cells 2 & 3. Check out the screenshot below for reference:

https://i.stack.imgur.com/Z7B1v.png

Here's a snippet of the code:


{
    title: 'WEST',
    field: 'west',
    cellStyle: { textAlign: 'center' },
    headerStyle: { backgroundColor: '#006EB8' }
},
]}
data={[
    { id: 1, group_name: '1', northeast: 'Baran', central: 'ddd', west: '3' },
    { id: 2, parentId: 1, group_name: '2', northeast: 'Baran', central: 'ddd', west: '3' },
    { id: 3, parentId: 1, group_name: '3', northeast: 'Baran', central: 'ddd', west: '3' },
    { id: 4, group_name: '4', northeast: 'Baran', central: 'ddd', west: '3' },
    { id: 5, group_name: '5', northeast: 'Baran', central: 'ddd', west: '3' },
    { id: 6, group_name: '6', northeast: 'Baran', central: 'ddd', west: '3' },
]}
onTreeExpandChange={(row, rows) => {
    if (rows) {
    // Implement logic to change the backgroundColor of cell 2 & 3
    }
}
}}
parentChildData={(row, rows) => rows.find(a => a.id === row.parentId)}
options={{
    search: false,
    sorting: true,
    exportButton: true,
    paging: false,

Any suggestions on how to achieve this styling customization?

Thank you!

Answer №1

To style rows in a React component, you can utilize the options property and maintain a state that tracks row ids as you drill down.

Here is an example:

options={{
  rowStyle: rowData => ({
    backgroundColor: (this.state.expandedRow && this.state.expandedRow.indexOf(rowData.id) != -1) ? '#FF0' : '#FFF'
  })
}}

You will also need to have an array like this.state.expandedRow in your state where you store row ids during the onTreeExpandChange event.

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

Are background styles complete without incorporating quotations?

Let's say I want to include a background image, my code might look like this: background: url(/images/button_bg.png) repeat-x scroll left bottom #146BAE However, when I check the code in Firebug, it shows: background: url("/images/button_bg.png") r ...

The message "jest command not recognized" appears

My test file looks like this: (I am using create-react-app) import React from 'react'; import ReactDOM from 'react-dom'; import App from './components/Calculator'; import { getAction, getResult } from './actions/' ...

The canvas is responsive to keyboard commands, however, the image remains stationary and unaffected

As I delved into the basics, incorporating canvas, CSS, and JavaScript, a question arose: should the image be housed in the HTML or the JavaScript code? Following this, I added a background color to the canvas and defined properties of the canvas in JavaSc ...

Can you explain the meaning of -ms-value?

I recently came across a solution for an issue specific to IE, and it worked perfectly for me. You can find the solution here: Change IE background color on unopened, focused select box However, I'm still puzzled about the meaning of -ms-value. Any i ...

Issue with orientation change when using webkit-overflow-scrolling: touch;

I am currently facing an issue with a fixed position div that has a specified width. The problem arises when the content is long enough to require overflow in one device orientation (landscape), but not the other (portrait) - scrolling stops working if the ...

The notifications for events on Google Calendar are not being received by attendees' Gmail inboxes

I've been utilizing the Google Calendar API to insert events into Google Calendar. While the events are being added to attendees' calendars, I'm facing an issue where the notification of event creation is not showing up for both the attendee ...

Do you know the term for when JavaScript is utilized to display specific sections of a png image?

Imagine you have an image file in the format of a PNG which includes various icons intended for use on a website. How can JavaScript be utilized to choose and showcase a specific icon from that image? It's a technique I've observed in practice, b ...

What strategies can I employ to manage browser compatibility issues while utilizing contemporary JS and CSS frameworks?

I have been working on a project that utilizes the most recent versions of Angular (version 5) and Bootstrap (4). However, after a few weeks of development, I noticed that some features are not functioning properly on Safari, and I am uncertain if all fea ...

Is it possible to apply action.payload to multiple keys within an object?

My plan for updating tasks in my Todo app is as follows: addTask: (state, action) => { const newTask = { id: uniqueId(), text: action.payload, completed: false, date: action.payload, }; state.push(newTas ...

Using webpack-dev-middleware in combination with create-react-app

In my React/Node application, I have implemented webpack-dev-middleware and webpack-hot-middleware on the Node side to achieve automatic bundle reload when client files change. Additionally, I use nodemon to automatically reload the server when there are ...

The previous and next buttons are displaying side by side rather than being positioned at the outer edges

I am having an issue where the prev and next buttons are displaying next to each other instead of at the edge of the div. Can someone please assist me in fixing this problem? Below is the provided HTML code: <div id ="moreOption_80322271" class="m ...

Having trouble receiving any ringing status in nextjs while utilizing the getstream SDK

I attempted to integrate the getstream video SDK for calling from the caller to the callee. While I can successfully create calls from the caller side, I am not receiving any status updates about the call on the callee side. Below are my codes for the cal ...

Stop header background image from loading on mobile browsers by utilizing the <picture> tag

Is there a method to utilize the <picture> element in order to prevent a page from downloading an image when viewed on a mobile phone? I have a website that features a header image. Unfortunately, due to the site's functionality, using CSS (bac ...

Tips for sending a complicated data structure from React to Node using Axios

As a novice, I am facing challenges updating a record with a nested object using axios from react : Despite trying numerous methods to make it work, such as the commented-out examples below, I have not been successful. const saveBlend = async (layout, ...

Creating a Material UI Gmail Tasks Toolbar with a similar design

Hello, I'm interested in learning how to achieve a similar style to the new Gmail Tasks app. The new Gmail user interface is inspired by Material Design principles. As I am working on developing a Gmail Add-on using React and InboxSDK, I want to repl ...

Achieving a layered effect: Bootstrap Navbar on Carousel

Currently, I am designing a mockup and aiming to create a full-screen carousel with the navigation bar links positioned on top of the images. At the moment, the navbar appears transparent until I scroll down the image/slider making it full screen. How can ...

Bootstrap 3 offset doesn't respond to dimensions

Even though I have set an offset for a medium-sized screen, the offset still appears in large screens. Here is a snippet of my HTML code. How can I resolve this issue? <div class="col-lg-6 col-sm-12"> <div class="row"> <div class="co ...

Implementing react-navigation: Embed a Component on every screen

I am currently utilizing a StackNavigator combined with redux in the following manner. What is the most elegant approach to incorporating a Component into all of my screens? I attempted to add a child Component to both AppWithNavigationState and AppNavig ...

"Discover the art of repurposing classes and functions in React JS for maximum efficiency

As a beginner in using React, I often find myself wondering about how to effectively reuse classes and functions. While searching on Google for answers, I come across suggestions to use props for reusing functions or classes. However, is there another way ...

Unable to get CSS transition to function properly after adding a class using jQuery

I am trying to create a hover effect that shows an image when the mouse is over a specific div, but for some reason, the transition is not working as expected. I understand that using visibility: hidden cannot be animated. Here is the code snippet: $(d ...