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>
相关推荐
lecepin16 分钟前
AI Coding 资讯 2025-09-17
前端·javascript·面试
IT_陈寒18 分钟前
React 18实战:7个被低估的Hooks技巧让你的开发效率提升50%
前端·人工智能·后端
树上有只程序猿1 小时前
终于有人把数据库讲明白了
前端
猩兵哥哥1 小时前
前端面向对象设计原则运用 - 策略模式
前端·javascript·vue.js
司宸1 小时前
Prompt设计实战指南:三大模板与进阶技巧
前端
RoyLin1 小时前
TypeScript设计模式:抽象工厂模式
前端·后端·typescript
华仔啊1 小时前
Vue3+CSS 实现的 3D 卡片动画,让你的网页瞬间高大上
前端·css
江城开朗的豌豆1 小时前
解密React虚拟DOM:我的高效渲染秘诀 🚀
前端·javascript·react.js
vivo互联网技术1 小时前
拥抱新一代 Web 3D 引擎,Three.js 项目快速升级 Galacean 指南
前端·three.js