You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
3.8 KiB
162 lines
3.8 KiB
6 years ago
|
---
|
||
|
order: 5
|
||
|
title:
|
||
|
zh-CN: 表格穿梭框
|
||
|
en-US: Table Transfer
|
||
|
---
|
||
|
|
||
|
## zh-CN
|
||
|
|
||
|
使用 Table 组件作为自定义渲染列表。
|
||
|
|
||
|
## en-US
|
||
|
|
||
|
Customize render list with Table component.
|
||
|
|
||
6 years ago
|
```jsx
|
||
6 years ago
|
import { Transfer, Switch, Table, Tag } from 'antd';
|
||
|
import difference from 'lodash/difference';
|
||
|
|
||
|
// Customize Table Transfer
|
||
|
const TableTransfer = ({ leftColumns, rightColumns, ...restProps }) => (
|
||
6 years ago
|
<Transfer {...restProps} showSelectAll={false}>
|
||
6 years ago
|
{({
|
||
6 years ago
|
direction,
|
||
|
filteredItems,
|
||
|
onItemSelectAll,
|
||
|
onItemSelect,
|
||
|
selectedKeys: listSelectedKeys,
|
||
|
disabled: listDisabled,
|
||
6 years ago
|
}) => {
|
||
|
const columns = direction === 'left' ? leftColumns : rightColumns;
|
||
|
|
||
|
const rowSelection = {
|
||
6 years ago
|
getCheckboxProps: item => ({ disabled: listDisabled || item.disabled }),
|
||
6 years ago
|
onSelectAll(selected, selectedRows) {
|
||
6 years ago
|
const treeSelectedKeys = selectedRows
|
||
|
.filter(item => !item.disabled)
|
||
|
.map(({ key }) => key);
|
||
|
const diffKeys = selected
|
||
|
? difference(treeSelectedKeys, listSelectedKeys)
|
||
|
: difference(listSelectedKeys, treeSelectedKeys);
|
||
6 years ago
|
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>
|
||
|
);
|
||
|
|
||
6 years ago
|
const mockTags = ['cat', 'dog', 'bird'];
|
||
6 years ago
|
|
||
|
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],
|
||
|
});
|
||
|
}
|
||
|
|
||
6 years ago
|
const originTargetKeys = mockData.filter(item => +item.key % 3 > 1).map(item => item.key);
|
||
6 years ago
|
|
||
|
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,
|
||
6 years ago
|
};
|
||
6 years ago
|
|
||
6 years ago
|
onChange = nextTargetKeys => {
|
||
6 years ago
|
this.setState({ targetKeys: nextTargetKeys });
|
||
6 years ago
|
};
|
||
6 years ago
|
|
||
6 years ago
|
triggerDisable = disabled => {
|
||
6 years ago
|
this.setState({ disabled });
|
||
|
};
|
||
|
|
||
6 years ago
|
triggerShowSearch = showSearch => {
|
||
6 years ago
|
this.setState({ showSearch });
|
||
|
};
|
||
|
|
||
|
render() {
|
||
|
const { targetKeys, disabled, showSearch } = this.state;
|
||
|
return (
|
||
|
<div>
|
||
|
<TableTransfer
|
||
|
dataSource={mockData}
|
||
|
targetKeys={targetKeys}
|
||
|
disabled={disabled}
|
||
|
showSearch={showSearch}
|
||
|
onChange={this.onChange}
|
||
6 years ago
|
filterOption={(inputValue, item) =>
|
||
|
item.title.indexOf(inputValue) !== -1 || item.tag.indexOf(inputValue) !== -1
|
||
|
}
|
||
6 years ago
|
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 }}
|
||
|
/>
|
||
|
</div>
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ReactDOM.render(<App />, mountNode);
|
||
6 years ago
|
```
|