--- 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). ```tsx import { MenuOutlined } from '@ant-design/icons'; import { Table } from 'antd'; import type { ColumnsType } from 'antd/es/table'; import { arrayMoveImmutable } from 'array-move'; import React, { useState } from 'react'; import type { SortableContainerProps, SortEnd } from 'react-sortable-hoc'; import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc'; interface DataType { key: string; name: string; age: number; address: string; index: number; } const DragHandle = SortableHandle(() => ); const columns: ColumnsType = [ { title: 'Sort', dataIndex: 'sort', width: 30, className: 'drag-visible', render: () => , }, { title: 'Name', dataIndex: 'name', className: 'drag-visible', }, { title: 'Age', dataIndex: 'age', }, { title: 'Address', dataIndex: 'address', }, ]; const data: DataType[] = [ { 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: React.HTMLAttributes) => ( )); const SortableBody = SortableContainer((props: React.HTMLAttributes) => ( )); const App: React.FC = () => { const [dataSource, setDataSource] = useState(data); const onSortEnd = ({ oldIndex, newIndex }: SortEnd) => { if (oldIndex !== newIndex) { const newData = arrayMoveImmutable(dataSource.slice(), oldIndex, newIndex).filter( (el: DataType) => !!el, ); console.log('Sorted items: ', newData); setDataSource(newData); } }; const DraggableContainer = (props: SortableContainerProps) => ( ); const DraggableBodyRow: React.FC = ({ className, style, ...restProps }) => { // function findIndex base on Table rowKey props and should always be a right array index const index = dataSource.findIndex(x => x.index === restProps['data-row-key']); return ; }; return ( ); }; export default App; ``` ```css .row-dragging { background: #fafafa; border: 1px solid #ccc; } .row-dragging td { padding: 16px; } .row-dragging .drag-visible { visibility: visible; } ```