当前位置:首页 > 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:

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

注意事项

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

vue实现表格拖动列宽

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

相关文章

vue日历表格实现

vue日历表格实现

实现Vue日历表格的基本步骤 使用Vue实现日历表格需要处理日期数据生成、渲染和交互逻辑。以下是核心实现方法: 安装依赖(可选) 若需复杂功能可引入date-fns或dayjs等日期库: np…

vue 实现拖动

vue 实现拖动

实现拖动的核心方法 在Vue中实现拖动功能可以通过HTML5的拖放API或第三方库如vuedraggable。以下是两种常见方案的实现细节。 使用HTML5原生拖放API HTML5提供了dragg…

css制作表格

css制作表格

CSS 制作表格的方法 使用 CSS 可以灵活地创建和样式化表格,以下是几种常见的方法: 使用 HTML 表格标签结合 CSS 样式 通过 HTML 的 <table>、<tr&…

使用vue实现表格

使用vue实现表格

使用 Vue 实现表格 Vue.js 提供了灵活的方式来实现表格功能,可以通过组合 v-for 指令和动态数据绑定快速构建表格。以下是几种常见的实现方法: 基础表格实现 通过 v-for 遍历数组数…

vue 实现跨行表格

vue 实现跨行表格

实现跨行表格的方法 在Vue中实现跨行表格可以通过多种方式完成,以下是几种常见的方法: 使用HTML原生表格 HTML原生表格支持rowspan属性,可以直接在Vue模板中使用: <tabl…

jquery拖动

jquery拖动

jQuery 拖动实现方法 使用 jQuery 实现拖动功能可以通过原生 HTML5 的拖放 API 或借助 jQuery UI 的拖拽组件。以下是两种常见的方法: 使用 jQuery UI 的…