当前位置:首页 > VUE

vue实现表格数据录入

2026-01-21 19:44:37VUE

Vue 实现表格数据录入的方法

基础表格数据绑定

使用 v-model 绑定表格输入框数据,配合 v-for 动态渲染表格行。适合静态数据结构或简单增删场景。

<template>
  <table>
    <tr v-for="(item, index) in tableData" :key="index">
      <td><input v-model="item.name" placeholder="姓名"></td>
      <td><input v-model="item.age" type="number" placeholder="年龄"></td>
      <td><button @click="removeRow(index)">删除</button></td>
    </tr>
    <button @click="addRow">新增行</button>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [{ name: '', age: '' }]
    }
  },
  methods: {
    addRow() {
      this.tableData.push({ name: '', age: '' })
    },
    removeRow(index) {
      this.tableData.splice(index, 1)
    }
  }
}
</script>

动态表单验证

结合 Element UIAnt Design Vue 的表格组件实现带验证的数据录入。以下以 Element UI 为例:

vue实现表格数据录入

<template>
  <el-table :data="tableData" border>
    <el-table-column prop="name" label="姓名">
      <template #default="{ row }">
        <el-input 
          v-model="row.name" 
          :rules="[{ required: true, message: '必填' }]"
        ></el-input>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ $index }">
        <el-button @click="deleteRow($index)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-button @click="addNewRow">新增行</el-button>
</template>

复杂数据结构处理

对于嵌套对象或动态列的场景,可使用计算属性配合自定义组件:

export default {
  data() {
    return {
      columns: ['product', 'price', 'quantity'],
      items: [
        { product: '商品A', price: 10, quantity: 1 },
        { product: '商品B', price: 20, quantity: 2 }
      ]
    }
  },
  methods: {
    addItem() {
      this.items.push(
        this.columns.reduce((obj, col) => {
          obj[col] = col === 'quantity' ? 1 : ''
          return obj
        }, {})
      )
    }
  }
}

数据持久化方案

提交数据时进行深度校验并处理:

vue实现表格数据录入

methods: {
  submitData() {
    const invalidItems = this.tableData.filter(
      item => !item.name || !item.age
    )
    if (invalidItems.length) {
      alert('请填写完整信息')
      return
    }
    axios.post('/api/save', this.tableData).then(() => {
      alert('保存成功')
    })
  }
}

性能优化建议

对于大型数据集采用虚拟滚动方案:

<template>
  <RecycleScroller
    class="table-container"
    :items="tableData"
    :item-size="50"
    key-field="id"
  >
    <template #default="{ item }">
      <tr class="table-row">
        <td><input v-model="item.name"></td>
        <td><input v-model.number="item.value"></td>
      </tr>
    </template>
  </RecycleScroller>
</template>

关键点总结:

  • 基础实现使用 v-model + v-for
  • 生产环境建议使用 UI 库的表格组件
  • 动态列需特殊处理数据结构
  • 大数据量考虑虚拟滚动方案
  • 提交前做好数据验证和错误处理

分享给朋友:

相关文章

vue 实现表格单选

vue 实现表格单选

实现表格单选的基本思路 在Vue中实现表格单选功能,通常需要结合表格组件(如el-table)和数据绑定。核心逻辑是通过点击事件更新当前选中行的唯一标识(如id),并通过:class或:style绑定…

vue实现表格刷新

vue实现表格刷新

Vue 实现表格刷新的方法 使用 v-if 强制重新渲染 通过 v-if 控制表格的显示与隐藏,切换时 Vue 会重新渲染组件。 <template> <button @cli…

vue实现表格滚动

vue实现表格滚动

Vue 实现表格滚动的方法 使用 CSS 固定表头并滚动内容 通过 CSS 设置表格容器的固定高度和 overflow-y: auto,实现内容区域的滚动。表头部分使用 position: stick…

vue实现多维表格

vue实现多维表格

Vue 实现多维表格的方法 多维表格(如类似 Excel 的数据表)在 Vue 中可以通过组件化设计和状态管理实现。以下是几种常见方案: 使用原生表格与动态渲染 通过 v-for 嵌套循环动态渲染行…

vue实现单选表格

vue实现单选表格

实现单选表格的基本思路 在Vue中实现单选表格通常需要结合<table>元素和Vue的数据绑定特性。通过v-model或自定义事件管理选中状态,确保同一时间仅有一行被选中。 使用v-mo…

vue实现表格渲染

vue实现表格渲染

Vue实现表格渲染的方法 使用v-for指令渲染表格数据 通过v-for指令可以遍历数组或对象,动态生成表格行。数据通常存储在组件的data属性中,通过双向绑定实现动态更新。 <templat…