当前位置:首页 > 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实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现 创建…

vue实现接口

vue实现接口

在 Vue 中实现接口请求 Vue 本身不直接提供 HTTP 请求功能,通常需要借助第三方库如 axios 或 fetch 来实现接口请求。以下是几种常见方法: 使用 axios 发送请求 安装 a…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <templ…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,…

vue 菜单实现

vue 菜单实现

Vue 菜单实现方法 在Vue中实现菜单功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-for动态生成菜单 通过数据驱动的方式动态渲染菜单项,适合菜单内容可能变化的场景: <t…

vue 实现图钉

vue 实现图钉

实现图钉效果的步骤 在Vue中实现图钉(固定位置)效果,可以通过CSS的position: sticky属性或监听滚动事件动态修改元素样式。以下是两种常见方法: 方法一:使用CSS的positi…