当前位置:首页 > VUE

vue实现表格拖动列宽

2026-01-07 06:04:29VUE

实现表格列宽拖动的核心思路

通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖动逻辑。在表头列分隔线处添加可拖拽区域,拖动时动态修改列的宽度。

vue实现表格拖动列宽

基础HTML结构示例

<template>
  <table>
    <thead>
      <tr>
        <th v-for="(col, index) in columns" :key="index" :style="{ width: col.width + 'px' }">
          {{ col.title }}
          <div class="resize-handle" @mousedown="startResize(index, $event)"></div>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, rowIndex) in data" :key="rowIndex">
        <td v-for="(col, colIndex) in columns" :key="colIndex" :style="{ width: col.width + 'px' }">
          {{ row[col.key] }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

CSS样式关键部分

.resize-handle {
  width: 5px;
  height: 100%;
  background: transparent;
  position: absolute;
  right: 0;
  top: 0;
  cursor: col-resize;
}

th {
  position: relative;
  user-select: none;
}

Vue组件实现逻辑

export default {
  data() {
    return {
      columns: [
        { title: '姓名', key: 'name', width: 150 },
        { title: '年龄', key: 'age', width: 100 },
        { title: '地址', key: 'address', width: 300 }
      ],
      data: [...],
      activeIndex: null,
      startX: 0,
      startWidth: 0
    }
  },
  methods: {
    startResize(index, e) {
      this.activeIndex = index
      this.startX = e.clientX
      this.startWidth = this.columns[index].width

      document.addEventListener('mousemove', this.handleResize)
      document.addEventListener('mouseup', this.stopResize)
    },
    handleResize(e) {
      if (this.activeIndex === null) return
      const deltaX = e.clientX - this.startX
      this.columns[this.activeIndex].width = Math.max(30, this.startWidth + deltaX)
    },
    stopResize() {
      this.activeIndex = null
      document.removeEventListener('mousemove', this.handleResize)
      document.removeEventListener('mouseup', this.stopResize)
    }
  }
}

性能优化建议

拖动过程中使用requestAnimationFrame优化渲染性能:

vue实现表格拖动列宽

handleResize(e) {
  if (this.activeIndex === null) return

  window.requestAnimationFrame(() => {
    const deltaX = e.clientX - this.startX
    this.columns[this.activeIndex].width = Math.max(30, this.startWidth + deltaX)
  })
}

边界情况处理

添加最小宽度限制防止列过窄:

this.columns[this.activeIndex].width = Math.max(30, this.startWidth + deltaX)

完整组件示例

可复用的表格组件应接收columns和data作为props:

props: {
  columns: {
    type: Array,
    required: true
  },
  data: {
    type: Array,
    required: true
  }
}

注意事项

  1. 确保表头单元格设置position: relative
  2. 拖拽手柄需要绝对定位在列右侧
  3. 移除事件监听防止内存泄漏
  4. 考虑添加防抖处理高频resize事件
  5. 移动端需要额外处理touch事件

标签: 拖动表格
分享给朋友:

相关文章

vue如何实现拖动

vue如何实现拖动

Vue 实现拖动的常用方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性和相关事件实现拖动功能。 <template>…

css表格制作

css表格制作

CSS表格制作方法 使用CSS创建表格可以通过多种方式实现,以下是常见的几种方法: 使用HTML原生表格标签结合CSS样式 <table class="styled-table">…

vue实现表格滚动

vue实现表格滚动

Vue 实现表格滚动的方法 使用 CSS 固定表头并滚动内容 通过 CSS 设置表格容器的固定高度和 overflow-y: auto,实现内容区域的滚动。表头部分使用 position: stick…

vue实现全选表格

vue实现全选表格

Vue实现表格全选功能 实现表格全选功能主要分为以下几个部分: 数据绑定 在Vue组件中定义数据模型,包括表格数据和全选状态: data() { return { tableData: […

vue easyui表格实现

vue easyui表格实现

Vue 与 EasyUI 表格集成方法 Vue 本身不直接支持 EasyUI,但可通过第三方库或手动集成实现。以下是两种主流方案: 方案一:使用 vue-easyui 封装库 安装官方维护的 Vue…

vue 实现拖动效果

vue 实现拖动效果

使用 Vue 实现拖动效果 Vue 提供了多种方式实现拖动效果,可以通过原生 HTML5 的拖放 API,也可以使用第三方库如 vuedraggable。以下是两种常见的方法: 使用 HTML5 拖…