2019-05-07 17:10:42 +08:00
|
|
|
---
|
|
|
|
order: 5
|
|
|
|
title:
|
|
|
|
zh-CN: 表格穿梭框
|
|
|
|
en-US: Table Transfer
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
使用 Table 组件作为自定义渲染列表。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Customize render list with Table component.
|
|
|
|
|
2019-05-07 17:22:47 +08:00
|
|
|
```jsx
|
2019-05-07 17:10:42 +08:00
|
|
|
import { Transfer, Switch, Table, Tag } from 'antd';
|
|
|
|
import difference from 'lodash/difference';
|
|
|
|
|
|
|
|
// Customize Table Transfer
|
|
|
|
const TableTransfer = ({ leftColumns, rightColumns, ...restProps }) => (
|
2019-05-07 17:22:47 +08:00
|
|
|
<Transfer {...restProps} showSelectAll={false}>
|
2019-05-07 17:10:42 +08:00
|
|
|
{({
|
2019-05-07 17:22:47 +08:00
|
|
|
direction,
|
|
|
|
filteredItems,
|
|
|
|
onItemSelectAll,
|
|
|
|
onItemSelect,
|
|
|
|
selectedKeys: listSelectedKeys,
|
|
|
|
disabled: listDisabled,
|
2019-05-07 17:10:42 +08:00
|
|
|
}) => {
|
|
|
|
const columns = direction === 'left' ? leftColumns : rightColumns;
|
|
|
|
|
|
|
|
const rowSelection = {
|
2019-05-07 17:22:47 +08:00
|
|
|
getCheckboxProps: item => ({ disabled: listDisabled || item.disabled }),
|
2019-05-07 17:10:42 +08:00
|
|
|
onSelectAll(selected, selectedRows) {
|
2019-05-07 17:22:47 +08:00
|
|
|
const treeSelectedKeys = selectedRows
|
|
|
|
.filter(item => !item.disabled)
|
|
|
|
.map(({ key }) => key);
|
|
|
|
const diffKeys = selected
|
|
|
|
? difference(treeSelectedKeys, listSelectedKeys)
|
|
|
|
: difference(listSelectedKeys, treeSelectedKeys);
|
2019-05-07 17:10:42 +08:00
|
|
|
onItemSelectAll(diffKeys, selected);
|
|
|
|
},
|
|
|
|
onSelect({ key }, selected) {
|
|
|
|
onItemSelect(key, selected);
|
|
|
|
},
|
|
|
|
selectedRowKeys: listSelectedKeys,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Table
|
|
|
|
rowSelection={rowSelection}
|
|
|
|
columns={columns}
|
|
|
|
dataSource={filteredItems}
|
|
|
|
size="small"
|
|
|
|
style={{ pointerEvents: listDisabled ? 'none' : null }}
|
|
|
|
onRow={({ key, disabled: itemDisabled }) => ({
|
|
|
|
onClick: () => {
|
|
|
|
if (itemDisabled || listDisabled) return;
|
|
|
|
onItemSelect(key, !listSelectedKeys.includes(key));
|
|
|
|
},
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</Transfer>
|
|
|
|
);
|
|
|
|
|
2019-05-07 17:22:47 +08:00
|
|
|
const mockTags = ['cat', 'dog', 'bird'];
|
2019-05-07 17:10:42 +08:00
|
|
|
|
|
|
|
const mockData = [];
|
|
|
|
for (let i = 0; i < 20; i++) {
|
|
|
|
mockData.push({
|
|
|
|
key: i.toString(),
|
|
|
|
title: `content${i + 1}`,
|
|
|
|
description: `description of content${i + 1}`,
|
|
|
|
disabled: i % 4 === 0,
|
|
|
|
tag: mockTags[i % 3],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-07 17:22:47 +08:00
|
|
|
const originTargetKeys = mockData.filter(item => +item.key % 3 > 1).map(item => item.key);
|
2019-05-07 17:10:42 +08:00
|
|
|
|
|
|
|
const leftTableColumns = [
|
|
|
|
{
|
|
|
|
dataIndex: 'title',
|
|
|
|
title: 'Name',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dataIndex: 'tag',
|
|
|
|
title: 'Tag',
|
|
|
|
render: tag => <Tag>{tag}</Tag>,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dataIndex: 'description',
|
|
|
|
title: 'Description',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const rightTableColumns = [
|
|
|
|
{
|
|
|
|
dataIndex: 'title',
|
|
|
|
title: 'Name',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
class App extends React.Component {
|
|
|
|
state = {
|
|
|
|
targetKeys: originTargetKeys,
|
|
|
|
disabled: false,
|
|
|
|
showSearch: false,
|
2019-05-07 17:22:47 +08:00
|
|
|
};
|
2019-05-07 17:10:42 +08:00
|
|
|
|
2019-05-07 17:22:47 +08:00
|
|
|
onChange = nextTargetKeys => {
|
2019-05-07 17:10:42 +08:00
|
|
|
this.setState({ targetKeys: nextTargetKeys });
|
2019-05-07 17:22:47 +08:00
|
|
|
};
|
2019-05-07 17:10:42 +08:00
|
|
|
|
2019-05-07 17:22:47 +08:00
|
|
|
triggerDisable = disabled => {
|
2019-05-07 17:10:42 +08:00
|
|
|
this.setState({ disabled });
|
|
|
|
};
|
|
|
|
|
2019-05-07 17:22:47 +08:00
|
|
|
triggerShowSearch = showSearch => {
|
2019-05-07 17:10:42 +08:00
|
|
|
this.setState({ showSearch });
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { targetKeys, disabled, showSearch } = this.state;
|
|
|
|
return (
|
2020-07-08 08:58:45 +08:00
|
|
|
<>
|
2019-05-07 17:10:42 +08:00
|
|
|
<TableTransfer
|
|
|
|
dataSource={mockData}
|
|
|
|
targetKeys={targetKeys}
|
|
|
|
disabled={disabled}
|
|
|
|
showSearch={showSearch}
|
|
|
|
onChange={this.onChange}
|
2019-05-07 17:22:47 +08:00
|
|
|
filterOption={(inputValue, item) =>
|
|
|
|
item.title.indexOf(inputValue) !== -1 || item.tag.indexOf(inputValue) !== -1
|
|
|
|
}
|
2019-05-07 17:10:42 +08:00
|
|
|
leftColumns={leftTableColumns}
|
|
|
|
rightColumns={rightTableColumns}
|
|
|
|
/>
|
|
|
|
<Switch
|
|
|
|
unCheckedChildren="disabled"
|
|
|
|
checkedChildren="disabled"
|
|
|
|
checked={disabled}
|
|
|
|
onChange={this.triggerDisable}
|
|
|
|
style={{ marginTop: 16 }}
|
|
|
|
/>
|
|
|
|
<Switch
|
|
|
|
unCheckedChildren="showSearch"
|
|
|
|
checkedChildren="showSearch"
|
|
|
|
checked={showSearch}
|
|
|
|
onChange={this.triggerShowSearch}
|
|
|
|
style={{ marginTop: 16 }}
|
|
|
|
/>
|
2020-07-08 08:58:45 +08:00
|
|
|
</>
|
2019-05-07 17:10:42 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(<App />, mountNode);
|
2019-05-07 17:22:47 +08:00
|
|
|
```
|
2020-01-02 15:47:30 +08:00
|
|
|
|
|
|
|
```css
|
|
|
|
#components-transfer-demo-table-transfer .ant-table td {
|
|
|
|
background: transparent;
|
|
|
|
}
|
|
|
|
```
|