Antd Table editable 状态 在 鼠标 hover 编辑行的时候,render 函数触发异常

项目技术 : react + antd

使用 antd 的可编辑表格 时遇到个问题,点击编辑之后,如果鼠标一直在当前编辑的这一行 hover,不移动到当前行之外,那么这一行的所有单元格 的 render 不会重新触发。

翻看 antd 文档发现可以使用 shouldCellUpdate 这个函数来使单元格强制刷新,

 

 强制 shouldCellUpdate 返回 true 之后,点击编辑单元格即可变为编辑状态。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过 `antd` 的 `Table` 组件的 `editable` 和 `onSave` 两个属性来实现编辑的功能。 首先,需要在 `Table` 组件中设置 `editable` 属性为 `true`,来开启编辑的功能: ```jsx <Table dataSource={dataSource} columns={columns} rowKey="id" editable={{ type: 'single', onSave: handleSave, }} /> ``` 其中,`type` 属性可以设置为 `single` 或 `multiple`,分别表示单编辑和多编辑。 然后,需要在 `Table` 组件中设置 `onSave` 属性,来处理保存编辑的逻辑: ```jsx const handleSave = async (rowKey, rowData) => { const newData = [...dataSource]; const index = newData.findIndex((item) => rowKey === item.id); const item = newData[index]; newData.splice(index, 1, { ...item, ...rowData }); setDataSource(newData); }; ``` 在上述代码中,我们首先复制原来的数据源 `dataSource`,然后找到要编辑在数据源中的位置 `index`,将编辑后的数据 `rowData` 合并到该数据 `item` 中,最后用更新后的数据源更新 `Table` 组件。 完整代码如下: ```jsx import { Table } from 'antd'; const columns = [ { title: 'ID', dataIndex: 'id', }, { title: 'Name', dataIndex: 'name', editable: true, }, { title: 'Age', dataIndex: 'age', editable: true, }, ]; const dataSource = [ { id: '1', name: 'John Brown', age: 32, }, { id: '2', name: 'Jim Green', age: 42, }, { id: '3', name: 'Joe Black', age: 23, }, ]; const App = () => { const [dataSource, setDataSource] = useState(dataSource); const handleSave = async (rowKey, rowData) => { const newData = [...dataSource]; const index = newData.findIndex((item) => rowKey === item.id); const item = newData[index]; newData.splice(index, 1, { ...item, ...rowData }); setDataSource(newData); }; return ( <Table dataSource={dataSource} columns={columns} rowKey="id" editable={{ type: 'single', onSave: handleSave, }} /> ); }; export default App; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值