• a-table 动态列宽拖拽 vue


    所需依赖及版本:

    "vue-draggable-resizable": "^2.3.0"

    "ant-design-vue": "1.7.8"

    封装dragMixin文件:

    1. import VueDraggableResizable from 'vue-draggable-resizable';
    2. import '@/assets/less/draggableResizable.less'
    3. export const draggableResizable = {
    4. components: {
    5. VueDraggableResizable
    6. },
    7. data() {
    8. return {}
    9. },
    10. created() {
    11. },
    12. mounted() {
    13. let nodeTable = document.getElementsByClassName('ant-table-fixed')
    14. nodeTable[0].style.removeProperty("width")
    15. },
    16. methods: {
    17. /**
    18. * 表格列可拖拽
    19. * 表格上使用::components="drag(columns)"
    20. * tips:columns中需包含dataIndex或者key和width(Number)
    21. */
    22. drag(columns) {
    23. return {
    24. header: {
    25. cell: this.initDrag(columns),
    26. },
    27. }
    28. },
    29. initDrag(columns) {
    30. return (h, props, children) => {
    31. const {key, ...restProps} = props
    32. const col = columns.find((col) => {
    33. const k = col.dataIndex || col.key
    34. return k === key
    35. })
    36. if (!col || !col.width) {
    37. return h('th', {...restProps}, [...children])
    38. }
    39. const dragProps = {
    40. key: col.dataIndex || col.key,
    41. class: 'table-draggable-handle',
    42. attrs: {
    43. w: 10,
    44. x: col.width,
    45. z: 1,
    46. axis: 'x',
    47. draggable: true,
    48. resizable: false,
    49. },
    50. on: {
    51. dragging: (x, y) => {
    52. col.width = Math.max(x, 1)
    53. },
    54. },
    55. }
    56. const drag = h('vue-draggable-resizable', {...dragProps})
    57. return h('th', {...restProps, class: 'resize-table-th'}, [...children, drag])
    58. }
    59. }
    60. }
    61. }

    dragResizable.less 文件:

    1. .resize-table-th {
    2. position: relative;
    3. .table-draggable-handle {
    4. transform: none !important;
    5. position: absolute;
    6. height: 100% !important;
    7. bottom: 0;
    8. left: auto !important;
    9. right: 0;
    10. cursor: col-resize;
    11. touch-action: none;
    12. }
    13. }
    14. .ant-table-header-column > div {
    15. overflow: hidden;
    16. white-space: nowrap;
    17. text-overflow: ellipsis;
    18. }

    组件内使用:

    1. :components="drag(columns)" 利用a-table components api自定义table
    2. <a-table
    3. ref="table"
    4. size="middle"
    5. :scroll="{ x: true }"
    6. bordered
    7. rowKey="id"
    8. :components="drag(columns)"
    9. :columns="columns"
    10. :dataSource="dataSource"
    11. :pagination="ipagination"
    12. :loading="loading"
    13. :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    14. class="j-table-force-nowrap"
    15. @change="handleTableChange"
    16. >
    17. </a-table>

  • 相关阅读:
    [Swift]定义一个全局的可管理的计时器
    关于js_节流的介绍和简单的使用
    Flutter图标
    GPT-3后的下一步:大型语言模型的未来方向
    Qt | 拖放、拖动的使用、将文件拖入使用示例
    基于Spring Boot的在线考试系统springboot19
    自动驾驶技术中常用英文单词及缩略语整理
    windows系统装成Ubuntu或linux后,无线/有线网卡无法连接或找不到适配器
    【for lovelier】IDEA + LeetCode Editor 最佳实践
    CSS盒子定位的扩张
  • 原文地址:https://blog.csdn.net/qq_40963664/article/details/133846841