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>
相关推荐
cz追天之路6 小时前
华为机考--- 字符串最后一个单词的长度
javascript·css·华为·less
Light606 小时前
CSS逻辑革命:原生if()函数如何重塑我们的样式编写思维
前端·css·响应式设计·组件化开发·css if函数·声明式ui·现代css
蜡笔小嘟6 小时前
宝塔安装dify,更新最新版本--代码版
前端·ai编程·dify
Irene19917 小时前
Vue:useSlots 和 useAttrs 深度解析
vue.js·useslots·useattrs
ModyQyW7 小时前
HBuilderX 4.87 无法正常读取 macOS 环境配置的解决方案
前端·uni-app
bitbitDown7 小时前
我的2025年终总结
前端
五颜六色的黑7 小时前
vue3+elementPlus实现循环列表内容超出时展开收起功能
前端·javascript·vue.js
wscats8 小时前
Markdown 编辑器技术调研
前端·人工智能·markdown
EnoYao8 小时前
Markdown 编辑器技术调研
前端·javascript·人工智能
JIngJaneIL8 小时前
基于java+ vue医院管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot