Search code examples
reactjsmaterial-uimaterial-table

divide/split a column into two parts and style each part in material table?


I am trying to divide material table column into two parts but It is not working. I was trying to apply custom css for a specific data but again Not working. here is my code.

<MaterialTable
  title=""
  columns={[
    {
      title: "SCENARIO",
      field: "scenario",
      customSort: (a, b) => a.scenario.length - b.scenario.length,
    },
    {
      title: "SCORE",
      field: "score",
      type: "numeric",
      cellStyle: {
        backgroundColor: "#e1f5f8",
        color: "#000",
        borderRight: "2px solid #000",
        fontWeight: "bold",
      },
    },
    { title: "FILL RATE", field: "fillRate", type: "numeric" },
    {
      title: "REVENUE",
      field: "revenue",
      type: "numeric",
    },
    { title: "COST TO SERVE", field: "costToServe", type: "numeric" },
    { title: "MARGIN", field: "margin", type: "numeric" },
    {
      title: "IMPACTED DEMAND",
      field: "impactedDemand",
      type: "numeric",
    },
  ]}
  data={[
    {
      scenario: "Live -04-25-2020",
      score: 7.6,
      fillRate: 97.5,
      revenue: 63,
      costToServe: 0,
      margin: 6.9,
      impactedDemand: 0,
    },
    {
      scenario: "Live",
      score: 7.0,
      fillRate: 92.5,
      revenue: 63,
      costToServe: 0,
      margin: 6.9,
      impactedDemand: 0,
    },
    {
      scenario: "Recomended",
      score: 7.1,
      fillRate: 97.5,
      revenue: 63,
      costToServe: 0,
      margin: 6.9,
      impactedDemand: 0,
    },
    {
      scenario: "Override",
      score: 7.0,
      fillRate: 97.5,
      revenue: 63,
      costToServe: 0,
      margin: 6.9,
      impactedDemand: 0,
    },
    {
      scenario: "ML",
      score: 6.9,
      fillRate: 97.5,
      revenue: 63,
      costToServe: 0,
      margin: 6.9,
      impactedDemand: 0,
    },
  ]}
  options={{
    sorting: true,
    search: false,
    paging: false,
    fixedColumns: {
      left: 2,
    },
  }}
 />

How Can I divide a specific column data into two parts and apply css on that column ? Please help me as I am new in material-table.


Solution

  • I'm not sure this is exactly what you need, but since there is no out-of-the-box way to do this directly inside material-table, you can use a custom render and create a <Grid> element inside your cell:

    render: rowData => {
      return (
        <Grid container spacing={0} className={classes.rateRevContainer}>
          <Grid item xs={6} className={classes.rateCol}>
            {rowData["fillRate"]}
          </Grid>
          <Grid item xs={6} className={classes.revCol}>
            {rowData["revenue"]}
          </Grid>
        </Grid>
      );
    

    You can see a full working version here: https://codesandbox.io/s/material-table-split-column-ihdty?file=/demo.js