reactjs - 如何在react-virtualized(CodeSandBox)中向表格添加水平滚动条

标签 reactjs html-table scrollbar react-virtualized

在此表中,我想添加一些具有宽度的列。如果列的宽度大于页面的宽度,则不会显示水平滚动条。

如何从沙盒中重新制作示例并添加水平滚动条?可以通过 Table 组件来做到这一点吗?

https://codesandbox.io/s/k9km4qjkk5

react :

import React from "react";
import ReactDOM from "react-dom";
import { AutoSizer, Column, Table } from "react-virtualized";

import "./styles.css";

export default class List extends React.Component {
  state = {
    list: [
      {
        name: "Brian Vaughn",
        description: "Software engineer",
        aa: "aaaaaaaaa",
        bb: "bbbbbbbbbbb",
        cc: "cccccccccccccc"
      },
      {
        name: "Brian Vaughn",
        description: "Software engineer",
        aa: "aaaaaaaaa",
        bb: "bbbbbbbbbbb",
        cc: "cccccccccccccc"
      },
      ...
    ]
  };
  render() {
    return (
      <AutoSizer>
        {({ height, width }) => (
          <Table
            width={width}
            height={height}
            headerHeight={20}
            rowHeight={35}
            overscanRowCount={100}
            rowCount={this.state.list.length}
            rowGetter={function({ index }) {
              return this.state.list[index];
            }.bind(this)}
          >
            <Column label="Name" dataKey="name" width={500} flexShrink={0} />
            <Column
              width={500}
              flexShrink={0}
              label="Description"
              dataKey="description"
            />
            <Column width={500} flexShrink={0} label="aa" dataKey="aa" />
            <Column width={500} flexShrink={0} label="bb" dataKey="bb" />
            <Column width={500} flexShrink={0} label="cc" dataKey="cc" />
          </Table>
        )}
      </AutoSizer>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<List />, rootElement);

最佳答案

感谢 A2A。

我也遇到过类似的情况,表格有 10 列,而视口(viewport)宽度不足以容纳。

经过一些失败的解决方法后,我想出了这个简单的修复方法。

对于Table组件,有条件地添加width

// Sum of min-widths of all columns - 500*5 - in your case

const MIN_TABLE_WIDTH = 2500;

<AutoSizer>
  {({width, height}) => (
    <Table
     width={width < MIN_TABLE_WIDTH ? MIN_TABLE_WIDTH : width}
     height={height}
     ...restProps
    >
      .....
    </Table>
  )}
</AutoSizer>

希望对您有所帮助。

关于reactjs - 如何在react-virtualized(CodeSandBox)中向表格添加水平滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55563249/

相关文章:

html - Materialize css --select-- 在 react 中不起作用

css - 奇怪的滚动条

html - 在 Safari iOS 13 上始终显示滚动条

jquery-ui - 如何删除 jqueymobile Web 应用程序中的滚动条?

reactjs - npm WARN @typescript-eslint/eslint-plugin@1.6.0 需要 typescript@* 的对等点

reactjs - Redux 在 React 中添加了另一个数组中的对象数组

html - 如何滚动具有可变数量的行和列的表

php - 带按钮的宽度表列 Bootstrap 3.3

javascript - POST 500(内部服务器错误)无法在 Node 服务器上使用 React JS 发布表单数据

html - 将脚放在页面底部