antd table 宽度_Table组件给列设置宽度不起作用,会按照内容自动伸缩

Translation of this issue:

Table component to set the width of the column does not work, will automatically scale according to the content

Version

3.6.4

Environment

Mac, Chrome67

Reproduction link

Steps to reproduce

Set the width to 100px for each column of the Columns (or any of them)

What is expected?

The column width of the column that is expected to be set by the pixel is the set value, not the width assigned by the Table component itself

What is actually happening?

Table will elastically set the width of the column according to the width

Is this a bug? How can we reach the width of the column where the column width is set for some columns is the set width

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在React项目中使用Ant Design的Table组件动态生成表头,需要完成以下步骤: 1. 定义表格数据源 首先需要定义表格的数据源,可以使用一个数组来存储表格的数据,例如: ```javascript const data = [ { key: '1', name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', }, { key: '2', name: 'Jim Green', age: 42, address: 'London No. 1 Lake Park', }, { key: '3', name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park', }, ]; ``` 2. 定义表头数据源 接下来需要定义表头的数据源,可以使用一个数组来存储表头的数据,例如: ```javascript const columns = [ { title: 'Name', dataIndex: 'name', key: 'name', }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, ]; ``` 3. 动态生成表头 在Ant Design的Table组件中,可以使用columns属性来指定表头数据源。为了动态生成表头,需要在渲染组件时动态生成表头的数据源,并将其传递给Table组件的columns属性,例如: ```javascript import React, { useState } from 'react'; import { Table } from 'antd'; const DynamicTable = () => { const [columns, setColumns] = useState([ { title: 'Name', dataIndex: 'name', key: 'name', }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, ]); const handleAddColumn = () => { const newColumn = { title: `Column ${columns.length + 1}`, dataIndex: `column${columns.length + 1}`, key: `column${columns.length + 1}`, }; setColumns([...columns, newColumn]); }; return ( <> <button onClick={handleAddColumn}>Add Column</button> <Table dataSource={data} columns={columns} /> </> ); }; export default DynamicTable; ``` 在上面的代码中,我们使用useState钩子来定义表头的数据源,并渲染一个添加的按钮。当点击添加按钮时,动态生成一个新的,并将其添加到表头的数据源中。这样就可以动态生成表头了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值