gpt4 book ai didi

css - Material 用户界面 : Give TableBody a max height and make it vertically scrollable

转载 作者:行者123 更新时间:2023-11-28 09:55:42 24 4
gpt4 key购买 nike

使用 Material UI,我希望表格的 TableBody 的最大高度为 500 像素。如果有任何行不能容纳在 TableBody 的高度内,那么 TableBody 应该垂直滚动,而 TableHead 锁定到位(卡住)。

enter image description here

这是我的代码:

import React from "react";
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';

const data = ["1", "1","1","1","1","1","1","1","1","1","1","1","1","1"];

class Demo extends React.Component {
render() {
return (
<div>
<Table>
<TableHead>
<TableRow style={{ 'backgroundColor': '#f5f5f5', "height": '35px' }}>
<TableCell>Column 1</TableCell>
<TableCell>Column 2</TableCell>
<TableCell></TableCell>
</TableRow>
</TableHead>
<TableBody>
{data.map(n => {
return (
<TableRow key={n}>
<TableCell>{n}</TableCell>
<TableCell>{n}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</div>
);
}
}

export default Demo;

工作示例:https://codesandbox.io/s/y03vmkkkqj

如何在 Material UI 中完成此操作?

请从我的示例中 fork 并提供指向有效解决方案的链接。

最佳答案

您可以在 Table 组件中使用 stickyHeader 属性,并将 maxHeight 赋给 TableContainer

<TableContainer style={{ maxHeight: 150 }}>
<Table stickyHeader>
<TableHead>
<TableRow style={{ 'backgroundColor': '#f5f5f5', "height": '35px' }}>
<TableCell>Column 1</TableCell>
<TableCell>Column 2</TableCell>
<TableCell></TableCell>
</TableRow>
</TableHead>
<TableBody>
{data.map(n => {
return (
<TableRow key={n}>
<TableCell>{n}</TableCell>
<TableCell>{n}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>

这里是官方demo来自 material-ui 文档。

关于css - Material 用户界面 : Give TableBody a max height and make it vertically scrollable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51068118/

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