物料表标题React

时间:2019-10-14 05:59:59

标签: reactjs material-ui material-table

我正在尝试使用物料表,我想在物料表中使用colspanrowspan。我已经搜索了示例和示例,但没有任何显示。

目前,我喜欢在物料表中

<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

如何在material-table中实现相同的设计?

1 个答案:

答案 0 :(得分:1)

您可以使用material-table的components属性创建自定义标题。

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>
  );
}

演示:codesandbox link