具有动态高度的 React Material-Ui 粘性表头

Dyn*_*mic 6 javascript css reactjs material-ui

我正在使用 React 的 Material-UI 框架来显示表格。我想使用粘性标题;但是,我不想在我的桌子上设置高度,因为我希望它随着页面滚动。除非我在 TableContainer 上设置高度,否则以下代码段不会粘贴标题。

https://codesandbox.io/s/winter-firefly-5wlx2?file=/src/App.js

import React from "react";
import {
  TableContainer,
  Table,
  TableHead,
  TableRow,
  TableCell
} from "@material-ui/core";
import "./styles.css";

export default function App() {
  return (
    <TableContainer>
      <Table stickyHeader>
        <TableHead>
          <TableRow>
            <TableCell>Value</TableCell>
          </TableRow>
        </TableHead>
        {
          Array(100).fill("Test").map((e) => <TableRow><TableCell>{e}</TableCell></TableRow>)
        }
      </Table>
    </TableContainer>
  );
}
Run Code Online (Sandbox Code Playgroud)

95f*_*973 7

Get rid of the TableContainer overflow-x: auto and it should work

const useStyles = makeStyles({
  customTableContainer: {
    overflowX: "initial"
  }
})

export default function App() {
  const classes = useStyles();
  return (
    <TableContainer classes={{root: classes.customTableContainer}}>
      <Table stickyHeader>
      
      ...
Run Code Online (Sandbox Code Playgroud)

编辑cool-shadow-59lrh

参考:https : //css-tricks.com/dealing-with-overflow-and-position-sticky/