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

简介: ant design 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;
}


相关文章
|
前端开发 测试技术 容器
React 快速实现拖拽改变容器宽高度
React 快速实现拖拽改变容器宽高度
685 0
|
Java 数据库连接 程序员
从头到尾手把手教你搭建阅读Mybatis源码的环境(程序员必备技能)
从头到尾手把手教你搭建阅读Mybatis源码的环境(程序员必备技能)
357 0
|
Java Maven
结合NBCIO-BOOT项目最新SpringToolSuite4安装配置
结合NBCIO-BOOT项目最新SpringToolSuite4安装配置
198 0
|
12月前
|
安全 网络安全
网络漏洞
指硬件、软件或策略上的缺陷,这种缺陷导致非法用户 未经授权而获得访问系统的权限或提高其访问权限。有了这种访问权限,非法用户就可以为所欲为,从而造成对网络安全的威胁。 区别于后门。后门:是软硬件制造者为了进行非授权访问而在程序中故意设置的万能访问口令,这些口令无论是被攻破,还是只掌握在制造者手中,都对使用者的系统安全构成严重的威胁。 漏洞与后门是不同的,漏洞是难以预知的,后门则是人为故意设置的。
|
开发框架 JSON API
Python中FastAPI项目使用 Annotated的参数设计
Python中FastAPI项目使用 Annotated的参数设计
代码分割的优势和劣势分别是什么?
代码分割的优势和劣势分别是什么?
189 0
|
SQL 关系型数据库 MySQL
MySQL - 左连接、右连接、内连接、完全外连接、交叉连接 & 一对多、多对一、多对多 & 联合连接
介绍MySQL中不同类型的SQL连接操作,包括左连接、右连接、内连接、完全外连接、交叉连接,以及数据库关系中的一对多、多对一、多对多和联合连接的概念和使用场景。
982 0
|
资源调度 前端开发
antd table表头拖拽实现(一)
antd table表头拖拽实现
1755 0
|
安全 网络安全 数据安全/隐私保护
网络安全:如何保护你的网络不受黑客攻击
【5月更文挑战第10天】 网络安全摘要:强化密码安全,使用防病毒软件,启用防火墙,定期更新软件,使用安全网络连接,备份重要数据,提高安全意识。这七大措施助你构建安全网络环境,抵御黑客攻击。记得持续学习,适应不断变化的威胁。