当前位置:首页 > VUE

vue实现输入表格

2026-01-19 19:07:16VUE

Vue 实现输入表格的方法

在 Vue 中实现输入表格可以通过多种方式完成,常见的是结合 v-for 指令和双向数据绑定 v-model。以下是几种实现方法:

vue实现输入表格

动态绑定表格数据

使用 v-for 动态渲染表格行,并通过 v-model 绑定输入框的值到 Vue 的数据对象。

vue实现输入表格

<template>
  <table>
    <thead>
      <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, index) in tableData" :key="index">
        <td><input v-model="row.name" type="text" /></td>
        <td><input v-model="row.age" type="number" /></td>
        <td><button @click="removeRow(index)">删除</button></td>
      </tr>
    </tbody>
  </table>
  <button @click="addRow">添加行</button>
</template>

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

使用组件化表格

将表格行封装为单独的组件,便于复用和管理逻辑。

<template>
  <table>
    <thead>
      <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <TableRow
        v-for="(row, index) in tableData"
        :key="index"
        :row="row"
        @remove="removeRow(index)"
      />
    </tbody>
  </table>
  <button @click="addRow">添加行</button>
</template>

<script>
import TableRow from './TableRow.vue'

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

使用第三方表格库

对于复杂需求,可以集成第三方表格库如 Element UIVxeTable

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="name" label="姓名">
      <template #default="{ row }">
        <el-input v-model="row.name" />
      </template>
    </el-table-column>
    <el-table-column prop="age" label="年龄">
      <template #default="{ row }">
        <el-input v-model.number="row.age" type="number" />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ $index }">
        <el-button @click="removeRow($index)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-button @click="addRow">添加行</el-button>
</template>

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

注意事项

  • 确保 v-model 绑定的数据类型正确,如数字类型需使用 v-model.number
  • 动态添加或删除行时,避免直接修改数组引用,应使用响应式方法如 pushsplice
  • 对于大型表格,考虑性能优化,如虚拟滚动或分页加载。

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

相关文章

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @tou…

vue 实现排序

vue 实现排序

Vue 实现排序的方法 使用计算属性实现排序 通过计算属性对数组进行排序,可以保持原始数据不变。示例代码展示了如何对列表按名称升序排序: <template> <div>…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…