当前位置:首页 > 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 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定一个变量,可以实现单选效果。当用户选择不同的选…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…

vue实现拖拉

vue实现拖拉

实现拖拽功能的基本步骤 在Vue中实现拖拽功能可以通过原生HTML5的拖放API或第三方库如vuedraggable完成。以下是两种方法的详细说明。 使用HTML5原生拖放API HTML5提供了原…

vue实现插件

vue实现插件

Vue 插件实现方法 Vue 插件通常用于为 Vue 应用添加全局功能或共享功能。以下是实现 Vue 插件的核心步骤: 插件基本结构 Vue 插件需要暴露一个 install 方法,该方法接收 Vu…

实现vue vm

实现vue vm

Vue VM 实现方法 Vue VM(ViewModel)是 Vue.js 的核心概念之一,负责连接视图(View)和数据模型(Model)。以下是实现 Vue VM 的几种方法: 使用 Vue 实…

vue路由实现滑动

vue路由实现滑动

实现 Vue 路由滑动效果的方法 使用 Vue Router 结合 CSS 过渡动画 在 Vue Router 的路由视图组件 <router-view> 上添加过渡效果,结合 CSS 实…