a-table 动态列宽拖拽 vue

所需依赖及版本:

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

"ant-design-vue": "1.7.8"

封装dragMixin文件:

复制代码
import VueDraggableResizable from 'vue-draggable-resizable';
import '@/assets/less/draggableResizable.less'

export const draggableResizable = {
  components: {
    VueDraggableResizable
  },
  data() {
    return {}
  },
  created() {
  },
  mounted() {
    let nodeTable = document.getElementsByClassName('ant-table-fixed')
    nodeTable[0].style.removeProperty("width")
  },
  methods: {
    /**
     * 表格列可拖拽
     * 表格上使用::components="drag(columns)"
     * tips:columns中需包含dataIndex或者key和width(Number)
     */
    drag(columns) {
      return {
        header: {
          cell: this.initDrag(columns),
        },
      }
    },
    initDrag(columns) {
      return (h, props, children) => {
        const {key, ...restProps} = props
        const col = columns.find((col) => {
          const k = col.dataIndex || col.key
          return k === key
        })
        if (!col || !col.width) {
          return h('th', {...restProps}, [...children])
        }
        const dragProps = {
          key: col.dataIndex || col.key,
          class: 'table-draggable-handle',
          attrs: {
            w: 10,
            x: col.width,
            z: 1,
            axis: 'x',
            draggable: true,
            resizable: false,
          },
          on: {
            dragging: (x, y) => {
              col.width = Math.max(x, 1)
            },
          },
        }
        const drag = h('vue-draggable-resizable', {...dragProps})
        return h('th', {...restProps, class: 'resize-table-th'}, [...children, drag])
      }
    }
  }
}

dragResizable.less 文件:

复制代码
.resize-table-th {
  position: relative;
  .table-draggable-handle {
    transform: none !important;
    position: absolute;
    height: 100% !important;
    bottom: 0;
    left: auto !important;
    right: 0;
    cursor: col-resize;
    touch-action: none;
  }
}
.ant-table-header-column > div {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

组件内使用:

复制代码
:components="drag(columns)"  利用a-table  components api自定义table  

<a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :components="drag(columns)"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >




</a-table>
相关推荐
归于尽2 分钟前
Generator?从 yield 卡壳,到终于搞懂协程那点事
前端·javascript
FogLetter2 分钟前
React组件开发进阶:本地存储与自定义Hooks的艺术
前端·javascript·react.js
支撑前端荣耀6 分钟前
五、测试用例的组织和编写
前端
支撑前端荣耀6 分钟前
七、命令行运行Cypress
前端
支撑前端荣耀6 分钟前
九、重塑你的“测试习惯”——避开Cypress的那些“坑”
前端
拾光拾趣录8 分钟前
Vite 与 Webpack 热更新原理
前端·webpack·vite
GISer_Jing10 分钟前
前端开发—全栈开发
前端·javascript
great11 分钟前
yarn和npm有什么区别
前端
拾光拾趣录11 分钟前
Flutter跨平台、性能优化与安全
前端·flutter
支撑前端荣耀12 分钟前
六、Cypress与元素交互
前端