user6821859
user6821859

Reputation:

Material Table - nested columns

Is it possible to make a table with nested columns using material-table library?

Final result that I want to achieve

Upvotes: 1

Views: 3042

Answers (2)

Akhilesh
Akhilesh

Reputation: 562

Yes its possible with material-table . You have to use Components property to achieve that.

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

  const data = [...];

  return (
    <div className="App">
      <MaterialTable
        columns={columns}
        data={data}
        components={{
          Header: props => {
            return (
              <TableHead>
                <TableRow>
                  <TableCell colSpan={2} align="center">
                    Average A
                  </TableCell>
                  <TableCell colSpan={2} align="center">
                    Average B
                  </TableCell>
                </TableRow>
                <TableRow>
                  <TableCell align="center">Lower</TableCell>
                  <TableCell align="center">Upper</TableCell>
                  <TableCell align="center">Lower</TableCell>
                  <TableCell align="center">Upper</TableCell>
                </TableRow>
              </TableHead>
            );
          },
          Row: ({ data }) => {
            return (
              <TableRow>
                <TableCell align="center">{data.lowerA}</TableCell>
                <TableCell align="center">{data.upperA}</TableCell>
                <TableCell align="center">{data.lowerB}</TableCell>
                <TableCell align="center">{data.upperB}</TableCell>
              </TableRow>
            );
          }
        }}
      />
    </div>
  );
}

DEMO: Codesandbox link

Upvotes: 2

Sukrut Bam
Sukrut Bam

Reputation: 87

You can use colspan. Check example here. https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_td_colspan

Upvotes: 0

Related Questions