Mui-datatables: Customize color of table headers

Created on 14 Apr 2020  路  6Comments  路  Source: gregnb/mui-datatables

I need to customize the colors of the two table headers:

  1. The one that has the the search, print, save buttons, and the table title, and

  2. The header that has the column labels.

Here's an example:

Screen Shot 2020-04-14 at 6 08 41 PM

I'm working with a a theme,

const getMuiTheme = () => createMuiTheme({ ...

but I'm not sure which components are responsible for each header.

Thanks for your help.

Most helpful comment

Thanks @patorjk ! I had also worked out a few of them, but didn't know all of the ones you listed.

Pasting my own code snippet in case it's useful to others:

getMuiTheme = () => createMuiTheme({
    overrides: {
        // handles table title bar color
        MUIDataTableToolbar: {
            root: {
                backgroundColor: Theme.tableHeader,
                color: Theme.textPrimary
            }
        },
        // handles table data header color
        MUIDataTableHeadCell: {
            fixedHeaderCommon: {
                backgroundColor: Theme.tableDataHeader
            }
        },
        // handles data row color; clashes and blocks MuiTableRow
        // MUIDataTableBodyCell: {
        //  root: {
        //      backgroundColor: Theme.tableDataRow,
        //      color: Theme.textPrimary,
        //      // borderBottom: "none"
        //  }
        // },
        // handles data footer color
        MUIDataTablePagination: {
            root: {
                backgroundColor: Theme.tableFooter,
                color: Theme.textPrimary
            }
        },
        // handles row hover color and selected row color
        MuiTableRow: {
            hover: { '&$root': { '&:hover': { backgroundColor: Theme.tableRowHoverColor }, } },
            root: {
                '&$selected': {
                    backgroundColor: Theme.tableRowSelectColor
                }
            }
        },
    }
})

Note: Theme.* is my stylesheet object.

All 6 comments

Great, this answers many of my questions. Thank you very much @wdh2100.

@Eithcowich : did you figure out which component styles were to be overwritten? Could you post the same here, it would be useful for me and others. Thanks.

@appukuttan-shailesh sorry, I did not figure out how to do this. You can look at the example @wdh2100 posted above, which is good and has a lot of info, but that's pretty much it so far.

In the example mentioned, try these overrides: (tested in material-ui v4)

  getMuiTheme = () => createMuiTheme({
    overrides: {
      MUIDataTable: {
        root: {
          backgroundColor: "#AAF",
        },
        paper: {
          boxShadow: "none",
        }
      },
      MUIDataTableBodyCell: {
        root: {
          backgroundColor: "#FFF"
        }
      },
      MuiToolbar: {
        root: {
          backgroundColor: '#f00'
        }
      },
      MuiTableCell: {
        head: {
          backgroundColor: 'purple',
        }
      },
      MUIDataTableSelectCell: {
        headerCell: {
          backgroundColor: 'blue',
        }
      },
    }
  });

The last 3 overrides target the toolbar and the header cells.

Thanks @patorjk ! I had also worked out a few of them, but didn't know all of the ones you listed.

Pasting my own code snippet in case it's useful to others:

getMuiTheme = () => createMuiTheme({
    overrides: {
        // handles table title bar color
        MUIDataTableToolbar: {
            root: {
                backgroundColor: Theme.tableHeader,
                color: Theme.textPrimary
            }
        },
        // handles table data header color
        MUIDataTableHeadCell: {
            fixedHeaderCommon: {
                backgroundColor: Theme.tableDataHeader
            }
        },
        // handles data row color; clashes and blocks MuiTableRow
        // MUIDataTableBodyCell: {
        //  root: {
        //      backgroundColor: Theme.tableDataRow,
        //      color: Theme.textPrimary,
        //      // borderBottom: "none"
        //  }
        // },
        // handles data footer color
        MUIDataTablePagination: {
            root: {
                backgroundColor: Theme.tableFooter,
                color: Theme.textPrimary
            }
        },
        // handles row hover color and selected row color
        MuiTableRow: {
            hover: { '&$root': { '&:hover': { backgroundColor: Theme.tableRowHoverColor }, } },
            root: {
                '&$selected': {
                    backgroundColor: Theme.tableRowSelectColor
                }
            }
        },
    }
})

Note: Theme.* is my stylesheet object.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ronaiza-cardoso picture ronaiza-cardoso  路  3Comments

naothomachida picture naothomachida  路  3Comments

mhmmdakbr picture mhmmdakbr  路  4Comments

gangakrishh picture gangakrishh  路  3Comments

alexanderwhatley picture alexanderwhatley  路  4Comments