当前位置:首页 > VUE

vue实现表格添加删除

2026-01-22 12:12:01VUE

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

数据绑定与表格渲染

在 Vue 中实现表格功能,需先定义表格数据数组和表单输入绑定的数据对象。通过 v-for 指令循环渲染表格行,并使用 v-model 绑定表单输入。

vue实现表格添加删除

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>姓名</th>
          <th>年龄</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in tableData" :key="index">
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
          <td>
            <button @click="deleteRow(index)">删除</button>
          </td>
        </tr>
      </tbody>
    </table>

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

添加数据方法

定义 addRow 方法,将表单输入的新数据添加到表格数据数组中。添加前可进行简单校验,防止空数据。

vue实现表格添加删除

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 25 }
      ],
      newItem: { name: '', age: '' }
    }
  },
  methods: {
    addRow() {
      if (!this.newItem.name || !this.newItem.age) return
      this.tableData.push({ ...this.newItem })
      this.newItem = { name: '', age: '' } // 清空表单
    }
  }
}
</script>

删除数据方法

通过 deleteRow 方法,根据行索引从表格数据数组中移除对应项。使用 splice 方法确保响应式更新。

methods: {
  deleteRow(index) {
    this.tableData.splice(index, 1)
  }
}

完整组件示例

整合上述代码,形成完整组件。包含表格渲染、添加和删除功能,并处理基础输入校验。

<template>
  <div>
    <table border="1">
      <thead>
        <tr>
          <th>姓名</th>
          <th>年龄</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in tableData" :key="index">
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
          <td>
            <button @click="deleteRow(index)">删除</button>
          </td>
        </tr>
      </tbody>
    </table>

    <div style="margin-top: 20px">
      <input v-model="newItem.name" placeholder="姓名" />
      <input v-model="newItem.age" placeholder="年龄" type="number" />
      <button @click="addRow">添加</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 25 }
      ],
      newItem: { name: '', age: '' }
    }
  },
  methods: {
    addRow() {
      if (!this.newItem.name || !this.newItem.age) {
        alert('请填写完整信息')
        return
      }
      this.tableData.push({ ...this.newItem })
      this.newItem = { name: '', age: '' }
    },
    deleteRow(index) {
      if (confirm('确定删除吗?')) {
        this.tableData.splice(index, 1)
      }
    }
  }
}
</script>

关键点说明

  • 响应式数据:Vue 自动跟踪 tableData 的变化,更新 DOM。
  • 列表渲染v-for 需配合 :key 提升性能,通常使用唯一标识而非索引。
  • 方法逻辑:添加时需处理对象引用,避免直接修改 newItem 导致后续数据联动。
  • 用户交互:删除前通过 confirm 确认,提升用户体验。

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

相关文章

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…

vue网页实现

vue网页实现

Vue 网页实现指南 环境准备 安装 Node.js 和 npm/yarn,确保环境配置正确。使用 Vue CLI 或 Vite 创建项目框架,快速搭建开发环境。 npm install -g @v…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind(或简写为 :)动态绑定图片的…

实现vue vm

实现vue vm

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