首页 > 其他分享 >ant design table实现上下行拖拽功能(类组件)

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

时间:2023-04-15 21:04:10浏览次数:35  
标签:ant const monitor index 上下行 design key props hoverIndex

前言

最好的种树是十年前,其次是现在。歌谣 每天一个前端小知识 提醒你改好好学习了 知乎博主 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 (
      <DndProvider backend={HTML5Backend}>
        <Table
          columns={columns}
          dataSource={this.state.data}
          components={this.components}
          onRow={(record, index) => ({
            index,
            moveRow: this.moveRow,
          })}
        />
      </DndProvider>
    );
  }
}
 
ReactDOM.render(<DragSortingTable />, 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;
}

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

第一步

第一步 引入第一个类


第二步

找准数据

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

第三步

进行数据的一个赋值我这边是dva.js赋值

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

第四步

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

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

实现效果

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

拖拽后

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

总结

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

标签:ant,const,monitor,index,上下行,design,key,props,hoverIndex
From: https://blog.51cto.com/u_14476028/6192445

相关文章

  • centos7 无法启动 修复​​Internal error XFS_WANT_CORRUPTED_GOTO​报错
    InternalerrorXFS_WANT_CORRUPTED_GOTO错误前面括号中的就是磁盘名称,比如我的就是dm-0修复完reboot重启就能进系统了......
  • 利用AntDesign中a-tree和checkbox构造组织单位人员树选择组件
    业务效果图核心代码<template><divclass="select-container"><a-modalv-model:visible="visible"@ok="handleOk"@cancel="handleCancel"width="1500px"><template#title>......
  • AntDesign中a-tab的forcerender属性强制DOM渲染
    <a-tabsv-model:activeKey="activeKey"@change="clickTag"><a-tab-panekey="1"tab="警情"v-if="tab01Visible":forceRender="true"><AssociatedElementsInformingDetail......
  • Semantic Kernel 入门系列:
    当我们使用NativeFunction的时候,除了处理一些基本的逻辑操作之外,更多的还是需要进行外部数据源和服务的对接,要么是获取相关的数据,要么是保存输出结果。这一过程在SemanticKernel中可以被归类为Connector。Connector更像是一种设计模式,并不像Function和Memory一样有强制和明确......
  • CS_61A_ant
     defreduce_armor(self,amount):"""ReducearmorbyAMOUNT,andremovetheFireAntfromitsplaceifithasnoarmorremaining.Makesuretodamageeachbeeinthecurrentplace,andapplythebonusif......
  • laravel + node 在vagrant + ubuntu18.04 部署过程
    注意:当前的操作都是基于本地mac开发坏境配置操作,遇到了很多坑,这里借此做记录:一、ubuntu18.04上部署的php环境php7.4+msyql,常用的php扩展之类的可以自行的查询安装二、ubuntu18.04上部署nodejs14.17.6开始搜索直接用命令,装的node不是我们当前所需要的最后决定使......
  • Vant
    移动端UI库官网https://vant-contrib.gitee.io/vant/#/zh-CN/ 之前的旧版本https://youzan.github.io/vant 创建Vue项目 sudocnpminstall-g@vue/cli#打开图形化界面vueui 图形化项目操作真的好酷呀!注意,选择项目预设时,选择自定义,这样可以勾选路由和......
  • codeforces #185 A Plant(矩阵快速幂+递推)
    题目地址:http://codeforces.com/problemset/problem/185/A通过这个题终于找回了点找递推公式的信心。。TAT。。不过真心感觉CF的题目质量都真不错。。。首先,第n个图形的上方,左下方,右下方的三个大三角形是跟第n-1个是一模一样的,所以是3*f(n-1)。然后只剩下中间一个倒着的大三角形了,......
  • Semantic Kernel 入门系列:
    了解的运作原理之后,就可以开始使用SemanticKernel来制作应用了。SemanticKernel将embedding的功能封装到了Memory中,用来存储上下文信息,就好像电脑的内存一样,而LLM就像是CPU一样,我们所需要做的就是从内存中取出相关的信息交给CPU处理就好了。内存配置使用Memory需要注册embe......
  • 亚信科技AntDB数据库荣膺第十二届数据技术嘉年华(DTC 2023)“最具潜力数据库”大奖
    近日,亚信科技AntDB数据库产品在第十二届数据技术嘉年华(DTC2023)峰会上斩获“2022年度最具潜力数据库”大奖。亚信安慧副总裁张桦先生受邀参会,并发表了《AntDB数据库通信行业核心系统应用与创新》的主题演讲,分享了AntDB数据库在大规模数据管理项目上的实战经验,讲述了AntDB在“超融合......