javascript - 如何使 CSS 与 MUI TableContainer 一起工作

标签 javascript css material-ui next.js

我创建了一个包含数据的 MUI 网格,使用 MUI 网站构建它,我希望它看起来像这样:

What I would like my chart to look like

相反,它看起来像这样,好像它无法读取“密集表”属性:

What it actually looks like

这是我的代码:

<Card className="flightPlanCard" style={{ maxHeight: 300, overflow: 'auto', backgroundColor: "rgba(255,255,255,.5)" }}>
                            <TableContainer component={Paper}>
                                <Table size="small" aria-label="a dense table" >
                                    <TableHead>
                                        <TableRow>
                                            <StyledTableCell>ID</StyledTableCell>
                                            <StyledTableCell align="center">Fréquence</StyledTableCell>
                                            <StyledTableCell align="center">Track</StyledTableCell>
                                            <StyledTableCell align="center">Distance&nbsp;(NM)</StyledTableCell>
                                            <StyledTableCell align="center">Coordonées</StyledTableCell>
                                            <StyledTableCell align="center">Nom/Remarque</StyledTableCell>
                                        </TableRow>
                                    </TableHead>
                                    <TableBody style={{ fontSize: 10 }}>
                                        {rows.map((row) => (
                                            <TableRow key={row.name} style={{ height: 10 }} >
                                                <StyledTableCell component="th" scope="row">
                                                    {row.id}
                                                </StyledTableCell>
                                                <StyledTableCell align="center">{row.frequency}</StyledTableCell>
                                                <StyledTableCell align="center">{row.track}</StyledTableCell>
                                                <StyledTableCell align="center">{row.distance}</StyledTableCell>
                                                <StyledTableCell align="center">{row.coordinates}</StyledTableCell>
                                                <StyledTableCell align="center">{row.name}</StyledTableCell>
                                            </TableRow>
                                        ))}
                                    </TableBody>
                                </Table>
                            </TableContainer>
                        </Card>

TableContainer 位于 Card 中,我在运行我的应用程序时也会收到警告:

Material-UI: The key `row` provided to the classes prop is not implemented in ForwardRef(TableCell).
You can only override one of the following: root,head,body,footer,sizeSmall,paddingCheckbox,paddingNone,alignLeft,alignCenter,alignRight,alignJustify,stickyHeader.

我该如何解决这个问题?

提前致谢

最佳答案

毕竟我找到了一个很好的解决方案来解决这个问题:你必须在文件夹/pages 中添加一个名为 _document.jsx 的文件,其中包含以下代码:

import React from 'react'
import Document, { Html, Head, Main, NextScript } from 'next/document'
import { ServerStyleSheets } from '@material-ui/styles'
import { createMuiTheme, responsiveFontSizes } from '@material-ui/core/styles'

const theme = responsiveFontSizes(createMuiTheme())

class MyDocument extends Document {
  render() {
    return (
      <Html>
        <Head>
          <meta charSet="utf-8" />
          <meta
            name="viewport"
            content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no"
          />
          <meta name="theme-color" content={theme.palette.primary.main} />
          <link
            rel="stylesheet"
            href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Roboto+Slab:400,700|Material+Icons"
          />
          <style jsx global>
            {`
              html,
              body {
                height: 100%;
                width: 100%;
              }
              *,
              *:after,
              *:before {
                box-sizing: border-box;
              }
              body {
                font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
                font-size: 1rem;
                margin: 0;
              }
            `}
          </style>
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

MyDocument.getInitialProps = async ctx => {
  // Render app and page and get the context of the page with collected side effects.
  const sheets = new ServerStyleSheets()
  const originalRenderPage = ctx.renderPage

  ctx.renderPage = () =>
    originalRenderPage({
      enhanceApp: App => props => sheets.collect(<App {...props} />)
    })

  const initialProps = await Document.getInitialProps(ctx)

  return {
    ...initialProps,
    // Styles fragment is rendered after the app and page rendering finish.
    styles: [
      <React.Fragment key="styles">
        {initialProps.styles}
        {sheets.getStyleElement()}
      </React.Fragment>
    ]
  }
}

就是这样!无需导入,一切正常 :)

关于javascript - 如何使 CSS 与 MUI TableContainer 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64590473/

相关文章:

javascript - 导入时自定义模块 "was not found"- VueJS 项目

javascript - 与嵌入式 Google Apps 电子表格交互

css - Bootstrap 布局在宽屏显示器上表现得很奇怪

html - 无法显示特殊字符 HTML

javascript - 如何告诉 npm 在安装时使用父项目的依赖项构建模块?

css - 如何在 material ui REACTjs 中覆盖 menuItem 中的选定类?

javascript - 嵌套 $http.get 函数

javascript - 在 Node.js 后端记录事件/操作以供以后分析

html - 覆盖页面的动态列表......但仅限于 Chrome for Android

javascript - 在选项卡之间切换时避免在 React 中重新渲染