react-data-grid icon indicating copy to clipboard operation
react-data-grid copied to clipboard

Custom styling for Cells

Open Tarun-1994-Kumar opened this issue 6 years ago • 9 comments

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

Tarun-1994-Kumar avatar Jan 24 '20 12:01 Tarun-1994-Kumar

I don't think it is possible with react data grid, but... you can achieve it with javascript through document.getElementBy... image

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);

emsi-iggy avatar Feb 20 '20 19:02 emsi-iggy

This worked. I also see the column formatter property which accepts row as a property and can format the cell

Tarun-1994-Kumar avatar Feb 24 '20 11:02 Tarun-1994-Kumar

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 avatar Apr 29 '20 02:04 amanmahajan7

@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

kawpii avatar Jan 12 '21 05:01 kawpii

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 avatar Feb 13 '22 17:02 kovasap

@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>
  );
}

CleanShot 2022-02-13 at 11 59 49

hope that's helpful!

mikereczek avatar Feb 13 '22 18:02 mikereczek

Thanks! That is helpful.

kovasap avatar Feb 13 '22 21:02 kovasap

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.
  }
}

kenklin avatar Apr 08 '22 04:04 kenklin

What is the respective solution for headerCellClass ? I want to set a list of classes but the type Column.headerCellClass is set as 'Maybe': Column<TRow<any>, TSummary>.headerCellClass?: Maybe<string>

What is funny is that it works when I ignore the typing

lucasalmeida92 avatar Mar 23 '23 18:03 lucasalmeida92