ant design中实现table的表格行的拖拽

 更新时间:2022年03月22日 11:11:48   作者:前端歌谣  
这篇文章主要介绍了ant design中实现table的表格行的拖拽,文章围绕table表格行拖拽实现的相关资料展开详细的代码内容,具有一定的参考价值,需要的小伙伴可以参考一下

前言:

 首先刚开始知道要书写一个这样的功能我的内心是比较崩溃的 完全没有思路,   然后就打开官网的文档进行观看。一开始准备写 打开官网的一个文档是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;
}

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

第一步 引入第一个类

第二步 找准数据

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

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

总结:

这样的话 表格行就可以进行拖拽操作了

到此这篇关于ant design中实现table的表格行的拖拽的文章就介绍到这了,更多相关table表格行拖拽实现内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • javascript中Function类型详解

    javascript中Function类型详解

    这篇文章主要介绍了javascript中Function类型详解的相关资料,需要的朋友可以参考下
    2015-04-04
  • js采用map取到id集合组并且实现点击一行选中一行

    js采用map取到id集合组并且实现点击一行选中一行

    本文为大家介绍下如何使用js采用map取到id集合组,并且点击一行选中一行
    2013-12-12
  • 使用OPENLAYERS3实现点选的方法

    使用OPENLAYERS3实现点选的方法

    这篇文章主要为大家详细介绍了使用OPENLAYERS3实现点选的几种方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • 详解JVM系列之内存模型

    详解JVM系列之内存模型

    JVM是一种用于计算设备的规范,它是一个虚构出来的机器,是通过在实际的计算机上仿真模拟各种功能实现的。JVM的内存区域可以被分为:线程、栈、堆、静态方法区。本文将介绍JVM的内存模型,感兴趣的小伙伴,可以参考下
    2021-06-06
  • js时间查询插件使用详解

    js时间查询插件使用详解

    这篇文章主要为大家详细介绍了js时间查询插件的使用方法,结合bootstrap进行使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • 微信小程序数据监听器使用实例详解

    微信小程序数据监听器使用实例详解

    这篇文章主要介绍了微信小程序数据监听器使用实例,数据监听器用于监听和响应任何属性和数据字段的变化,从而执行特定的操作。它的作用类似于vue中的watch侦听器
    2023-04-04
  • 浅谈js中对象的使用

    浅谈js中对象的使用

    下面小编就为大家带来一篇浅谈js中对象的使用。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • Textbox控件注册回车事件及触发按钮提交事件具体实现

    Textbox控件注册回车事件及触发按钮提交事件具体实现

    Lyncplus客户端中访问Web页面时遇到了TextBox控件回车自动完成按钮的提交事件失效的情况,于是上网查找相关的介绍最终解决了这两个问题,感兴趣的你可以参考下或许对你有所帮助
    2013-03-03
  • js实现导航栏中英文切换效果

    js实现导航栏中英文切换效果

    本篇文章主要分享了javascript实现导航栏中英文切换效果的示例代码,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • echarts折线图实现部分虚线部分实线效果的方法

    echarts折线图实现部分虚线部分实线效果的方法

    在折线图中,通常实线表示实际数据,而虚线用于表示预测数据,这篇文章主要介绍了echarts折线图实现部分虚线部分实线效果的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-09-09

最新评论