当前位置:首页 > VUE

vue实现表格拖动列宽

2026-02-09 20:29:16VUE

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

通过监听鼠标事件(mousedown、mousemove、mouseup)动态修改列宽度。需要处理表头单元格的拖拽手柄,计算鼠标移动距离并更新列宽。

基本实现步骤

创建可拖拽的表头单元格,添加拖拽手柄元素:

<template>
  <table>
    <thead>
      <tr>
        <th v-for="(col, index) in columns" :key="index">
          <div class="header-content">
            {{ col.title }}
            <div 
              class="resize-handle"
              @mousedown="startResize(index, $event)"
            ></div>
          </div>
        </th>
      </tr>
    </thead>
    <tbody>
      <!-- 表格数据行 -->
    </tbody>
  </table>
</template>

添加CSS样式确保拖拽手柄可见:

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

th {
  position: relative;
  padding-right: 15px; /* 为手柄留空间 */
}

实现拖拽逻辑的JavaScript部分:

vue实现表格拖动列宽

export default {
  data() {
    return {
      columns: [
        { title: '列1', width: 150 },
        { title: '列2', width: 200 }
      ],
      isResizing: false,
      currentColumn: null,
      startX: 0,
      startWidth: 0
    }
  },
  methods: {
    startResize(index, e) {
      this.isResizing = true
      this.currentColumn = 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.isResizing) return
      const dx = e.clientX - this.startX
      this.columns[this.currentColumn].width = this.startWidth + dx
    },
    stopResize() {
      this.isResizing = false
      document.removeEventListener('mousemove', this.handleResize)
      document.removeEventListener('mouseup', this.stopResize)
    }
  }
}

性能优化建议

使用CSS transforms代替直接修改宽度减少重排:

handleResize(e) {
  if (!this.isResizing) return
  const dx = e.clientX - this.startX
  this.$refs.table.querySelectorAll('th')[this.currentColumn].style.width = 
    `${this.startWidth + dx}px`
}

添加防抖处理避免频繁更新:

vue实现表格拖动列宽

import { debounce } from 'lodash'

methods: {
  handleResize: debounce(function(e) {
    // 处理逻辑
  }, 16) // 约60fps
}

完整组件示例

结合表格数据的完整组件实现:

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

边界情况处理

防止列宽过小:

handleResize(e) {
  if (!this.isResizing) return
  const dx = e.clientX - this.startX
  const newWidth = Math.max(30, this.startWidth + dx) // 最小30px
  this.columns[this.currentColumn].width = newWidth
}

处理表格滚动情况:

startResize(index, e) {
  // 获取表格相对于视口的位置
  const rect = this.$refs.table.getBoundingClientRect()
  this.startX = e.clientX - rect.left
  // 其他逻辑...
}

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

相关文章

css表格制作

css表格制作

CSS 表格制作基础 使用 CSS 可以创建美观且响应式的表格。以下是一些基本方法: table 元素用于定义表格,tr 定义行,td 定义单元格,th 定义表头单元格。 <table>…

css怎么制作表格

css怎么制作表格

使用HTML和CSS创建表格 HTML提供<table>标签用于创建表格结构,结合CSS可以自定义样式。以下是基本实现方法: HTML结构 <table> <th…

vue表格实现单选框

vue表格实现单选框

Vue 表格实现单选框的方法 在 Vue 中实现表格单选框功能,可以通过结合 el-table(Element UI)或原生表格与 Vue 的数据绑定特性完成。以下是几种常见实现方式: 使用 Ele…

vue实现表格复选

vue实现表格复选

实现表格复选的基本思路 在Vue中实现表格复选功能,通常需要结合v-model绑定选中状态,监听复选框变化事件,以及维护一个选中项的数组。以下是具体实现方法: 基础实现步骤 数据准备 定义表格数据和…

vue表格动态实现

vue表格动态实现

Vue 表格动态实现方法 动态表格的实现通常涉及数据绑定、列配置动态化以及交互功能的灵活处理。以下是几种常见场景的具体实现方式: 动态数据绑定 通过v-for指令循环渲染表格行数据,数据变化时表格自…

js实现图片的拖动

js实现图片的拖动

实现图片拖动的步骤 HTML 结构 在 HTML 中创建一个可拖动的图片元素,确保设置 draggable 属性为 true。 <img id="draggable-img" src="ima…