当前位置:首页 > VUE

vue实现可变表格

2026-02-17 20:48:01VUE

Vue实现可变表格的方法

可变表格通常指能够动态增删行列、编辑内容的表格。以下是几种常见的实现方式:

使用v-for动态渲染表格

通过v-for指令结合数组数据动态生成表格行列,配合v-model实现双向绑定:

vue实现可变表格

<template>
  <table>
    <tr v-for="(row, rowIndex) in tableData" :key="rowIndex">
      <td v-for="(cell, colIndex) in row" :key="colIndex">
        <input v-model="tableData[rowIndex][colIndex]" />
      </td>
      <td><button @click="removeRow(rowIndex)">删除行</button></td>
    </tr>
    <button @click="addRow">添加行</button>
    <button @click="addColumn">添加列</button>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        ['', '', ''],
        ['', '', '']
      ]
    }
  },
  methods: {
    addRow() {
      this.tableData.push(new Array(this.tableData[0].length).fill(''))
    },
    addColumn() {
      this.tableData.forEach(row => row.push(''))
    },
    removeRow(index) {
      this.tableData.splice(index, 1)
    }
  }
}
</script>

使用第三方表格组件

Element UI的el-table组件提供更丰富的功能:

<template>
  <el-table :data="tableData" border>
    <el-table-column
      v-for="(col, index) in columns"
      :key="index"
      :prop="col.prop"
      :label="col.label">
      <template #default="scope">
        <el-input v-model="scope.row[col.prop]" />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button @click="removeRow(scope.$index)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-button @click="addRow">添加行</el-button>
  <el-button @click="addColumn">添加列</el-button>
</template>

<script>
export default {
  data() {
    return {
      columns: [
        { prop: 'name', label: '姓名' },
        { prop: 'age', label: '年龄' }
      ],
      tableData: [
        { name: '', age: '' },
        { name: '', age: '' }
      ]
    }
  },
  methods: {
    addRow() {
      this.tableData.push({ name: '', age: '' })
    },
    addColumn() {
      const prop = 'column' + (this.columns.length + 1)
      this.columns.push({ prop, label: '新列' })
      this.tableData.forEach(row => row[prop] = '')
    },
    removeRow(index) {
      this.tableData.splice(index, 1)
    }
  }
}
</script>

实现单元格合并

通过计算属性实现动态合并单元格:

vue实现可变表格

computed: {
  spanMethod({ row, column, rowIndex, columnIndex }) {
    if (columnIndex === 0) {
      if (rowIndex % 2 === 0) {
        return { rowspan: 2, colspan: 1 }
      } else {
        return { rowspan: 0, colspan: 0 }
      }
    }
  }
}

实现拖拽排序

使用sortablejs实现行列拖拽排序:

import Sortable from 'sortablejs'

mounted() {
  const table = document.querySelector('.el-table__body-wrapper tbody')
  Sortable.create(table, {
    animation: 150,
    onEnd: ({ newIndex, oldIndex }) => {
      const movedItem = this.tableData.splice(oldIndex, 1)[0]
      this.tableData.splice(newIndex, 0, movedItem)
    }
  })
}

性能优化建议

对于大数据量表格,建议使用虚拟滚动技术。Element UI的el-table已内置虚拟滚动,只需设置height属性:

<el-table :data="bigData" height="500px">
  <!-- 列定义 -->
</el-table>

以上方法可根据实际需求组合使用,实现各种复杂的可变表格功能。

标签: 表格vue
分享给朋友:

相关文章

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式…

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-if…

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在V…

vue实现triger

vue实现triger

在Vue中实现触发器(trigger)功能通常涉及自定义事件或DOM事件触发。以下是几种常见场景的实现方法: 自定义事件触发 通过$emit方法触发父组件中监听的自定义事件: // 子组件 t…

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…