gpt4 book ai didi

reactjs - React js Material-UI 响应式表格

转载 作者:行者123 更新时间:2023-12-03 13:22:09 24 4
gpt4 key购买 nike

我正在使用react js构建一个Web应用程序,并且正在使用material-ui组件库。我正在使用表格组件,它在桌面上看起来不错,但我希望它能够调整并在移动浏览器上看起来也不错。 Material-ui支持这样的东西吗?我该怎么做?当前情况示例:电脑\手机: PC

Mobile

源代码:

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';

const styles = theme => ({
root: {
width: '100%',
marginTop: theme.spacing.unit * 3,
overflowX: 'auto',
},
table: {
minWidth: 700,
},
});

let id = 0;
function createData(name, calories, fat, carbs, protein) {
id += 1;
return { id, name, calories, fat, carbs, protein };
}

const data = [
createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
createData('Eclair', 262, 16.0, 24, 6.0),
createData('Cupcake', 305, 3.7, 67, 4.3),
createData('Gingerbread', 356, 16.0, 49, 3.9),
];

function SimpleTable(props) {
const { classes } = props;

return (
<Paper className={classes.root}>
<Table className={classes.table}>
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell numeric>Calories</TableCell>
<TableCell numeric>Fat (g)</TableCell>
<TableCell numeric>Carbs (g)</TableCell>
<TableCell numeric>Protein (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{data.map(n => {
return (
<TableRow key={n.id}>
<TableCell component="th" scope="row">
{n.name}
</TableCell>
<TableCell numeric>{n.calories}</TableCell>
<TableCell numeric>{n.fat}</TableCell>
<TableCell numeric>{n.carbs}</TableCell>
<TableCell numeric>{n.protein}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</Paper>
);
}

SimpleTable.propTypes = {
classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(SimpleTable);

最佳答案

对于 Material-UI 表格,每个表格单元格的 padding-rightpadding-left 都应该更改,您可以在 Codesandbox 中获取代码

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';

const styles = theme => ({
root: {
display: 'flex',
marginTop: theme.spacing.unit * 3,
overflowX: 'hide',
},
table: {
minWidth: 340,
},
tableCell: {
paddingRight: 4,
paddingLeft: 5
}
});

let id = 0;
function createData(name, calories, fat, carbs, protein) {
id += 1;
return { id, name, calories, fat, carbs, protein };
}

const data = [
createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
createData('Eclair', 262, 16.0, 24, 6.0),
createData('Cupcake', 305, 3.7, 67, 4.3),
createData('Gingerbread', 356, 16.0, 49, 3.9),
];

function SimpleTable(props) {
const { classes } = props;

return (
<Paper className={classes.root}>
<Table className={classes.table}>
<TableHead>
<TableRow>
<TableCell className={classes.tableCell}>Dessert (100g serving)</TableCell>
<TableCell numeric className={classes.tableCell}>Calories</TableCell>
<TableCell numeric className={classes.tableCell}>Fat (g)</TableCell>
<TableCell numeric className={classes.tableCell}>Carbs (g)</TableCell>
<TableCell numeric className={classes.tableCell}>Protein (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{data.map(n => {
return (
<TableRow key={n.id}>
<TableCell component="th" scope="row" className={classes.TableCell}>
{n.name}
</TableCell>
<TableCell numeric className={classes.tableCell}>{n.calories}</TableCell>
<TableCell numeric className={classes.tableCell}>{n.fat}</TableCell>
<TableCell numeric className={classes.tableCell}>{n.carbs}</TableCell>
<TableCell numeric className={classes.tableCell}>{n.protein}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</Paper>
);
}

SimpleTable.propTypes = {
classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(SimpleTable);

要使其响应,您需要使用 Grid system 首先,例如这是整页的网格系统:

 <Grid item xs={12}>
<Table/>
</Grid>

关于reactjs - React js Material-UI 响应式表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51264502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com