使用 react-resizable实现表格列的拖拽
拖拽表格的borer进行列宽调整
react table文档
https://github.com/react-grid-layout/react-resizable
https://blog.csdn.net/mingwei_zhu/article/details/116781122
https://codesandbox.io/s/drag-row-1fjg4?file=/index.js
https://3x.ant.design/components/table-cn/#components-table-demo-resizable-column
image.png
【注意】

  • 报错依赖包报错,还需要引入react-draggable包,并重启项目
  • 使用react-resizable 时必须引入其包中style.css文件,或者粘贴至自己的css中引用,否则不显示拖拽图标
  • 传入的columns一定要每列都要设置列宽,设置列宽属性 width:number,不然对应列的后面不会出现拖拽标识,或者出现了,但是不可拖拽

拖拽表格样式

  • 下载的包里可以找到css代码。 css需要是全局的,
  • import’../node_modules/react-resizable/css/styles.css’;
  • 如果是 styleComponent,要用 :global{} 包裹一下试下 ```less

// 表格拖拽样式 .react-resizable { position: relative; background-clip: padding-box; }

.react-resizable-handle { position: absolute; width: 10px; height: 100%; bottom: 0; right: -5px; cursor: col-resize; z-index: 1; }

// 禁止用户选中表头文字 tr { th { -webkit-user-select: none; user-select: none; / Chrome and Opera / } }

  1. - 一定要针对表头文字进行禁止用户选择,不然用户按住鼠标左键从文字滑动的时候,选中了文字,
  2. - 然后再去拖拽,会发现松了鼠标后,依然可以进行拖拽,所以禁止用户选中表头文字
  3. <a name="VFWJS"></a>
  4. ## ResizeColumns
  5. ```jsx
  6. import React from 'react';
  7. import { Table } from 'antd';
  8. import { Resizable } from 'react-resizable';
  9. const ResizeableTitle = props => {
  10. const { onResize, width, ...restProps } = props;
  11. if (!width) {
  12. return <th {...restProps} />;
  13. }
  14. return (
  15. <Resizable
  16. width={width}
  17. height={0}
  18. onResize={onResize}
  19. draggableOpts={{ enableUserSelectHack: false }}
  20. >
  21. <th {...restProps} />
  22. </Resizable>
  23. );
  24. };
  25. class ResizeColumns extends React.Component {
  26. state = {
  27. columns: [
  28. {
  29. title: 'Date',
  30. dataIndex: 'date',
  31. width: 200,
  32. },
  33. {
  34. title: 'Amount',
  35. dataIndex: 'amount',
  36. width: 100,
  37. },
  38. {
  39. title: 'Type',
  40. dataIndex: 'type',
  41. width: 100,
  42. },
  43. {
  44. title: 'Note',
  45. dataIndex: 'note',
  46. width: 100,
  47. },
  48. {
  49. title: 'Action',
  50. key: 'action',
  51. render: () => <a>Delete</a>,
  52. },
  53. ],
  54. };
  55. components = {
  56. header: {
  57. cell: ResizeableTitle,
  58. },
  59. };
  60. data = [
  61. {
  62. key: 0,
  63. date: '2018-02-11',
  64. amount: 120,
  65. type: 'income',
  66. note: 'transfer',
  67. },
  68. {
  69. key: 1,
  70. date: '2018-03-11',
  71. amount: 243,
  72. type: 'income',
  73. note: 'transfer',
  74. },
  75. {
  76. key: 2,
  77. date: '2018-04-11',
  78. amount: 98,
  79. type: 'income',
  80. note: 'transfer',
  81. },
  82. ];
  83. handleResize = index => (e, { size }) => {
  84. this.setState(({ columns }) => {
  85. const nextColumns = [...columns];
  86. nextColumns[index] = {
  87. ...nextColumns[index],
  88. width: size.width,
  89. };
  90. return { columns: nextColumns };
  91. });
  92. };
  93. render() {
  94. const columns = this.state.columns.map((col, index) => ({
  95. ...col,
  96. onHeaderCell: column => ({
  97. width: column.width,
  98. onResize: this.handleResize(index),
  99. }),
  100. }));
  101. return (
  102. <Table
  103. bordered
  104. className='resizeTable'
  105. components={this.components}
  106. columns={columns}
  107. dataSource={this.data}
  108. />
  109. );
  110. }
  111. }
  112. export default ResizeColumns;

必须要有的样式

  1. .resizeTable .react-resizable {
  2. position: relative;
  3. background-clip: padding-box;
  4. }
  5. .resizeTable .react-resizable-handle {
  6. position: absolute;
  7. width: 10px;
  8. height: 100%;
  9. bottom: 0;
  10. right: -5px;
  11. cursor: col-resize;
  12. z-index: 1;
  13. }

react-resizable

react-resizable https://github.com/react-grid-layout/react-resizable
react-resizable文档 https://strml.github.io/react-resizable/examples/1.html
image.png