如何在React Material UI表格中添加borderTop?

10

enter image description here

我想要给上面的表格添加顶部边框。

我尝试过:

const styles = theme => {
    root : { borderTopWidth: 1, borderColor: 'red'}
}

...

class TableComponent ...

{ classes } = this.props; 

<Table className={classes.root}>
</Table

export default withStyles(styles)(TableComponent)

我认为这不是语法问题,因为其他选项如background: 'red'可以正常工作。也许我错过了什么。我该如何在这个表格中实现顶部边框?


你尝试给 borderTopWidth 添加一个单位了吗?比如使用 1px 而不是 1 - ChrisR
@ChrisR React 默认足够智能,会默认添加像素。 - Liam
2个回答

20
您忘记定义一个borderStyle属性。
const styles = theme => {
    root : { borderTopWidth: 1, borderColor: 'red',borderStyle: 'solid'} // or borderTop: '1px solid red'
}

...

class TableComponent ...

{ classes } = this.props; 

<Table className={classes.root}>
</Table

export default withStyles(styles)(TableComponent)

或者您可以像这样添加内联样式:

<Table style={{borderTop: '1px solid red'}}>
</Table>

0

@Shrroy已经回答了这个问题。但我一直在苦苦思索如何仅添加单元格中的右边框而不是其他任何边框。我将在此分享它,假设它可能会帮助其他人(@Shrroy的答案有所帮助)。同样,您可以拥有顶部边框或任何其他组合(例如:仅右侧和左侧边框)。

如何仅向单元格的一侧添加边框。

const styles = theme => {
    tableRightBorder : { borderWidth: 0, borderWidth: 1, borderColor: 'red',borderStyle: 'solid'} // or borderTop: '1px solid red'
}

...

class TableComponent ...

{ classes } = this.props; 

<Table >
    <TableHead>
        <TableRow >
            <TableCell>Column 1</TableCell>
            <TableCell>Column 2</TableCell>
        </TableRow>
    </TableHead>
    <TableBody>
        <TableRow>
            <TableCell className={classes.tableRightBorder}>Cell 1</TableCell>
            <TableCell>Cell 2</TableCell>
        </TableRow>
    </TableBody>
</Table>

export default withStyles(styles)(TableComponent)

它会看起来像这样。 在此输入图像描述


想获得与上述图像完全工作的组件,请尝试此表

import React from 'react';
import PropTypes from 'prop-types';
import { makeStyles } from '@material-ui/styles';

import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableRow,
} from '@material-ui/core';
import { connect } from 'react-redux';

const useStyles = makeStyles(theme => ({
    root: {},
    tableRightBorder: {
        borderWidth: 0,
        borderRightWidth: 1,
        borderColor: 'black',
        borderStyle: 'solid',
    },
}));

const DataTable = props => {
    const classes = useStyles();

    return (
        <Table>
            <TableHead>
                <TableRow>
                    <TableCell>
                        Column 1
                    </TableCell>
                    <TableCell>Column 2</TableCell>
                </TableRow>
            </TableHead>
            <TableBody>
                <TableRow>
                    <TableCell className={classes.tableRightBorder}>
                        Cell 1
                    </TableCell>
                    <TableCell>Cell 2</TableCell>
                </TableRow>
            </TableBody>
        </Table>
    );
};

DataTable.propTypes = {
    className: PropTypes.string,
};

function mapStateToProps() {}

export default connect(mapStateToProps, {})(DataTable);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接