Ant Design Table隐藏列后设置宽度失效解决方案
要解决这个问题,可以在隐藏某一列后手动设置其他列的宽度,以保证设置的宽度仍然生效。\n\n首先,你需要在 <Table> 组件中设置一个变量 showColumn 来控制是否显示某一列,例如:\n\njsx\nconst [showColumn, setShowColumn] = useState(true);\n\n\n然后,在 <Table> 组件中根据 showColumn 来判断是否隐藏某一列,例如:\n\njsx\n<Table dataSource={dataSource} columns={[\n { title: '列1', dataIndex: 'column1', key: 'column1', width: 200 },\n { title: '列2', dataIndex: 'column2', key: 'column2', width: showColumn ? 200 : 0 },\n { title: '列3', dataIndex: 'column3', key: 'column3', width: 200 },\n]} />\n\n\n接下来,你可以通过按钮或其他交互方式来控制 showColumn 的值,从而隐藏或显示某一列,例如:\n\njsx\n<Button onClick={() => setShowColumn(!showColumn)}>隐藏/显示某一列</Button>\n\n\n最后,为了保证设置的宽度仍然生效,你可以在 <Table> 组件中通过 scroll 属性设置水平滚动条,例如:\n\njsx\n<Table dataSource={dataSource} columns={columns} scroll={{ x: 600 }} />\n\n\n这样,当你隐藏某一列时,其他列的宽度仍然会生效,并且会出现水平滚动条来显示隐藏的列。
原文地址: https://www.cveoy.top/t/topic/pWFk 著作权归作者所有。请勿转载和采集!