Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Col span and Row span in material-table header React

I am trying to use the material-table and I would like to use colspan and rowspan in the material-table. I already search for example and sample but I don't see anything.

Currently, I use to like that in the material table

<TableHead>
                      <TableRow>
                        <TableCell rowSpan={2}>Approve</TableCell>
                        <TableCell rowSpan={2} align="center">Date</TableCell>
                        <TableCell rowSpan={2} align="center">Emp id</TableCell>
                        <TableCell rowSpan={2} align="center">Emp Name</TableCell>
                        <TableCell rowSpan={2} align="center">Shift</TableCell>
                        <TableCell rowSpan={2} align="center">Cost Center</TableCell>

                        <TableCell colSpan={2} align="center">In</TableCell>
                        <TableCell colSpan={2} align="center">Out</TableCell>

                        <TableCell rowSpan={2} align="center">Action</TableCell>
                      </TableRow>
                      <TableRow>                       
                        <TableCell align="center">Time</TableCell>
                        <TableCell align="center">Date</TableCell>
                        <TableCell align="center">Time</TableCell>
                        <TableCell align="center">Date</TableCell>
                      </TableRow>
                    </TableHead>

enter image description here

How can I achieve this same design in material-table?

like image 791
Loran Avatar asked Mar 03 '23 07:03

Loran


1 Answers

You can use the components property of material-table to create custom header.

function App() {
  const columns = [...];

  const data = [...];

  return (
    <div className="App">
      <MaterialTable
        columns={columns}
        data={data}
        components={{
          Header: props => {
            return (
              <TableHead>
                <TableRow>
                  <TableCell rowSpan={2}>Approve</TableCell>
                  <TableCell colSpan={2} align="center">
                    In
                  </TableCell>
                  <TableCell colSpan={2} align="center">
                    Out
                  </TableCell>
                  <TableCell rowSpan={2} align="center">
                    Action
                  </TableCell>
                </TableRow>
                <TableRow>
                  <TableCell align="center">Time</TableCell>
                  <TableCell align="center">Date</TableCell>
                  <TableCell align="center">Time</TableCell>
                  <TableCell align="center">Date</TableCell>
                </TableRow>
              </TableHead>
            );
          },
          Row: ({ data }) => {
            return (
              <TableRow>
                <TableCell>{data.approve}</TableCell>
                <TableCell align="center">{data.inTime}</TableCell>
                <TableCell align="center">{data.inDate}</TableCell>
                <TableCell align="center">{data.outTime}</TableCell>
                <TableCell align="center">{data.outDate}</TableCell>
                <TableCell align="center">{data.action}</TableCell>
              </TableRow>
            );
          }
        }}
      />
    </div>
  );
}

DEMO: codesandbox link

like image 79
Akhilesh Avatar answered Mar 31 '23 13:03

Akhilesh