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

Developing a hovercard/tooltip feature

I'm currently working on developing a hovercard feature for a social media platform I'm building. The concept involves displaying additional information about a user when their name is hovered over. The hovercard will appear with the user's ...

TransitionGroup with CssTransition fails to execute exit transition

After making the switch from the outdated CSSTransitionGroup to the newer react-transition-group library for CSSTransition and TransitionGroup, I encountered an interesting challenge. I've been tinkering with creating an overlay loader, aiming to add ...

How can I display a "loading..." message as a temporary placeholder while waiting for my Apexcharts to load?

I've spent a day trying to solve this issue but couldn't find a solution. Any help would be greatly appreciated. Recently, I was working on creating a cryptocurrency tracker in React. I successfully built a table that displays multiple currencie ...

Verifying a checkbox selection within an Autocomplete feature using MUI

[ { label: 'First', checked: false }, { label: 'Second', checked: true } ] Here is a brief example of how the data may be structured. I am utilizing Material UI's Autocomplete feature to enable searching based on labels. Thes ...

Retrieve an SVG file from the internet and incorporate it as a <svg /> element within a React component in a Next.js project

I have a situation where I am trying to style .svg files within an API being used by a NEXT.js application. Currently, the files are loaded using <img src="https://api.com/image.svg" />, but this method does not allow for styles to be appl ...

Tips on how to prevent certain classes from being impacted by a hue-rotate filter applied to all elements on a webpage

I am currently in the process of adding a feature that allows users to choose between a dark or light theme, as well as select a specific theme color for the app. The implementation involves using CSS filters such as invert(1) for the dark theme and hue-ro ...

Translating PCRE(PHP) regular expressions into ECMAScript(Javascript) syntax

I have this PCRE Regex that I'm using to validate JSON strings, but now I need to convert it to JavaScript so I can use it for validation in a React application. PCRE Regex - /(?(DEFINE) (?<json>(?>\s*(?&object)\s*|\s* ...

Troubleshooting problem with AngularJS ng-repeat

Seeking assistance with an angularjs issue as a newcomer to the platform. Building a photo gallery where the initial page displays thumbnail photos from twitter and instagram using ng-repeat loop. Hovering over an image fades it out, revealing a button f ...

When the mouse leaves, the gauge chart component's size will expand

I have encountered a problem while using the react-gauge-chart library in my react project. Within the project, I have integrated a popover component using material-ui and incorporated the gauge chart component from the library within the popover modal. T ...

Conflict between the top menu and body on the HTML page is causing issues

Encountered an issue with my HTML code: CSS: .fixedmenu { overflow: hidden; background-color:rgb(153,0,51); position: fixed; /* Ensures the navbar stays in place */ top: 0; /* Positions it at the top of the page */ width: 100%; /* Occ ...

When navigating to the next page, MUIDataTable displays a "no matching records exist" message for server-side pagination, despite the data being successfully returned in the network tab

In my next js app, I am utilizing MUIDatatable with server-side pagination. The issue I am facing is that even though the data is correctly returned by the API for each page change, it only displays on the first page. When I try to navigate to the next pag ...

What is the best way to store a small number of files in the state

I have recently implemented Drag and Drop functionality, and now I am facing an issue where I need to save a few files in state. const [uploadedFiles, setUploadedFiles] = useState<any[]>([]); const onDropHandler = async (e: React.DragEvent<HTMLDi ...

Is it advisable to use an if statement or question mark in TypeScript to prevent the possibility of a null value?

Currently delving into TypeScript and exploring new concepts. I encountered a scenario where inputRef.current could potentially be null, so I opted to directly use a question mark which seems to work fine. However, in the tutorial video I watched, they use ...

Experience the dynamic expansion of a droppable div as you elegantly drop an element within

When the questions "question1" and "question2" are dragged into "questionslots," I want the "questionSlots" div to expand its size dynamically when the second question is dropped into it. This means that both questions should be accommodated and visible wi ...

Using the material-ui checkbox in combination with react-hook-form

Struggling with setting the value of a material-ui checkbox programmatically using react-hook-form's reset method. While my TextField is behaving as expected, the CheckBox is not responding. Here is an example of my code: Check out the code here ...

Is there a way to adjust my input value to correspond to December of 2022?

I am working on a project where I need to set a default month and year that the user cannot modify. They should only be able to change the date in December months. Any tips on how I can achieve this? <input id="input_date" type="date&qu ...

Is it considered acceptable in React for the value of one state to be determined by another state?

Consider this scenario: state1 tracks the mouseover of clientX and clientY, while state2 retrieves the value from state1 upon clicking. Is this implementation acceptable? const [move,setMove]=useState([]) const [click,setClick]=useState([]) useEffect(() ...

Customizing Material-UI styles using makeStyles

Currently, I am in the process of building a small portfolio website. Despite my attempts to style an avatar using the makeStyles class, it seems that the Mui avatar root is overriding my desired styling. I am wondering if there is a way to achieve this w ...

What could be the reason for a particular product edit page showing up completely blank?

In my ongoing project, I am developing an admin panel that allows administrators to add new products to their website. These products are then stored in a Firestore database and managed using Redux Toolkit. The added products can be viewed and edited in th ...

Having trouble changing a CSS property (margin-right)?

I have created a simple HTML list that consists of various items: <div id="menu"> <ul> <li>apples</li> <li>&#149</li> <li>bananas</li> <li>oranges</li> <li>grape ...