ant-design/components/table/demo/drag-sorting-handler.md

151 lines
3.3 KiB
Markdown
Raw Normal View History

---
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';
2022-05-23 14:37:16 +08:00
import { Table } from 'antd';
2022-07-04 22:09:54 +08:00
import type { ColumnsType } from 'antd/es/table';
2021-08-13 17:33:09 +08:00
import { arrayMoveImmutable } from 'array-move';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
import type { SortableContainerProps, SortEnd } from 'react-sortable-hoc';
2022-05-23 14:37:16 +08:00
import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc';
interface DataType {
key: string;
name: string;
age: number;
address: string;
index: number;
}
const DragHandle = SortableHandle(() => <MenuOutlined style={{ cursor: 'grab', color: '#999' }} />);
const columns: ColumnsType<DataType> = [
{
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: 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<HTMLTableRowElement>) => (
<tr {...props} />
));
const SortableBody = SortableContainer((props: React.HTMLAttributes<HTMLTableSectionElement>) => (
<tbody {...props} />
));
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) => (
<SortableBody
2020-12-22 16:46:52 +08:00
useDragHandle
chore: merge master into feature (#28751) * fix: Transfer dataSource cannot be immutable (#28675) close #28662 * docs: fix errors in example code (#28677) * ci: expand ie check (#28673) * ci: expand ie check * Update issue-open-check.yml * perf(📦): reduce @babel/runtime package size (#28678) * perf(📦): reduce @babel/runtime package size https://github.com/ant-design/antd-tools/commit/04cd73dea1206d1119847ffd7342b28b26d0babc * chore(:up:): upgrade @ant-design/react-slick to esm support version * upgrade @ant-design/tools * ci: add open condition (#28682) * fix(Slider): forcePopupAlign null when unmounted (#28699) * docs: Update overview.zh-CN.md (#28703) * docs: Update resources.en-US.md (#28701) * chore: bump rc-select to 12.1.0 (#28715) * fix: stylelint plugin (#28730) * Update package.json * perf(📦): upgrade rc-image to 5.x (#28727) * refactor: upgrade rc-image to 5.x reduce bundle size * upgrade rc-image * upgrade @ant-design/tools https://github.com/ant-design/antd-tools/pull/226 * rc-image 5.0.0 * fix image preview icon missing * refactor code * docs: example of synchronous rc-tree (#28648) * ci: fix outputs type 过程中的使用 string。奇怪啊,之前测试过的,今天点的时候发现不行了 * Update package.json * fix: site overflow cause sticky invalid (#28741) * fix: site overflow cause sticky invalid * disable auto scroll * chore: upgrade rc-dialog and rc-drawer (#28749) * chore: upgrade rc-dialog and rc-drawer (#28687) * chore: upgrade rc-dialog and rc-drawer * upgrade rc-util * update snapshots * upgrade rc-util * upgrade rc-util * update snapshots * upgrade rc-dialog * perf: remove duplicated rc-dialog Co-authored-by: 骗你是小猫咪 <darryshaw@gmail.com> Co-authored-by: bigbigbo <zxb141242@163.com> Co-authored-by: xrkffgg <xrkffgg@gmail.com> Co-authored-by: Yann Pringault <yann.pringault@gmail.com> Co-authored-by: godfather <greenday.wj@foxmail.com> Co-authored-by: Mateusz Wierzbicki <22788841+mateusz-wierzbicki@users.noreply.github.com> Co-authored-by: Kermit <kermitlx@outlook.com> Co-authored-by: AkiJoey <akijoey1010635951@gmail.com> Co-authored-by: qqabcv520 <605655316@qq.com> Co-authored-by: 骗你是小猫咪 <darryshaw@gmail.com>
2021-01-08 10:14:01 +08:00
disableAutoscroll
2020-12-22 16:46:52 +08:00
helperClass="row-dragging"
onSortEnd={onSortEnd}
2020-12-22 16:46:52 +08:00
{...props}
/>
);
const DraggableBodyRow: React.FC<any> = ({ 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 <SortableItem index={index} {...restProps} />;
};
return (
<Table
pagination={false}
dataSource={dataSource}
columns={columns}
rowKey="index"
components={{
body: {
wrapper: DraggableContainer,
row: DraggableBodyRow,
},
}}
/>
);
};
export default App;
```
```css
.row-dragging {
background: #fafafa;
border: 1px solid #ccc;
}
.row-dragging td {
padding: 16px;
}
.row-dragging .drag-visible {
visibility: visible;
}
```