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>
相关推荐
AI浩6 小时前
【Labelme数据操作】LabelMe标注批量复制工具 - 完整教程
运维·服务器·前端
涔溪6 小时前
CSS 网格布局(Grid Layout)核心概念、基础语法、常用属性、实战示例和进阶技巧全面讲解
前端·css
2401_878454536 小时前
浏览器工作原理
前端·javascript
西陵7 小时前
为什么说 AI 赋能前端开发,已经不是选择题,而是必然趋势?
前端·架构·ai编程
by__csdn8 小时前
Vue3 setup()函数终极攻略:从入门到精通
开发语言·前端·javascript·vue.js·性能优化·typescript·ecmascript
天天扭码8 小时前
前端如何实现RAG?一文带你速通,使用RAG实现长期记忆
前端·node.js·ai编程
一条可有可无的咸鱼9 小时前
企业招聘信息,企业资讯进行公示
java·vue.js·spring boot·uni-app
Luna-player9 小时前
在前端中,<a> 标签的 href=“javascript:;“ 这个是什么意思
开发语言·前端·javascript
lionliu05199 小时前
js的扩展运算符的理解
前端·javascript·vue.js
小草cys9 小时前
项目7-七彩天气app任务7.4.2“关于”弹窗
开发语言·前端·javascript