ant-design/components/table/demo/row-selection-custom.md

105 lines
2.4 KiB
Markdown
Raw Normal View History

---
order: 4
title:
en-US: Custom selection
zh-CN: 自定义选择项
---
## zh-CN
通过 `rowSelection.selections` 自定义选择项,默认不显示下拉选项,设为 `true` 时显示默认选择项。
## en-US
Use `rowSelection.selections` custom selections, default no select dropdown, show default selections via setting to `true`.
```tsx
import { Table } from 'antd';
2022-07-04 22:09:54 +08:00
import type { ColumnsType } from 'antd/es/table';
import type { TableRowSelection } from 'antd/es/table/interface';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
interface DataType {
key: React.Key;
name: string;
age: number;
address: string;
}
const columns: ColumnsType<DataType> = [
2019-05-07 14:57:32 +08:00
{
title: 'Name',
dataIndex: 'name',
},
{
title: 'Age',
dataIndex: 'age',
},
{
title: 'Address',
dataIndex: 'address',
},
];
const data: DataType[] = [];
for (let i = 0; i < 46; i++) {
data.push({
key: i,
name: `Edward King ${i}`,
age: 32,
address: `London, Park Lane no. ${i}`,
});
}
const App: React.FC = () => {
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
2018-06-27 15:55:04 +08:00
const onSelectChange = (newSelectedRowKeys: React.Key[]) => {
console.log('selectedRowKeys changed: ', newSelectedRowKeys);
setSelectedRowKeys(newSelectedRowKeys);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
const rowSelection: TableRowSelection<DataType> = {
selectedRowKeys,
onChange: onSelectChange,
selections: [
Table.SELECTION_ALL,
Table.SELECTION_INVERT,
Table.SELECTION_NONE,
{
key: 'odd',
text: 'Select Odd Row',
onSelect: changableRowKeys => {
let newSelectedRowKeys = [];
newSelectedRowKeys = changableRowKeys.filter((_, index) => {
if (index % 2 !== 0) {
2019-05-07 14:57:32 +08:00
return false;
}
return true;
});
setSelectedRowKeys(newSelectedRowKeys);
},
},
{
key: 'even',
text: 'Select Even Row',
onSelect: changableRowKeys => {
let newSelectedRowKeys = [];
newSelectedRowKeys = changableRowKeys.filter((_, index) => {
if (index % 2 !== 0) {
return true;
}
return false;
});
setSelectedRowKeys(newSelectedRowKeys);
},
},
],
};
return <Table rowSelection={rowSelection} columns={columns} dataSource={data} />;
};
export default App;
2019-05-07 14:57:32 +08:00
```