javascript - 如何使用 Material UI 在滚动时使 AppBar 和表格的标题粘在同一页面上(因此彼此堆叠)

标签 javascript node.js reactjs material-ui

这是 AppBar 和 Table 的屏幕截图:

This is the screenshot at the top of the page.

This is the screenshot once you start scrolling down

这是 AppBar 和 Table 的代码:

 <AppBar position="static" style = {{background : "#00009A"}}>
          <Toolbar>
          <Link to="/view" className={classes.navLink} align = 'left'>
            <Box textAlign = "left" >
          <Typography variant="h6" flex={1} align = 'left'>
            APP TITLE
          </Typography>
          </Box>
          </Link>

          <Link to="/view" className={classes.navLink}>
            <Button color="inherit" align='left' }>Map Skills</Button>
          </Link>
</AppBar>

<Table stickyHeader aria-label="sticky table" style={{ marginTop: 20, marginBottom: 30 }}>
      <TableHead>
        <TableRow>
          {columns.map(column => (
            <TableCell
              key={column.id}
              align={column.align}
              style={{ width: column.minWidth, fontWeight: 500, fontSize: 17, backgroundColor: "#C1D8F7"}}
            >
              {column.label === "" ? props.heading : column.label}
            </TableCell>
          ))}
        </TableRow>
      </TableHead>
      <TableBody>


我尝试使 AppBar 的“position = 'fixed'”,但向下滚动时它只是与表格的标题重叠。

我还尝试过使用 zIndex 将内容移到前面或后面。

我需要在滚动时将它们堆叠在一起,而不是重叠。 但我认为表头的粘性标题总是粘在页面顶部。

任何解决方案都会有很大帮助!谢谢!

最佳答案

将 AppBar 设置为 position="fixed"、表头 paddingTop: "68px"、表 stickyHeader,并删除任何 overflow: 'auto'

<AppBar position="fixed" color="primary">
  <Toolbar>
  <IconButton color="secondary">
    <FilterListRounded />
  </IconButton>
  <Typography variant="h6">App Title</Typography>
  </Toolbar>
</AppBar>

...

const StyledTableCell = withStyles(() => ({
  head: {
    paddingTop: "68px",
  },
}))(TableCell);

...

<Table size="small" stickyHeader>
  <TableHead className={classes.head}>
    <TableRow>
      <StyledTableCell>Header 1</StyledTableCell>
      <StyledTableCell>Header 2</StyledTableCell>
      <StyledTableCell>Header 3</StyledTableCell>
    </TableRow>
  </TableHead>
  <TableBody>
    {props.results.map(row => {
      return(
        <TableRow key={row.id} hover>
          <TableCell>{row.one}</TableCell>
          <TableCell>{row.two}</TableCell>
          <TableCell>{row.three}</TableCell>
        </TableRow>
    )})}
  </TableBody>
</Table>

关于javascript - 如何使用 Material UI 在滚动时使 AppBar 和表格的标题粘在同一页面上(因此彼此堆叠),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59181444/

相关文章:

javascript - 在 Vuejs 和 Laravel 中上传文件以及动态输入字段

javascript - 在 Javascript 中循环正则表达式匹配

javascript - Rollup + React 17 与新的 JSX 转换 - "React is not defined"

javascript - 有什么方法可以删除 HTML 元素中的 Angular 属性吗?

javascript - ReactJS 路由器无法读取未定义的渲染

javascript - 当对象属性不存在时引发错误

javascript - 用 promise 解决游标问题

node.js - Web 套接字和 Flash 套接字客户端连接到一个 Node.js

javascript - Gatsby JS : Dynamically assign Style/Class based on current time?

reactjs - react : ReferenceError: regeneratorRuntime is not defined