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

#yyds干货盘点 ant design table实现上下行拖拽功能(类组件)

时间:2023-03-24 17:02:12浏览次数:40  
标签:yyds ant const monitor index 上下行 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;
}

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

第一步

第一步 引入第一个类


第二步

找准数据

#yyds干货盘点 ant design table实现上下行拖拽功能(类组件)_ide

第三步

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

#yyds干货盘点 ant design table实现上下行拖拽功能(类组件)_ide_02

第四步

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

#yyds干货盘点 ant design table实现上下行拖拽功能(类组件)_官网_03

实现效果

#yyds干货盘点 ant design table实现上下行拖拽功能(类组件)_ide_04

拖拽后

#yyds干货盘点 ant design table实现上下行拖拽功能(类组件)_ide_05

总结

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

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

相关文章

  • #yyds干货盘点 React+antDesign封装一个tab组件(类组件)
    前言我是歌谣放弃很容易但是坚持一定很酷喜欢我就一键三连哈微信公众号关注前端小歌谣在我们的工作生活中每次学习一个框架我们就不免要封装组件而具备封装一个完美......
  • 实现一个简易的antd表格拖拽
    <a-table:columns="columns":data-source="dataSource"><templateslot="icon"slot-scope="record"><divstyle="cursor:pointer;":drag......
  • 助力信创自主可控,AntDB与浪潮、超聚变完成产品互认
    日前,湖南亚信安慧科技有限公司与浪潮商用机器有限公司、超聚变数字技术有限公司展开产品兼容互认工作。近年来,在数据处理需求快速增长以及信创政策加持的情况下,信创行业活力......
  • 亚信安慧携AntDB数据库入选信通院软件供应链厂商和产品名录
    日前,中国信息通讯研究院(简称:中国信通院)在其主办的3SCON软件供应链安全大会上,发布了软件供应链厂商和产品名录。中国信通院云计算与大数据研究所副所长栗蔚表示,我国软件供应......
  • AntDB数据库助力中国移动结算中心建设
    为响应中国移动集团公司IT集中化的要求:全面落实“十三五”十大战略工程,加快“推动公司IT资源一体化整合“重点专项工作。以IT系统为载体,构建高效运营支撑体系,形成集中化支撑......
  • #yyds干货盘点#一个延迟库恢复的案例
    在日常工作中可能会存在误删数据的情况,今天就简单介绍下如何利用延迟库进行数据库的快速恢复。1.环境准备建立一个测试的主从库,写入一些测试数据,非本文要点,过程略。2.设置延......
  • [FastAPI-14]pydantic多个请求体
    fromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()'''多个请求体{"user":{"username":"string","password":"string"......
  • [FastAPI-13]pydantic请求体接收数据
    fromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()'''创建继承BaseModel的类,定义模型user路径函数中定义形参user,类型为User通过对象use......
  • CentOS 7 : wpa_supplicant (CESA-2021:0808) Vulnerability_day 16
    今天要跟大家分享的是关于CentOS的系统缺陷。下面是关于这个问题的具体的描述:TheremoteCentOSLinux7hosthasapackageinstalledthatisaffectedbyavulnerabil......
  • #yyds干货盘点# LeetCode面试题:插入区间
    1.简述:给你一个无重叠的,按照区间起始端点排序的区间列表。在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。 示例 1:输入:i......