Custom styling for Cells
Hi everyone, Is there a possibilty to custom style only particular cells. For example, I want to add a background color for cells (0,0) and cell(3,5).Is there a way to achieve it in React Data Grid? I am using react-data-grid v5.0.4
I don't think it is possible with react data grid, but... you can achieve it with javascript
through document.getElementBy...

For example: (https://codesandbox.io/s/rdg-cell-editing-xywxo?fontsize=14&hidenavigation=1&theme=dark)
import React from "react";
import ReactDOM from "react-dom";
import ReactDataGrid from "react-data-grid";
import "./styles.css";
const columns = [
{ key: "id", name: "ID", editable: true },
{ key: "title", name: "Title", editable: true },
{ key: "complete", name: "Complete", editable: true }
];
const rows = [
{ id: 0, title: "Task 1", complete: 20 },
{ id: 1, title: "Task 2", complete: 40 },
{ id: 2, title: "Task 3", complete: 63 },
{ id: 3, title: "Task 4", complete: 80 }
];
class Example extends React.Component {
state = { rows };
componentDidMount() {
this.setStylingForCells();
}
setStylingForCells() {
//lets say i want to color cell (0,0) and (2,1) depending on their value in property "complete":
//to get the cell (2,1) i need to calculate 2*(number of columns-1) + 1
//because i have all the cells in the 1 Dimensional array
//document.getElementsByClassName("react-grid-Cell") <-- this is the array
const indexOfCell00 = 0
const indexOfCell21 = (2*columns.length-1) + 1;
//if row number 0 has even value in "complete" column then set cell(0,0) backgroundColor to red, else (odd value) blue.
if(rows[0].complete % 2 === 0) {
document.getElementsByClassName("react-grid-Cell")[indexOfCell00].style.backgroundColor = "red";
} else {
document.getElementsByClassName("react-grid-Cell")[indexOfCell00].style.backgroundColor = "blue";
}
//if row number 2 has even value in "complete" column then set cell(2,1) backgroundColor to red, else (odd value) blue.
//here is different way -> instead of "rows[0].complete" you can do: "Object.values(rows[2])[1]"
//because we know that the property "complete" is third property
if(Object.values(rows[2])[1] % 2 === 0) {
document.getElementsByClassName("react-grid-Cell")[indexOfCell21].style.backgroundColor = "red";
} else {
document.getElementsByClassName("react-grid-Cell")[indexOfCell21].style.backgroundColor = "blue";
}
}
onGridRowsUpdated = ({ fromRow, toRow, updated }) => {
this.setState(state => {
const rows = state.rows.slice();
for (let i = fromRow; i <= toRow; i++) {
rows[i] = { ...rows[i], ...updated };
}
return { rows };
});
};
render() {
return (
<ReactDataGrid
columns={columns}
rowGetter={i => this.state.rows[i]}
rowsCount={rows.length}
onGridRowsUpdated={this.onGridRowsUpdated}
enableCellSelect={true}
/>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<Example />, rootElement);
This worked. I also see the column formatter property which accepts row as a property and can format the cell
If you are able to upgrade then you can use the new cellClass prop
https://github.com/adazzle/react-data-grid/blob/canary/CHANGELOG.md
@amanmahajan7 I have requirement I want to allow user to color cell as they want, can you please give me a code sample how to implement this using cellClass prop
I'm also interested in seeing an example of this. The CHANGELOG.md link above is broken. Could this feature maybe be added to the examples?
@kovasap here's one way of accomplishing that:
https://codesandbox.io/s/cold-mountain-6ibs0?file=/src/App.js
// using tailwind css classes
const rows = [
{ id: 1, name: "John", css: "bg-blue-50" },
{ id: 2, name: "Amy", css: "bg-red-50" },
{ id: 3, name: "Tim", css: "bg-green-50" }
];
const columns = [
{
key: "id",
width: 50,
name: "ID"
},
{
key: "name",
width: 250,
name: "Name",
cellClass: "px-0", // remove left and right padding
formatter: ({ row }) => {
return <div className={row.css}>{row.name}</div>;
}
}
];
export default function App() {
return (
<div className="App">
<DataGrid columns={columns} rows={rows} />
</div>
);
}

hope that's helpful!
Thanks! That is helpful.
reczy above wrote:
{ key: "name", width: 250, name: "Name", cellClass: "px-0", // remove left and right padding formatter: ({ row }) => { return <div className={row.css}>{row.name}</div>; } }
You can altogether eliminate the use offormatter above and use the function form of cellClass: (row). I was tipped off by this post by amanmahajan7
{
key: "name",
width: 250,
name: "Name",
cellClass: (row) => {
return ["px-0", row.css].join(" ") // e.g., "px-0", "px-0 bg-blue-50", etc.
}
}
What is the respective solution for headerCellClass ?
I want to set a list of classes but the type Column.headerCellClass is set as 'MaybeColumn<TRow<any>, TSummary>.headerCellClass?: Maybe<string>
What is funny is that it works when I ignore the typing