当前位置:首页 > VUE

vue实现输入表格

2026-01-19 19:07:16VUE

Vue 实现输入表格的方法

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

动态绑定表格数据

使用 v-for 动态渲染表格行,并通过 v-model 绑定输入框的值到 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 实现评论编辑功能 数据绑定与表单结构 使用 v-model 双向绑定评论内容到表单输入框,确保编辑时能显示原内容并同步修改。示例模板结构: <template> <di…

vue实现条件判断

vue实现条件判断

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

vue设计与实现目录

vue设计与实现目录

以下是《Vue设计与实现》一书的典型目录结构(根据常见技术书籍及Vue核心内容整理): 响应式系统 响应式数据的基本原理 依赖收集与触发更新 实现简易响应式系统 Proxy与Refle…

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template>…

vue中实现显示和隐藏

vue中实现显示和隐藏

控制显示与隐藏的方法 在Vue中实现元素的显示和隐藏可以通过多种方式完成,以下是常见的几种方法: 1. 使用v-if指令v-if根据条件动态添加或移除DOM元素,适合需要频繁切换的场景。当条件为f…

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…