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>
相关推荐
铅笔侠_小龙虾5 分钟前
深入理解 Vue.js 原理
前端·javascript·vue.js
西西学代码9 分钟前
Flutter---showCupertinoDialog
java·前端·flutter
你的眼睛會笑11 分钟前
vue3 使用html2canvas实现网页截图并下载功能 以及问题处理
前端·javascript·vue.js
ZTLJQ22 分钟前
植物大战僵尸HTML5游戏完整实现教程
前端·游戏·html5
无光末阳38 分钟前
vue 环境下多个定时器的创建与暂停的统一封装
前端·vue.js
Hilaku41 分钟前
技术Leader的“第一性原理”:我是如何做技术决策的?
前端·javascript·面试
liyf41 分钟前
发布-订阅(Publish–Subscribe) vs 观察者模式(Observer Pattern)
前端
云中雾丽1 小时前
Flutter 里的 Riverpod 用法解析
前端
前端snow1 小时前
记录:非常典型的一个redux问题
前端
慧一居士1 小时前
src/App.vue 和 public/index.html 关系和区别
前端·vue.js