当前位置:首页 > VUE

vue实现表格添加删除

2026-02-23 03:12:50VUE

Vue 实现表格添加与删除功能

在Vue中实现表格的添加和删除功能,通常涉及数据绑定、事件处理和动态渲染。以下是具体实现方法:

数据准备

定义表格数据数组和表单输入绑定的数据对象:

vue实现表格添加删除

data() {
  return {
    tableData: [
      { id: 1, name: '张三', age: 25 },
      { id: 2, name: '李四', age: 30 }
    ],
    form: {
      name: '',
      age: ''
    },
    nextId: 3
  }
}

添加功能实现

创建添加数据的方法,将表单数据推送到表格数组中:

methods: {
  addItem() {
    if (!this.form.name || !this.form.age) return

    this.tableData.push({
      id: this.nextId++,
      name: this.form.name,
      age: Number(this.form.age)
    })

    this.form.name = ''
    this.form.age = ''
  }
}

模板部分绑定表单和添加按钮:

vue实现表格添加删除

<div>
  <input v-model="form.name" placeholder="姓名">
  <input v-model="form.age" placeholder="年龄" type="number">
  <button @click="addItem">添加</button>
</div>

删除功能实现

创建删除方法,通过过滤或splice移除指定项:

methods: {
  deleteItem(id) {
    this.tableData = this.tableData.filter(item => item.id !== id)
    // 或使用splice:
    // const index = this.tableData.findIndex(item => item.id === id)
    // this.tableData.splice(index, 1)
  }
}

模板中为每行添加删除按钮:

<table>
  <tr v-for="item in tableData" :key="item.id">
    <td>{{ item.name }}</td>
    <td>{{ item.age }}</td>
    <td>
      <button @click="deleteItem(item.id)">删除</button>
    </td>
  </tr>
</table>

完整组件示例

<template>
  <div>
    <div>
      <input v-model="form.name" placeholder="姓名">
      <input v-model="form.age" placeholder="年龄" type="number">
      <button @click="addItem">添加</button>
    </div>

    <table>
      <tr v-for="item in tableData" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
        <td>
          <button @click="deleteItem(item.id)">删除</button>
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 }
      ],
      form: {
        name: '',
        age: ''
      },
      nextId: 3
    }
  },
  methods: {
    addItem() {
      if (!this.form.name || !this.form.age) return

      this.tableData.push({
        id: this.nextId++,
        name: this.form.name,
        age: Number(this.form.age)
      })

      this.form.name = ''
      this.form.age = ''
    },
    deleteItem(id) {
      this.tableData = this.tableData.filter(item => item.id !== id)
    }
  }
}
</script>

增强功能建议

  1. 添加表单验证,确保输入合法
  2. 使用计算属性处理排序或筛选
  3. 添加确认对话框防止误删
  4. 对于大型数据集考虑虚拟滚动优化性能

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

相关文章

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…

vue 实现协同

vue 实现协同

Vue 实现协同编辑的方案 协同编辑指多个用户同时编辑同一文档并实时同步更改。Vue 结合相关库和技术可轻松实现该功能。 使用 WebSocket 实现实时通信 WebSocket 提供全双工通信,…

vue实现watch

vue实现watch

监听数据变化 在Vue中,watch用于监听响应式数据的变化并执行回调函数。基本语法如下: watch: { propertyName(newValue, oldValue) { //…

vue实现前端跳转

vue实现前端跳转

Vue 实现前端跳转的方法 在 Vue 中实现前端跳转通常使用 Vue Router,以下是几种常见的跳转方式: 声明式导航(模板中使用 <router-link>) 在模板中直接使用…