简体   繁体   English

使用 Material UI 组件自定义 TableRow

[英]Customize TableRow with Material UI component

I'm using Material UI table example and trying to customize the rows to have my own component to replace the default row.I want to have some margin between the rows and a shadow (using elevation prop of Paper element).我正在使用 Material UI 表示例并尝试自定义行以拥有我自己的组件来替换默认行。我希望在行和阴影之间有一些边距(使用 Paper 元素的高程道具)。 This is what I achieved so far:这是我迄今为止取得的成就:

import React, { useState } from 'react'
import Paper from '@material-ui/core/Paper';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableContainer from '@material-ui/core/TableContainer';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';

const MyPaper = (item) => {
  return (
    <Paper
      elevation={6}>
      {item}
    </Paper>
  );
};

const List = () => {
  const items = ['a', 'b'];

  return (     
    <div style={{ maxWidth: "100%" }}>
      <TableContainer>
        <Table aria-label="simple table">
          <TableHead>
            <TableRow>
              <TableCell>Dessert (100g serving)</TableCell>
              <TableCell align="right">Calories</TableCell>
              <TableCell align="right">Fat&nbsp;(g)</TableCell>
              <TableCell align="right">Carbs&nbsp;(g)</TableCell>
              <TableCell align="right">Protein&nbsp;(g)</TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {items.map(item => (
              <TableRow key={item} component={() => MyPaper(item)}>
                <TableCell>{item}</TableCell>
                <TableCell>{item}</TableCell>
                <TableCell>{item}</TableCell>
                <TableCell>{item}</TableCell>
                <TableCell>{item}</TableCell>
              </TableRow>
            ))}
          </TableBody>
        </Table>
      </TableContainer>
    </div>
  )
}

export default List

but the result is being applied only in the first column.但结果仅适用于第一列。 How do I fix this to have my custom row to occupy the entire table space?如何解决此问题以使我的自定义行占据整个表空间?

Thanks in advance提前致谢

You can do it easily with withstyles property of Material-UI, you can arrange Paper or other MUI components to give them classes and arrange them in styles variable:您可以使用 Material-UI 的 withstyles 属性轻松完成,您可以安排 Paper 或其他 MUI 组件为它们提供类并将它们安排在样式变量中:

import React, { useState } from 'react'
import Paper from '@material-ui/core/Paper';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableContainer from '@material-ui/core/TableContainer';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import { WithStyles } from '@material-ui/core'


const styles = theme => ({
...theme,
Row:  {
margin: 10 
      }
})

const List = () => {
  const items = ['a', 'b'];
  const {classes} = this.props
  return (     
    <div style={{ maxWidth: "100%" }}>
      <TableContainer>
        <Table aria-label="simple table">
          <TableHead>
            <TableRow>
              <TableCell>Dessert (100g serving)</TableCell>
              <TableCell align="right">Calories</TableCell>
              <TableCell align="right">Fat&nbsp;(g)</TableCell>
              <TableCell align="right">Carbs&nbsp;(g)</TableCell>
              <TableCell align="right">Protein&nbsp;(g)</TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {items.map(item => (
              <TableRow key={item} className={classes.Row}>
                <TableCell>{item}</TableCell>
                <TableCell>{item}</TableCell>
                <TableCell>{item}</TableCell>
                <TableCell>{item}</TableCell>
                <TableCell>{item}</TableCell>
              </TableRow>
            ))}
          </TableBody>
        </Table>
      </TableContainer>
    </div>
  )
}

export default withStyles(styles)(List)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM