当前位置:首页 > VUE

vue实现表格拖动列宽

2026-01-07 06:04:29VUE

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

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

基础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优化渲染性能:

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:

vue实现表格拖动列宽

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或第三方库如vuedraggable。以下是两种常见方法的详细说明: 使用HTML5原生拖放API HTML5提供了dra…

css表格制作表格

css表格制作表格

CSS 表格制作方法 基础表格结构 使用HTML的<table>标签创建表格框架,搭配<tr>(行)、<td>(单元格)和<th>(表头)标签: &l…

vue动态表格实现

vue动态表格实现

Vue动态表格实现方法 使用v-for指令渲染动态数据 通过v-for指令可以循环遍历数组数据生成表格行,数据变化时表格自动更新。示例代码: <template> <table…

vue实现上传表格

vue实现上传表格

Vue实现上传表格的方法 使用Element UI上传组件 Element UI提供了el-upload组件,可以方便地实现文件上传功能。需要安装Element UI并引入相关组件。 安装Eleme…

vue实现拖拽表格

vue实现拖拽表格

Vue 实现拖拽表格的方法 使用第三方库(推荐) 推荐使用 vuedraggable 或 sortablejs 这类成熟的拖拽库,它们对 Vue 有良好支持且功能丰富。 安装 vuedraggabl…

vue实现横向表格

vue实现横向表格

横向表格的实现方法 在Vue中实现横向表格,可以通过调整CSS样式和数据结构来实现。横向表格与常规表格的区别在于数据的展示方向,通常需要将表头和数据行进行转置。 使用CSS Flex布局 通过Fle…