ant design table实现上下行拖拽功能(类组件)

文章介绍了如何利用React的Dnd库和antd组件,结合immutability-helper来实现表格行的拖拽排序功能。通过定义DragSource和DropTarget,以及处理数据移动的回调函数,实现了在表格中动态调整行顺序的效果。
摘要由CSDN通过智能技术生成

前言

最好的种树是十年前,其次是现在。歌谣 每天一个前端小知识 提醒你改好好学习了 知乎博主 csdn博主 b站博主 放弃很容易但是坚持一定很酷 我是歌谣 喜欢就一键三连咯 你得点赞是对歌谣最大的鼓励 微信公众号关注前端小歌谣

起始

首先刚开始知道要书写一个这样的功能我的内心是比较崩溃的 完全没有思路 然后就打开官网的文档进行观看

开始

一开始准备写 打开官网的一个文档是4.0的 看起来也是一脸的蒙蔽 接着找到3的一个文档

代码部分

``` import { Table } from 'antd'; import { DndProvider, DragSource, DropTarget } from 'react-dnd'; import { HTML5Backend } from 'react-dnd-html5-backend'; import update from 'immutability-helper';

let dragingIndex = -1;

class BodyRow extends React.Component { render() { const { isOver, connectDragSource, connectDropTarget, moveRow, ...restProps } = this.props; const style = { ...restProps.style, cursor: 'move' };

let { className } = restProps;
if (isOver) {
  if (restProps.index > dragingIndex) {
    className += ' drop-over-downward';
  }
  if (restProps.index < dragingIndex) {
    className += ' drop-over-upward';
  }
}

return connectDragSource(
  connectDropTarget(<tr {...restProps} className={className} style={style} />),
);

} }

const rowSource = { beginDrag(props) { dragingIndex = props.index; return { index: props.index, }; }, };

const rowTarget = { drop(props, monitor) { const dragIndex = monitor.getItem().index; const hoverIndex = props.index;

// Don't replace items with themselves
if (dragIndex === hoverIndex) {
  return;
}

// Time to actually perform the action
props.moveRow(dragIndex, hoverIndex);

// Note: we're mutating the monitor item here!
// Generally it's better to avoid mutations,
// but it's good here for the sake of performance
// to avoid expensive index searches.
monitor.getItem().index = hoverIndex;

}, };

const DragableBodyRow = DropTarget('row', rowTarget, (connect, monitor) => ({ connectDropTarget: connect.dropTarget(), isOver: monitor.isOver(), }))( DragSource('row', rowSource, connect => ({ connectDragSource: connect.dragSource(), }))(BodyRow), );

const columns = [ { title: 'Name', dataIndex: 'name', key: 'name', }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, ];

class DragSortingTable extends React.Component { state = { 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', }, ], };

components = { body: { row: DragableBodyRow, }, };

moveRow = (dragIndex, hoverIndex) => { const { data } = this.state; const dragRow = data[dragIndex];

this.setState(
  update(this.state, {
    data: {
      $splice: [[dragIndex, 1], [hoverIndex, 0, dragRow]],
    },
  }),
);

};

render() { return ( ); } }

ReactDOM.render( , mountNode);

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; } ```

这是官网的示例 那么我们如何实现呢

第一步

第一步 引入第一个类

在这里插入图片描述 ## 第二步

找准数据

在这里插入图片描述

第三步

进行数据的一个赋值我这边是dva.js赋值 在这里插入图片描述

第四步

回调或者确定按钮处理数据(这边是确定按钮处理值然后调接口)

在这里插入图片描述

实现效果

在这里插入图片描述

拖拽后

在这里插入图片描述

总结

人生本是一场修行 且行且珍惜

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Ant Design Vue中实现表格行的拖动,你可以使用Ant Design Vue提供的Draggable组件。下面是一个示例,展示了如何使用Ant Design Vue实现表格行的拖动: 1. 首先,确保你已经安装了Ant Design Vue。你可以使用以下命令来安装它: ```bash npm install ant-design-vue@next ``` 2. 在你的Vue组件中引入所需的组件和样式: ```vue <template> <div> <a-table :columns="columns" :dataSource="dataSource"> <template #body="{ row, index }"> <draggable v-model="dataSource" :element="'tr'" :options="dragOptions"> <tr :key="index"> <td>{{ row.name }}</td> <td>{{ row.age }}</td> <td>{{ row.address }}</td> </tr> </draggable> </template> </a-table> </div> </template> <script> import { Table, Draggable } from 'ant-design-vue'; import 'ant-design-vue/dist/antd.css'; export default { components: { 'a-table': Table, draggable: Draggable, }, data() { return { columns: [ { title: 'Name', dataIndex: 'name', key: 'name' }, { title: 'Age', dataIndex: 'age', key: 'age' }, { title: 'Address', dataIndex: 'address', key: 'address' }, ], dataSource: [ { key: 1, name: 'John', age: 28, address: 'New York' }, { key: 2, name: 'Alice', age: 32, address: 'London' }, { key: 3, name: 'Bob', age: 24, address: 'Paris' }, ], dragOptions: { animation: 200, handle: '.ant-table-row', }, }; }, }; </script> ``` 在上面的代码中,我们使用了Ant Design Vue的Table和Draggable组件。我们将Draggable组件包裹在表格行的模板中,以实现行的拖动。我们还定义了一些样式选项,例如动画效果和拖动的句柄。 这样,你就可以在Ant Design Vue中实现表格行的拖动了。根据你的需求,你可以自定义样式和行为,以满足你的项目需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值