antd拖拽列导致触发table排序问题,解决方案

6,241次阅读
没有评论

共计 2705 个字符,预计需要花费 7 分钟才能阅读完成。

前言

antd 的 table 表格拖拽列,会导致触发排序问题,解决方案如下,相当于加一个点击的事件判断,如果是拖拽,则不触发。

解决代码如下:

import {Resizable} from 'react-resizable'


class ResizeableTitle extends React.Component {render() {const { onResize, width, onClick, ...restProps} = this.props;
    return ( (this.resizing = true)}
        onResizeStop={() => {setTimeout(() => {this.resizing = false;});
        }}
        onResize={onResize}
      >
         {if (!this.resizing && onClick) {onClick(...args);
            }
          }}
          {...restProps}
        />
      
    );
  }
}

在拖拽表格中使用

components = {
    header: {cell: ResizeableTitle}
  }

完整代码

import React from 'react'
import listCSS  from "./listTable.less"
import 'antd/dist/antd.css'
import zhCN from 'antd/lib/locale/zh_CN'
import PropTypes from 'prop-types'
import {Table,ConfigProvider} from 'antd'
import {Resizable} from 'react-resizable'


class ResizeableTitle extends React.Component {render() {const { onResize, width, onClick, ...restProps} = this.props;
    return ( (this.resizing = true)}
        onResizeStop={() => {setTimeout(() => {this.resizing = false;});
        }}
        onResize={onResize}
      >
         {if (!this.resizing && onClick) {onClick(...args);
            }
          }}
          {...restProps}
        />
      
    );
  }
}


class ResizeableTable extends React.PureComponent {constructor(props) {super(props);

    this.state = {columns: props.columns};
  }

  components = {
    header: {cell: ResizeableTitle}
  }
  calculateColumsWidthSum = (columns = [], firstColWidth = 0, lastColWidth = 0)=> {const arrReducer = (accumulator, currentValue) => {if (!currentValue || !currentValue.width) {return accumulator}

      let width = currentValue.width
      if (typeof width === 'string') {if (width.endsWith('px')) {width = parseFloat(width.split('px')[0])
        } else {return accumulator}
      } else if (typeof width === 'number') {width = parseFloat(width)
      } else {return accumulator}
      return accumulator + width
    }
    return columns.reduce(arrReducer, 0) + firstColWidth + lastColWidth
  }

  componentDidMount() {}

  render() {const {className,tableLayout,rowKey,components,scrollToFirstRowOnChange,locale,columns,lastColumnWidth,firstColumnWidth, ...restProps} = this.props
    const _columns = this.state.columns.map((col, index) => ({
      ...col,
      onHeaderCell: (column) => ({
        width: column.width,
        onResize: this.handleResize(index)
      })
    }))
    let tableScrollWidth = this.calculateColumsWidthSum(_columns, firstColumnWidth, lastColumnWidth)
    const _components = Object.assign({},components,this.components)
    return 
        
} handleResize = (index) => (e, { size}) => {e.stopImmediatePropagation(); this.setState(({columns}) => {const nextColumns = [...columns]; nextColumns[index] = {...nextColumns[index], width: size.width }; return {columns: nextColumns} }) } } ResizeableTable.propTypes = { sticky: PropTypes.object, columns: PropTypes.array, dataSource: PropTypes.array, onChange:PropTypes.func, firstColumnWidth:PropTypes.number, lastColumnWidth:PropTypes.number, scroll:PropTypes.object } ResizeableTable.defaultProps ={sticky:{offsetHeader:56}, columns:[], dataSource:[], firstColumnWidth:65, lastColumnWidth:150 } export default ResizeableTable

github 仓库

github 组件仓库地址 https://github.com/confidence68/ResizeableTable

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于1970-01-01发表,共计2705字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)