2017-11-17 18:53:07 +08:00
|
|
|
---
|
2019-11-15 14:35:25 +08:00
|
|
|
order: 26
|
2017-11-17 18:53:07 +08:00
|
|
|
title:
|
|
|
|
en-US: Drag sorting
|
|
|
|
zh-CN: 拖拽排序
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
使用自定义元素,我们可以集成 react-dnd 来实现拖拽排序。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
By using custom components, we can integrate table with react-dnd to implement drag sorting.
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2017-11-17 18:53:07 +08:00
|
|
|
import { Table } from 'antd';
|
2020-03-03 22:19:19 +08:00
|
|
|
import { DndProvider, useDrag, useDrop } from 'react-dnd';
|
2017-11-17 18:53:07 +08:00
|
|
|
import HTML5Backend from 'react-dnd-html5-backend';
|
|
|
|
import update from 'immutability-helper';
|
|
|
|
|
2020-05-10 12:54:57 +08:00
|
|
|
const type = 'DragableBodyRow';
|
2017-11-17 18:53:07 +08:00
|
|
|
|
2020-03-03 22:19:19 +08:00
|
|
|
const DragableBodyRow = ({ index, moveRow, className, style, ...restProps }) => {
|
|
|
|
const ref = React.useRef();
|
|
|
|
const [{ isOver, dropClassName }, drop] = useDrop({
|
|
|
|
accept: type,
|
|
|
|
collect: monitor => {
|
|
|
|
const { index: dragIndex } = monitor.getItem() || {};
|
|
|
|
if (dragIndex === index) {
|
|
|
|
return {};
|
2018-05-30 17:56:20 +08:00
|
|
|
}
|
2020-03-03 22:19:19 +08:00
|
|
|
return {
|
|
|
|
isOver: monitor.isOver(),
|
|
|
|
dropClassName: dragIndex < index ? ' drop-over-downward' : ' drop-over-upward',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
drop: item => {
|
|
|
|
moveRow(item.index, index);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const [, drag] = useDrag({
|
|
|
|
item: { type, index },
|
|
|
|
collect: monitor => ({
|
|
|
|
isDragging: monitor.isDragging(),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
drop(drag(ref));
|
|
|
|
return (
|
|
|
|
<tr
|
|
|
|
ref={ref}
|
|
|
|
className={`${className}${isOver ? dropClassName : ''}`}
|
|
|
|
style={{ cursor: 'move', ...style }}
|
|
|
|
{...restProps}
|
|
|
|
/>
|
|
|
|
);
|
2017-11-17 18:53:07 +08:00
|
|
|
};
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
key: 'name',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Age',
|
|
|
|
dataIndex: 'age',
|
|
|
|
key: 'age',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Address',
|
|
|
|
dataIndex: 'address',
|
|
|
|
key: 'address',
|
|
|
|
},
|
|
|
|
];
|
2017-11-17 18:53:07 +08:00
|
|
|
|
|
|
|
class DragSortingTable extends React.Component {
|
|
|
|
state = {
|
2019-05-07 14:57:32 +08:00
|
|
|
data: [
|
|
|
|
{
|
|
|
|
key: '1',
|
|
|
|
name: 'John Brown',
|
|
|
|
age: 32,
|
|
|
|
address: 'New York No. 1 Lake Park',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '2',
|
|
|
|
name: 'Jim Green',
|
|
|
|
age: 42,
|
|
|
|
address: 'London No. 1 Lake Park',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
name: 'Joe Black',
|
|
|
|
age: 32,
|
|
|
|
address: 'Sidney No. 1 Lake Park',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2017-11-17 18:53:07 +08:00
|
|
|
|
|
|
|
components = {
|
|
|
|
body: {
|
2018-05-30 17:56:20 +08:00
|
|
|
row: DragableBodyRow,
|
2017-11-17 18:53:07 +08:00
|
|
|
},
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-11-17 18:53:07 +08:00
|
|
|
|
|
|
|
moveRow = (dragIndex, hoverIndex) => {
|
|
|
|
const { data } = this.state;
|
|
|
|
const dragRow = data[dragIndex];
|
|
|
|
|
2019-02-08 21:08:40 +08:00
|
|
|
this.setState(
|
|
|
|
update(this.state, {
|
|
|
|
data: {
|
2020-01-06 11:13:39 +08:00
|
|
|
$splice: [
|
|
|
|
[dragIndex, 1],
|
|
|
|
[hoverIndex, 0, dragRow],
|
|
|
|
],
|
2019-02-08 21:08:40 +08:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-11-17 18:53:07 +08:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2019-06-24 13:20:08 +08:00
|
|
|
<DndProvider backend={HTML5Backend}>
|
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
dataSource={this.state.data}
|
|
|
|
components={this.components}
|
|
|
|
onRow={(record, index) => ({
|
|
|
|
index,
|
|
|
|
moveRow: this.moveRow,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</DndProvider>
|
2017-11-17 18:53:07 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 13:20:08 +08:00
|
|
|
ReactDOM.render(<DragSortingTable />, mountNode);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|
2017-11-17 18:53:07 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```css
|
2017-11-17 18:53:07 +08:00
|
|
|
#components-table-demo-drag-sorting tr.drop-over-downward td {
|
|
|
|
border-bottom: 2px dashed #1890ff;
|
|
|
|
}
|
|
|
|
|
|
|
|
#components-table-demo-drag-sorting tr.drop-over-upward td {
|
|
|
|
border-top: 2px dashed #1890ff;
|
|
|
|
}
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|