【问题标题】:react-table tbody is not vertically scrollablereact-table tbody 不可垂直滚动
【发布时间】:2021-06-12 03:35:57
【问题描述】:

我正在使用 react-table 和样式化的组件,并试图使 tbody 可滚动并带有粘性头。我尝试为父容器设置高度,然后将 tbody overflow-y 设置为 auto,但是这种方法不起作用。我做错了什么?

CodeSandbox

const TableContainerOuter = styled.div`
  width: 100%;
  height: 100%;
  overflow-y: hidden;
  padding-bottom: 21px;
`;

const TableContainer = styled.div`
  width: 100%;
  height: 300px;
  min-width: 900px;
`;

const StyledTable = styled.table`
  width: 100%;
  position: relative;
  height: 30px;
  border-spacing: 0px;

  th {
    position: sticky;
    top: 0;
    left: 0;
    background: #f3f4f5;
    font-size: 9px;
    font-weight: 500;
    line-height: 230%;
    letter-spacing: 0.05em;
    color: ${(props) => props.theme.pbDarker};
    text-transform: uppercase;
    text-align: left;
    padding: 6px 0px;
    user-select: none;
    &:first-child {
      border-top-left-radius: 6px;
      border-bottom-left-radius: 6px;
      padding-left: 22px;
    }
    &:last-child {
      border-top-right-radius: 6px;
      border-bottom-right-radius: 6px;
    }
  }

  tbody {
    overflow-y: scroll;
    height: 100px;
  }

  td {
    padding: 16px 0px;
    border-style: solid;
    border-width: 0px;
    font-size: 12px;
    font-weight: 400;
    border-bottom-width: 1px;
    border-color: ${(props) => props.theme.pbFaded1};
    min-width: 95px;
    :first-of-type {
      padding-left: 22px;
    }
  }
`;

【问题讨论】:

    标签: css reactjs html-table react-table


    【解决方案1】:

    您没有应用溢出。你可以看看你是否检查你的桌子。将溢出添加到您的 TableContainer 中,它将起作用。往下看

    const TableContainer = styled.div`
      width: 100%;
      height: 100px;
      min-width: 900px;
      overflow-y: scroll;
    `;
    

    【讨论】:

    • 谢谢。但我想要实现的是滚动条位于标题下方,因此只有正文是可滚动的,否则看起来整个表格都是可滚动的。
    • @AlinaKhachatrian 你真的不能那样做。您可以将 tbody 设置为 display: block。但是,您将失去 th 和 td 固定宽度,因为浏览器将不再将其视为表格,但您将在其上滚动。
    • 是的,我也试过了,但我必须按照设计,所以它必须在标题下方...
    • @AlinaKhachatrian 您可以在表格上设置固定宽度并根据它在 tr 中分隔元素。您也可以编写一些js来根据屏幕大小设置宽度,使其更具动态
    【解决方案2】:

    创建 2 个表。一个有列标题一个没有任何。设置第二个表格滚动

    【讨论】:

    猜你喜欢
    • 2011-07-17
    • 2022-06-21
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 2019-07-27
    • 2022-10-12
    • 2022-06-13
    相关资源
    最近更新 更多