在表格组件中,可以通过设置 columns
属性来定义列的渲染。可以将选择列和扩展图标列分别定义,并将它们按照需求排列。
下面是一个示例,其中选择列在第二列,扩展图标在第一列:
import { Table } from 'antd';
const dataSource = [
// ...
];
const columns = [
{
title: '',
key: 'expandIcon',
render: (text, record) =>
record.children ? (
console.log('onExpand', record)}
/>
) : null,
},
{
title: '选择',
key: 'selection',
dataIndex: 'selection',
render: () => ,
},
{
title: '名称',
dataIndex: 'name',
key: 'name',
},
// ...
];
const TableComponent = () => {
return
;
};
在 columns
中先定义了扩展图标列,然后是选择列,再是其他列。
注意,为了让选择列实现勾选功能,需要添加 dataIndex
属性,并在每行数据中加入对应的数据项,示例如下:
const dataSource = [
{ id: 1, name: '张三', selection: true },
{ id: 2, name: '李四', selection: false },
// ...
];
这样,就可以在选择列中呈现复选框,并实现选择功能。