当前位置:首页 > VUE

vue表格宽度拖动实现

2026-01-22 21:08:17VUE

vue表格宽度拖动实现

在Vue中实现表格宽度拖动功能,可以通过监听鼠标事件并结合CSS样式调整来实现。以下是具体实现方法:

使用原生事件监听

创建一个可拖动的列分隔线,通过监听mousedownmousemovemouseup事件来调整列宽。

vue表格宽度拖动实现

<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>
      <tr v-for="(row, rowIndex) in data" :key="rowIndex">
        <td v-for="(col, colIndex) in columns" :key="colIndex">
          {{ row[col.key] }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      columns: [
        { title: 'Name', key: 'name', width: 200 },
        { title: 'Age', key: 'age', width: 100 },
        { title: 'Address', key: 'address', width: 300 }
      ],
      data: [
        { name: 'John', age: 25, address: 'New York' },
        { name: 'Jane', age: 30, address: 'London' }
      ],
      isResizing: false,
      currentColumnIndex: null,
      startX: 0,
      startWidth: 0
    };
  },
  methods: {
    startResize(index, event) {
      this.isResizing = true;
      this.currentColumnIndex = index;
      this.startX = event.clientX;
      this.startWidth = this.columns[index].width;

      document.addEventListener('mousemove', this.handleResize);
      document.addEventListener('mouseup', this.stopResize);
    },
    handleResize(event) {
      if (!this.isResizing) return;

      const deltaX = event.clientX - this.startX;
      const newWidth = this.startWidth + deltaX;

      if (newWidth > 50) { // 最小宽度限制
        this.columns[this.currentColumnIndex].width = newWidth;
      }
    },
    stopResize() {
      this.isResizing = false;
      document.removeEventListener('mousemove', this.handleResize);
      document.removeEventListener('mouseup', this.stopResize);
    }
  }
};
</script>

<style>
table {
  border-collapse: collapse;
  width: 100%;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px;
  position: relative;
}
.resize-handle {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 5px;
  background-color: #f0f0f0;
  cursor: col-resize;
}
.resize-handle:hover {
  background-color: #ccc;
}
.header-content {
  position: relative;
  padding-right: 5px;
}
</style>

使用第三方库

如果不想手动实现,可以使用现成的Vue表格组件库,如vxe-tableelement-ui,它们内置了列宽拖动功能。

vue表格宽度拖动实现

vxe-table示例:

<template>
  <vxe-table :data="tableData">
    <vxe-column type="seq" width="60"></vxe-column>
    <vxe-column field="name" title="Name" :resizable="true"></vxe-column>
    <vxe-column field="age" title="Age" :resizable="true"></vxe-column>
  </vxe-table>
</template>

<script>
import { VxeTable, VxeColumn } from 'vxe-table';

export default {
  components: { VxeTable, VxeColumn },
  data() {
    return {
      tableData: [
        { name: 'John', age: 25 },
        { name: 'Jane', age: 30 }
      ]
    };
  }
};
</script>

element-ui示例:

<template>
  <el-table :data="tableData" border style="width: 100%">
    <el-table-column prop="name" label="Name" width="180" resizable></el-table-column>
    <el-table-column prop="age" label="Age" width="180" resizable></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: 'John', age: 25 },
        { name: 'Jane', age: 30 }
      ]
    };
  }
};
</script>

注意事项

  • 拖动过程中需要限制最小宽度,避免列宽过小。
  • 在拖动结束后移除事件监听,避免内存泄漏。
  • 如果表格内容复杂,考虑使用防抖或节流优化性能。

标签: 拖动宽度
分享给朋友:

相关文章

vue实现拖动图片验证

vue实现拖动图片验证

Vue 实现拖动图片验证功能 核心思路 拖动图片验证通常需要用户将图片拖动到指定区域或按正确顺序排列。实现时需要处理拖拽事件、验证逻辑和视觉反馈。 实现步骤 1. 基础组件结构 <tem…

vue实现表格拖动列宽

vue实现表格拖动列宽

实现表格列宽拖动的核心思路 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽逻辑,动态修改列宽。需要处理表头与表体的联动,确保拖动时视觉一致。 基础HTML结构 &…

vue如何实现拖动

vue如何实现拖动

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

jquery拖动

jquery拖动

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

js实现图片拖动

js实现图片拖动

实现图片拖动的步骤 使用JavaScript实现图片拖动功能,可以通过HTML5的拖放API结合事件监听实现。以下是具体方法: 设置HTML结构 为图片添加draggable属性,并设置唯一ID以便…

vue实现图片拖动

vue实现图片拖动

Vue 实现图片拖动的步骤 使用 HTML5 拖放 API 在 Vue 中实现图片拖动可以利用 HTML5 的拖放 API。通过 draggable 属性设置元素可拖动,监听 dragstart、dr…