mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-16 01:41:02 +08:00
138 lines
2.8 KiB
Markdown
138 lines
2.8 KiB
Markdown
---
|
|
order: 27
|
|
title:
|
|
en-US: Drag sorting with handler
|
|
zh-CN: 拖拽手柄列
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
也可以使用 [react-sortable-hoc](https://github.com/clauderic/react-sortable-hoc) 来实现一个拖拽操作列。
|
|
|
|
## en-US
|
|
|
|
Alternatively you can implement drag sorting with handler using [react-sortable-hoc](https://github.com/clauderic/react-sortable-hoc).
|
|
|
|
```jsx
|
|
import { Table } from 'antd';
|
|
import { sortableContainer, sortableElement, sortableHandle } from 'react-sortable-hoc';
|
|
import { MenuOutlined } from '@ant-design/icons';
|
|
import arrayMove from 'array-move';
|
|
|
|
const DragHandle = sortableHandle(() => (
|
|
<MenuOutlined style={{ cursor: 'pointer', color: '#999' }} />
|
|
));
|
|
|
|
const columns = [
|
|
{
|
|
title: 'Sort',
|
|
dataIndex: 'sort',
|
|
width: 30,
|
|
className: 'drag-visible',
|
|
render: () => <DragHandle />,
|
|
},
|
|
{
|
|
title: 'Name',
|
|
dataIndex: 'name',
|
|
className: 'drag-visible',
|
|
},
|
|
{
|
|
title: 'Age',
|
|
dataIndex: 'age',
|
|
},
|
|
{
|
|
title: 'Address',
|
|
dataIndex: 'address',
|
|
},
|
|
];
|
|
|
|
const data = [
|
|
{
|
|
key: '1',
|
|
name: 'John Brown',
|
|
age: 32,
|
|
address: 'New York No. 1 Lake Park',
|
|
index: 0,
|
|
},
|
|
{
|
|
key: '2',
|
|
name: 'Jim Green',
|
|
age: 42,
|
|
address: 'London No. 1 Lake Park',
|
|
index: 1,
|
|
},
|
|
{
|
|
key: '3',
|
|
name: 'Joe Black',
|
|
age: 32,
|
|
address: 'Sidney No. 1 Lake Park',
|
|
index: 2,
|
|
},
|
|
];
|
|
|
|
const SortableItem = sortableElement(props => <tr {...props} />);
|
|
const SortableContainer = sortableContainer(props => <tbody {...props} />);
|
|
const DragableBodyRow = ({ index, className, style, ...restProps }) => (
|
|
<SortableItem index={restProps['data-row-key']} {...restProps} />
|
|
);
|
|
|
|
class SortableTable extends React.Component {
|
|
state = {
|
|
dataSource: data,
|
|
};
|
|
|
|
onSortEnd = ({ oldIndex, newIndex }) => {
|
|
const { dataSource } = this.state;
|
|
if (oldIndex !== newIndex) {
|
|
const newData = arrayMove([].concat(dataSource), oldIndex, newIndex).filter(el => !!el);
|
|
console.log('Sorted items: ', newData);
|
|
this.setState({ dataSource: newData });
|
|
}
|
|
};
|
|
|
|
render() {
|
|
const { dataSource } = this.state;
|
|
const DraggableContainer = props => (
|
|
<SortableContainer
|
|
useDragHandle
|
|
helperClass="row-dragging"
|
|
onSortEnd={this.onSortEnd}
|
|
{...props}
|
|
/>
|
|
);
|
|
return (
|
|
<Table
|
|
pagination={false}
|
|
dataSource={dataSource}
|
|
columns={columns}
|
|
rowKey="index"
|
|
components={{
|
|
body: {
|
|
wrapper: DraggableContainer,
|
|
row: DragableBodyRow,
|
|
},
|
|
}}
|
|
/>
|
|
);
|
|
}
|
|
}
|
|
|
|
ReactDOM.render(<SortableTable />, mountNode);
|
|
```
|
|
|
|
```css
|
|
.row-dragging {
|
|
background: #fafafa;
|
|
border: 1px solid #ccc;
|
|
}
|
|
|
|
.row-dragging td {
|
|
padding: 16px;
|
|
visibility: hidden;
|
|
}
|
|
|
|
.row-dragging .drag-visible {
|
|
visibility: visible;
|
|
}
|
|
```
|