代码之家  ›  专栏  ›  技术社区  ›  ton1

如何添加borderTop以反应材质UI表?

  •  10
  • ton1  · 技术社区  · 7 年前

    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' working properly. 也许我错过了什么。如何对此表实现topBorder?

    2 回复  |  直到 7 年前
        1
  •  20
  •   Liam    7 年前

    您忘记定义 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>
    
        2
  •  0
  •   Keet Sugathadasa    5 年前

    @Shroy已经回答了这个问题。但我一直在努力只添加 Right Border

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

    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)
    

    看起来是这样的。 enter image description here


    要获取上图的完整工作组件,请尝试下表

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