reactjs - Ant Table fixed column width - Stack Overflow

admin2025-04-16  2

  1. The column width is not set via the width property.
  2. If you set the width of the cells, the column will still stretch if the table container is enlarged.

Can you tell me, has anyone come across such a case?

my code:

  const columns = [
    {
      key: "a",
      dataIndex: "a",
      title: "A",
      width: "100px",
    },
    {
      key: "b",
      dataIndex: "b",
      title: "B",
      width: "50px",
    },
    {
      key: "c",
      dataIndex: "c",
      title: "C",
      width: "100px",
    },
    {
      key: "d",
      dataIndex: "d",
      title: "D",
      width: "100px",
    },

    {
      key: "e",
      dataIndex: "e",
      title: "E",
      width: "100px",
    },
  ];

  const dataSource = [
    { a: 1, b: 2, c: 3, d: 4, e: 5 },
    { a: 2, b: 3, c: 4, d: 5, e: 1 },
    { a: 3, b: 4, c: 5, d: 1, e: 2 },
    { a: 4, b: 5, c: 1, d: 2, e: 3 },
    { a: 5, b: 1, c: 2, d: 3, e: 4 },
  ];

  return (
    <div style={{ width: "100%" }}>
      <Table tableLayout="fixed" columns={columns} dataSource={dataSource} />
    </div>
  );
}```
转载请注明原文地址:http://www.anycun.com/QandA/1744735086a86875.html