-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DataGrid] Customize row className #1298
Comments
@erikian There isn't enough information. Please provide a reproduction of what you have done so far, It would help a lot to understand the problem you are trying to solve |
Sorry for the lack of information, let's try again 😃 Each table row represents a device. My goal is to add an Here's a better example: import { DataGrid } from '@material-ui/data-grid'
import { useCallback, useEffect } from 'react'
import styled from 'styled-components'
interface Device {
id: number
status: 'online' | 'offline'
}
interface IProps {
devices: Device[]
}
const MyCurrentTable = (props: IProps) => {
const { devices } = props
const columns = [
{
field: 'id',
headerName: 'Device ID',
},
{
field: 'status',
headerName: 'Status',
}
]
// I'm storing the ids of all offline devices in this array
const offlineDevices = useCallback(() => {
return devices
.filter(({ status }) => status === 'offline')
.map(({ id }) => id)
}, [devices])
// whenever props.devices changes, I need to do this to add/remove the 'offline' class
useEffect(() => {
document
.querySelectorAll('#dataGridWrapper .MuiDataGrid-row[data-id]')
.forEach(row => {
// the data-id attribute is provided by the DataGrid component itself (through the field `id` in the columns definition)
const id = +row.getAttribute('data-id')
// this device is now offline
if (offlineDevices.includes(id)) {
row.classList.add('offline')
}
// this device is no longer, or has never been, offline
else {
row.classList.remove('offline')
}
})
}, [devices, offlineDevices])
return (
<div id="dataGridWrapper">
<DataGrid rows={devices} columns={columns} />
</div>
)
}
export default styled(MyCurrentTable)`
.MuiDataGrid-row {
// here's what I want to do with my class
&.offline {
background: red;
}
}
` Ideally, I'd be able to specify a import { DataGrid } from '@material-ui/data-grid'
import styled from 'styled-components'
interface Device {
id: number
status: 'online' | 'offline'
}
interface IProps {
devices: Device[]
}
const MyNewAndImprovedTable = (props: IProps) => {
const { devices } = props
const columns = [
{
field: 'id',
headerName: 'Device ID',
},
{
field: 'status',
headerName: 'Status',
}
]
const rows = devices.map(device => ({
...device,
// here's the magic
className: device.status === 'offline' ? 'offline' : 'online',
}))
return (
<div id="dataGridWrapper">
<DataGrid
rows={rows}
columns={columns}
/>
</div>
)
}
export default styled(MyNewAndImprovedTable)`
.MuiDataGrid-row {
&.offline {
background: red;
}
}
` |
You can add a css class to a cell using |
@dtassone You're right, it would work if I did something like this: <DataGrid
rows={rows}
columns={columns.map(column => ({
...column,
cellClassName: ({row: device}) => device.status,
}))}
/> While this works for my specific case, adding a class to every single cell still looks a bit hacky. Also, if I needed to style the entire row in some other way (like a border, linear-gradient background or something), this would not work, so I guess this feature still is a nice-to-have. |
To add more context.
export default styled(DataGrid)`
.MuiDataGrid-row {
background: red;
}
`); which @erikian already does.
Maybe we should add the "waiting for more upvotes" label? |
I think that we should add "rowClassName", "headerClassName", "cellClassName" should have the same signature for consistency (params => string). |
This is definitely necessary. I'm working on a trading application and I need to show green rows for profit, red rows for loss, gray rows for inactive etc. I'm currently using |
Summary 💡
I haven't found a way to customize the class of a row in the DataGrid component based on row data and I think this feature would be extremely useful.
Examples 🌈
My DataGrid is being used to display a set of devices. I want to add a specific className to each row so I can style it based on the properties of each device (eg: I want a row with red background for offline devices, green background for online devices, upside down for devices with names starting with A...). Code-wise, it would look something like this:
Motivation 🔦
Currently, in order to add an
offline
class to a row, for instance, I need to traverse the DOM whenever my devices change (which is quite often) and use each row'sdata-id
attribute to check if the device with that ID is offline in order todomNode.classList.add('offline')
ordomNode.classList.remove('offline')
, and we all know how bad, terrible, cumbersome, no-good, root-of-all-evil traversing the DOM is.The text was updated successfully, but these errors were encountered: