react dnd 拖拽antd table

1、封装拖拽Table

import React, { useState, useCallback, useRef, useEffect } from 'react'

import { DndProvider, useDrag, useDrop } from 'react-dnd'

import HTML5Backend from 'react-dnd-html5-backend'

import { Table } from "antd"

import update from 'immutability-helper'

 

const ItemTypes = {

ROW: 'row',

}

 

function DragTable(props) {

 

const { dataSource, columns, onChange, ...tableProps } = props;

 

const [list, setList] = useState([])

 

useEffect(() => {

setList(dataSource)

}, [dataSource])

 

const handleDrag = useCallback(

(dragIndex, hoverIndex) => {

const dragItem = list[dragIndex]

const newList = update(list, {

$splice: [

[dragIndex, 1],

[hoverIndex, 0, dragItem],

],

})

setList(newList);

onChange(newList);

},

[list],

)

 

const DraggableRow = (props) => {

 

const { index, record, handleDrag, ...restProps } = props;

 

const ref = useRef(null)

 

// 接收

const [{ isOver, canDrop }, drop] = useDrop({

accept: ItemTypes.ROW,

drop: (col) => handleDrag(col.index, index),

collect: (monitor) => ({

isOver: monitor.isOver(),

canDrop: monitor.canDrop(),

}),

})

 

const rowStyle = { ...restProps.style }

if (isOver && canDrop) {

rowStyle.background = 'green'

}

 

// 拖拽

const [{ isDragging }, drag] = useDrag({

item: { type: ItemTypes.ROW, record, index },

collect: monitor => ({

isDragging: monitor.isDragging(),

}),

})

const opacity = isDragging ? 0 : 1

 

drag(drop(ref))

return (

<tr ref={ref} {...restProps} style={{ ...rowStyle, opacity }}></tr>

)

};

 

const components = {

body: {

row: DraggableRow,

},

};

 

return (

<DndProvider backend={HTML5Backend}>

<Table

{...tableProps}

components={components}

dataSource={list}

columns={columns}

onRow={(record, index) => ({

record,

index,

handleDrag

})}

/>

</DndProvider>

)

}

export default DragTable

 

2、使用组件

 

import React from 'react'

import DragTable from "./DragTable"

 

function DragDemo(props) {

const list = [

{ id: 1, text: 'Write a cool JS library', },

{ id: 2, text: 'Make it generic enough', },

{ id: 3, text: 'Write README', },

{ id: 4, text: 'Create some examples', },

{ id: 5, text: 'note that this element is taller than the others', },

{ id: 6, text: '???', },

{ id: 7, text: 'PROFIT', },

]

const columns = [

{

title: "ID",

dataIndex: 'id',

key: 'id',

},

{

title: "text",

dataIndex: 'text',

key: 'text',

}

]

 

function handleTableDrag(list) {

console.log(list)

}

 

return <DragTable

rowKey="id"

dataSource={list}

columns={columns}

pagination={false}

onChange={handleTableDrag} />

}

export default DragDemo

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
React DND(Drag and Drop)是一个用于实现功能的React库。排序是指在页面中的某个区域内,可以通过元素的方式改变元素的顺序。 在React DND中,我们可以通过使用DragSource和DropTarget组件来实现排序。DragSource组件是被的元素,而DropTarget组件是被元素放下的目标容器。 首先,我们需要将需要排序的元素包装在DragSource组件中。这可以通过使用DragSource高阶组件来实现,该组件接受一个配置对象作为参数,其中包含了的行为和数据。 接下来,我们需要将目标容器包装在DropTarget组件中。同样,可以通过使用DropTarget高阶组件来实现。DropTarget组件也接受一个配置对象作为参数,其中定义了接受元素的行为和数据。 一旦配置完成,我们就可以在页面中看到被包装的元素可以,并且可以放置在DropTarget组件中。 为了实现排序,我们可以在DropTarget组件的配置对象中定义一个回调函数,用于处理元素放置时的逻辑。在这个回调函数中,我们可以获取被的元素和被放置的位置,并更新元素的顺序。 通过在配置对象中定义其他选项,如canDrop和isDragging等,我们还可以进一步控制排序的行为,如限制放置的条件和展示不同的样式等。 总结来说,使用React DND可以简单快捷地实现排序功能。通过使用DragSource和DropTarget组件以及配置对象,我们可以定义的行为和数据,并在放置时执行自定义的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值