【问题标题】:Antd table columns are shrinking when you add more columns, how to fix column width?antd表列增加多列缩小,如何固定列宽?
【发布时间】:2022-01-20 20:55:12
【问题描述】:

我有这张基本表:

const columns = [
  {
    title: 'Column A',
    width: 240,
  },
  {
    title: 'Column b',
    width: 240,
  },
  {
    title: 'Column C',
    width: 240,
  },
  {
    title: 'Column D',
    width: 240,
  },
  {
    title: 'Column E',
    width: 240,
  },
  {
    title: 'Column F',
    width: 240,
  }
]

const rows = [
  ...
]


<Table
  loading={loading}
  columns={columns}
  dataSource={rows}
  scroll={{ x: 'scroll' }}
  pagination={false}
/>

但是,当我添加新列时,它会缩小以适应表格区域中的所有列。如何防止这种情况,并强制表格开始向右滚动,并使列实际上是我指定的宽度?

这是我动态添加新列。

【问题讨论】:

    标签: html-table antd


    【解决方案1】:

    为您的 Table 组件添加一个 className 并重写 table 标签的内联样式。默认表格布局为auto,您需要将其重写为fixed 值。他们的区别你可以看here

    所以您的代码将如下所示:

    function () {
      return (
        <Table
          className="table-wrapper"
          columns={columns}
          dataSource={rows}
          scroll={{ x: "scroll" }}
          pagination={false}
        />
      );
    }
    

    然后在样式中:

    .table-wrapper table {
      table-layout: fixed !important; /* rewrite inline styles */
    }
    

    codesandbox link

    【讨论】:

    • 该死,不幸的是,这似乎没有解决它:/
    • 现在下一个逻辑:如果有空闲空间,表格就会伸展,但是当空间不够时,列的宽度是240px。您的预期行为是什么?我会尽力帮助
    猜你喜欢
    • 2021-02-18
    • 2012-12-25
    • 2021-01-11
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多