Ant Design + react-drag-listview实现Table拖拽变换列位置

Ant Design + react-drag-listview + react-resizable 实现拖拽变换列位置, 可改变列宽

废话不多说,直接上代码

一:单独使用,只实现拖拽变换列位置

import React from 'react';
import { Table } from 'antd';
import ReactDragListView from 'react-drag-listview'class DragTable extends React.Component {constructor(props) {super(props);this.state = {columns: this.props.columns}}static getDerivedStateFromProps(nextProps, prevState) {return null;}render() {const { components, columns, ...others } = this.props;const that = this;const dragProps = {onDragEnd(fromIndex, toIndex) {const columns = [...that.state.columns];const item = columns.splice(fromIndex, 1)[0];columns.splice(toIndex, 0, item);that.setState({columns});},nodeSelector: "th"};return (<ReactDragListView.DragColumn {...dragProps}><Table columns={this.state.columns}{ ...others }></Table></ReactDragListView.DragColumn>)}
}export default DragTable;

二:与react-resizable结合使用,可改变列宽,可改变列位置

react-resizable可参Ant Design + react-resizable实现列表页可拖拽宽度变化_仗剑走天涯的博客-CSDN博客

DragTable 组件封装:

import React from 'react';
import { Table } from 'antd';
import { Resizable } from 'react-resizable';
import ReactDragListView from 'react-drag-listview'
import 'react-resizable/css/styles.css';
import './index.css'const ResizeableTitle = (props) => {const { onResize, width, ...restProps } = props;if (!width) {return <th {...restProps} />;}return (<Resizablewidth={width?width:null}height={0}minConstraints={[50, 50]}onResize={onResize}draggableOpts={{ enableUserSelectHack: false }}><th {...restProps} /></Resizable>);
}class DragTable extends React.Component {constructor(props) {super(props);this.state = {columns: this.props.columns}}static getDerivedStateFromProps(nextProps, prevState) {return null;}components = {header: {cell: ResizeableTitle,},};handleResize = index => (e, { size }) => {this.setState(({ columns }) => {const nextColumns = [...columns];nextColumns[index] = {...nextColumns[index],width: size.width?size.width:'100%',};return { columns: nextColumns };});}render() {const { components, columns, ...others } = this.props;const that = this;const dragProps = {onDragEnd(fromIndex, toIndex) {const columns = [...that.state.columns];const item = columns.splice(fromIndex, 1)[0];columns.splice(toIndex, 0, item);that.setState({columns});},nodeSelector: "th",handleSelector: 'p'};const newColumns = this.state.columns.map((col, index) => ({...col,onHeaderCell: column => ({width: column.width?column.width:100, // 100 没有设置宽度可以在此写死 例如100试下onResize: this.handleResize(index),}),})) return (<ReactDragListView.DragColumn {...dragProps}><Table components={this.components}columns={newColumns}{ ...others }></Table></ReactDragListView.DragColumn>)}
}export default DragTable;

样式调整

.react-resizable-handle-se {top: 0;right: 0;cursor: w-resize;
}.react-resizable-handle {position: absolute;width: 10px;height: calc(100% - 10px);background-repeat: no-repeat;background-origin: content-box;box-sizing: border-box;background-image: none;background-position: bottom right;border-right: 1px solid #f0f0f0;margin: 5px 0px;padding: 0px;
}.ant-table-header .ant-table-cell p {width: calc(100% - 15px);cursor: move;margin-left: 5px;
}.ant-table thead > tr > th {margin-right: 10px;
}.ant-table-header {-moz-user-select:none; /* Firefox私有属性 */-webkit-user-select:none; /* WebKit内核私有属性 */-ms-user-select:none; /* IE私有属性(IE10及以后) */-khtml-user-select:none; /* KHTML内核私有属性 */-o-user-select:none; /* Opera私有属性 */user-select:none; /* CSS3属性 */
}.ant-table-header th:last-child .react-resizable-handle {display: none;
}

使用:

import React from 'react';
import DragTable from '../../components/DragTable';class APP extends React.Component {constructor(props) {super(props);this.state = {loading: false,data: []}}render() {const columns = [{ title: () => { return <p>name</p> }, dataIndex: 'name', key: 'name', ellipsis: true },{ title: () => { return <p>value</p> }, dataIndex: 'value', key: 'value', ellipsis: true }]return (<DragTable columns={columns}loading={this.state.loading}rowKey='id'dataSource={this.state.data}size='small'pagination={false}style={{marginTop: 10}}rowClassName={(record, index) => {}}></DragTable>);}
}

Ant Design + react-drag-listview实现Table拖拽变换列位置相关推荐

  1. Ant Design + react-resizable实现列表页可拖拽宽度变化

    Ant Design + react-resizable实现列表页可拖拽宽度变化 查找了下Ant Design中的Table组件,没有发现表格header列头可以拖拽实现宽度变化的功能,领导要想要这种 ...

  2. ant design vue利用rowClassName给table添加行样式

    ant design vue利用rowClassName给table添加行样式 目录 1. 需求:表格每行数据hasVerdict值不为'1'时标红显示 2. 实现方式:table属性rowClass ...

  3. android ListView和GridView拖拽移位具体实现及拓展

    关于ListView拖拽移动位置,想必大家并不陌生,比较不错的软件都用到如此功能了.如:搜狐,网易,百度等,但是相比来说还是百度的用户体验较好,不偏心了,下面看几个示例:              首 ...

  4. iview table 自定义列_案例 | iview中Table:拖拽适配列、自定义固定列、合并行

    文 / 景朝霞 来源公号 / 朝霞的光影笔记 ID / zhaoxiajingjing ❥❥❥❥点个赞,让我知道你来过~❥❥❥❥ 0 / 更新Table "iview": &quo ...

  5. HTML 锁定表格首行首列、拖拽表格列宽

    摘自:http://s.yanghao.org/program/viewdetail.php?i=61717 因为与自己的实际情况有些不同,简单的改造了下...如果有版权问题,请留言... 锁定Tab ...

  6. Element UI表格拖拽(vue中) —— 行拖拽、列拖拽

    目录 安装依赖 vuedraggable 实现拖拽的要点 行拖拽要点 列拖拽要点 完整范例代码 安装依赖 vuedraggable 安装  vuedraggable 的同时,会自动安装 sortabl ...

  7. Vue2 + ant design vue1.7.8版本 Table组件 手动拖拽、可伸缩列功能

    低版本的antd table组件 官网文档上有可伸缩列功能,但是用起来有很明显的bug,无法直接拿来用:接下来给大家提供一个手写的拖拽方案: 1.首先,写好v-table组件,这里最重要的就是tabl ...

  8. 弹窗案例实现(Ant Design + React hooks)

    Ant Design 是React里常用的UI框架 我们选用的是Ant Design 的 Modal 我们可以从Ant Design 官网看到一个基本的弹框大致长这个样子 一个弹框,大概需要弹框名称, ...

  9. React基于antd Table实现可拖拽调整列宽的表格

    实现功能 1:表格列宽初始自动分配.列宽总和不能超过容器宽度(无横向滚动条,公司项目特殊需求) 2:当容器宽度变化时,保持当前列宽的分配比例,等比缩小 3:拖动过程中不进行列宽的调整,只有释放之后再进 ...

最新文章

  1. Java 二维数组的初始化
  2. php tostring(),【php】“__toString()”方法使用,php__tostring
  3. Android第一个程序截图,java – 在Android上截取屏幕截图
  4. 你知道电脑如何录制屏幕视频吗?快来看看这两个实用方法
  5. 计算机毕业设计题目汇总 - 简单的毕设选题
  6. QQ浏览器的历史记录在那 QQ浏览器查看浏览历史的方法
  7. 【一年总结】记我的大一生活
  8. Velodyne-16完全配置
  9. 打印机经常遇到的一些问题
  10. 使用物理学和领域知识的神经网络的无标签监督解读(上)
  11. C# Predefined type 'System.Object' is not defined or imported
  12. wps 写论文时 参考文献的横线怎么消除
  13. sae bae微信上墙|微信墙完美版(php源码),最新微信表白墙源码 微信互动墙 微信留言板 微信留言墙源码 - A保站...
  14. 利用Jquery自动生成条形码
  15. 决定物联网安全的有哪些因素
  16. pandas之用loc、iloc、ix进行索引以及切片
  17. Android指纹识别,看这一篇就够了
  18. 面试 - 阿里华为资深HR面试套路全揭晓
  19. 【SpringCloud技术专题】「Hystrix」(9)熔断器的原理和实现机制
  20. 云计算发展与BPaaS构建企业Private PaaS分析

热门文章

  1. springboot+vue在线答疑系统(源码+说明文档)
  2. 工作篇 之 解决谷歌地图与 NestedScrollView 滑动冲突
  3. 微信小程序毕业设计 基于微信小程序在线电子书阅读系统开题报告
  4. CAD图层的顺序的调整
  5. C# vb .net实现焦距淡色特效滤镜
  6. 索尼6400夜景测试 镜头索尼18-55
  7. 根据经纬度调用Google地图显示对应位置
  8. python解一元二次方程复数_一元二次方程求解(包括复数各种情况)
  9. 杭州辣府餐饮JAVA_超全“滨江美食必打卡list”,不收藏会后悔!年前再去搓几顿啊~...
  10. 勇敢做自己,女神节快乐!