Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the text color of the selected row in material ui table

I am trying to change the color of the row text and the background color of row on selection.

I am able to change the background color successfully but I am not able to change the text color.

<TableRow
        className={classes.tableBody}
      >

tableBody: {
    "&:focus": {
      color: "yellow !important",
      backgroundColor: "#3D85D2 !important",
    },
  },
like image 895
Ankita Avatar asked Apr 25 '19 15:04

Ankita


People also ask

How do I change the text row color in a table?

If you want to change the color of a single line of text, you can use the "<font>" tag to define the color. Alternatively, if you have multiple cells or rows or want to change the color of the entire table, you can use CSS style tags.

How do I change the color of my Mui table?

We need to apply background uniquely to the first cell of the header, then uniformly to the rest of the header, and uniformly to the first column of the table body. In the TableRow component I set background color using the sx prop. Then in the first child TableCell, I set a difference background color.

How do I change the background color of a row in a table?

HTML | <tr> bgcolor Attribute The HTML <tr> bgcolor Attribute is used to specify the background color of a table row. It is not supported by HTML 5. Attribute Values: color_name: It sets the background color by using the color name.


2 Answers

The background color is controlled in TableRow. In order to get the correct specificity (you shouldn't ever need to leverage "!important" when overriding Material-UI styles), you need to leverage the "hover" class similar to what is done within TableRow.

The color is controlled in TableCell, so that is the level where you need to control it.

For a working solution, in the styles you would have something like:

const styles = theme => ({
  tableRow: {
    "&$hover:hover": {
      backgroundColor: "blue"
    }
  },
  tableCell: {
    "$hover:hover &": {
      color: "pink"
    }
  },
  hover: {}
});

then in the rendering:

            <TableRow
              hover
              key={row.id}
              classes={{ hover: classes.hover }}
              className={classes.tableRow}
            >
              <TableCell
                className={classes.tableCell}
                component="th"
                scope="row"
              >
                {row.name}
              </TableCell>

Here's a working version based on your sandbox:

Edit Table hover colors

Here's a similar example, but using "selected" instead of "hover":

https://codesandbox.io/s/llyqqwmr79

This uses the following styles:

const styles = theme => ({
  tableRow: {
    "&$selected, &$selected:hover": {
      backgroundColor: "purple"
    }
  },
  tableCell: {
    "$selected &": {
      color: "yellow"
    }
  },
  selected: {}
});

and some state:

 const [selectedID, setSelectedID] = useState(null);

and changing the TableRow rendering to be:

            <TableRow
              hover
              key={row.id}
              onClick={() => {
                setSelectedID(row.id);
              }}
              selected={selectedID === row.id}
              classes={{ selected: classes.selected }}
              className={classes.tableRow}
            >

v4 of Material-UI will include some changes that should make overriding styles considerably easier (and easier to figure out how to do successfully without looking at the source code).

In v4 of Material-UI, we can use the global class names for the selected state ("Mui-selected") and for TableCell ("MuiTableCell-root") and then we only need to apply a single class to TableRow:

const styles = (theme) => ({
  tableRow: {
    "&.Mui-selected, &.Mui-selected:hover": {
      backgroundColor: "purple",
      "& > .MuiTableCell-root": {
        color: "yellow"
      }
    }
  }
});

Edit Table hover colors (forked)

like image 132
Ryan Cogswell Avatar answered Sep 29 '22 10:09

Ryan Cogswell


const useStyles = makeStyles((theme) => ({
 selected: {
    backgroundColor: "green !important",
    "&:hover": {
      backgroundColor: "green !important",
    },
  },
}));

 const classes = useStyles();
<TableRow selected classes={{selected: classes.selected,}}/>
like image 27
SaimumIslam27 Avatar answered Sep 29 '22 09:09

SaimumIslam27