react antd Table 的行点击事件onRowClick

官方给出的例子为:

onRow={(record) => ({
  onClick: () => {
     this.selectRow(record);
  },
})}

发现不能触发点击事件,改为如下代码解决问题

onRowClick={
	record => {
		console.log(JSON.stringify(record));
	}
}

此时打印结果为点击的Table的行中的内容,是一个对象
onRow可能由于版本问题失效,使用onRowClick即可

如果您需要在 Ant Design 的 Table 组件中嵌套 Table 组件,可以使用 `expandedRowRender` 属性来实现。下面是一个示例代码: ```jsx import React from 'react'; import { Table } from 'antd'; const columns = [ { title: 'Name', dataIndex: 'name' }, { title: 'Age', dataIndex: 'age' }, { title: 'Address', dataIndex: 'address' }, ]; const data = [ { key: 1, name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', children: [ { key: 11, name: 'Jim Green', age: 42, address: 'London No. 2 Lake Park', }, { key: 12, name: 'Joe Black', age: 32, address: 'Sidney No. 3 Lake Park', }, ], }, { key: 2, name: 'Jim Green', age: 42, address: 'London No. 2 Lake Park', children: [ { key: 21, name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', }, { key: 22, name: 'Joe Black', age: 32, address: 'Sidney No. 3 Lake Park', }, ], }, { key: 3, name: 'Joe Black', age: 32, address: 'Sidney No. 3 Lake Park', children: [ { key: 31, name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', }, { key: 32, name: 'Jim Green', age: 42, address: 'London No. 2 Lake Park', }, ], }, ]; const App = () => { const expandedRowRender = (record) => { const columns = [ { title: 'Name', dataIndex: 'name' }, { title: 'Age', dataIndex: 'age' }, { title: 'Address', dataIndex: 'address' }, ]; const data = record.children; return <Table columns={columns} dataSource={data} pagination={false} />; }; return <Table columns={columns} dataSource={data} expandable={{ expandedRowRender }} />; }; export default App; ``` 在这个示例代码中,我们定义了一个 `expandedRowRender` 函数,在这个函数中,我们使用 Table 组件来渲染子表格。然后,在主表格中,我们将 `expandedRowRender` 函数传递给 `expandable` 属性来实现嵌套表格的效果。具体地,当用户点击主表格中的一个时,它的子表格就会被展开。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值