AntD 表格的 onClick 方法默认是适用于每一行数据的,如果希望用户在点击某一行数据时跳转到其他路由,可以通过使用 react-router 的 history.push 方法实现。 示例代码:
import { Table } from 'antd';
import { useHistory } from 'react-router-dom';
const dataSource = [
{
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',
},
];
const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
render: (text, record) => handleClick(record)}>{text},
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
},
{
title: 'Address',
dataIndex: 'address',
key: 'address',
},
];
const TableWithLink = () => {
const history = useHistory();
const handleClick = (record) => {
history.push(`/details/${record.key}`);
};
return
;
};
在该示例中,我们在 columns 中将 Name 列以 Link 形式渲染,并通过 handleClick 方法在用户点击时跳转到其他路由,具体实现可参考 handleClick 方法。
下一篇:Antd表格分页后排序不正确