当前位置:首页 > VUE

vue实现表格增删改

2026-01-20 12:48:46VUE

实现表格增删改功能

在Vue中实现表格的增删改功能,可以通过以下方式完成:

数据绑定与渲染 使用v-for指令遍历数据数组,动态渲染表格内容。示例代码:

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

新增数据 通过表单收集新数据,使用数组的push方法添加:

methods: {
  addItem() {
    this.tableData.push({
      name: this.newName,
      age: this.newAge
    });
    this.newName = '';
    this.newAge = '';
  }
}

删除数据 使用数组的splice方法根据索引删除:

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

编辑数据 设置编辑状态和临时数据存储:

data() {
  return {
    editingIndex: -1,
    tempItem: {}
  }
},
methods: {
  editItem(index) {
    this.editingIndex = index;
    this.tempItem = {...this.tableData[index]};
  },
  saveEdit() {
    Object.assign(this.tableData[this.editingIndex], this.tempItem);
    this.cancelEdit();
  },
  cancelEdit() {
    this.editingIndex = -1;
    this.tempItem = {};
  }
}

表单绑定 使用v-model实现双向绑定:

<input v-model="tempItem.name" v-if="editingIndex === index">
<input v-model="tempItem.age" v-if="editingIndex === index">
<button @click="saveEdit" v-if="editingIndex === index">保存</button>
<button @click="cancelEdit" v-if="editingIndex === index">取消</button>

完整组件示例

将上述代码整合为完整组件:

<template>
  <div>
    <table>
      <tr v-for="(item, index) in tableData" :key="index">
        <td>
          <span v-if="editingIndex !== index">{{ item.name }}</span>
          <input v-model="tempItem.name" v-else>
        </td>
        <td>
          <span v-if="editingIndex !== index">{{ item.age }}</span>
          <input v-model="tempItem.age" v-else>
        </td>
        <td>
          <button @click="editItem(index)" v-if="editingIndex !== index">编辑</button>
          <button @click="saveEdit" v-else>保存</button>
          <button @click="cancelEdit" v-if="editingIndex === index">取消</button>
          <button @click="deleteItem(index)">删除</button>
        </td>
      </tr>
    </table>
    <div>
      <input v-model="newName" placeholder="姓名">
      <input v-model="newAge" placeholder="年龄">
      <button @click="addItem">添加</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 25 }
      ],
      newName: '',
      newAge: '',
      editingIndex: -1,
      tempItem: {}
    }
  },
  methods: {
    addItem() {
      this.tableData.push({
        name: this.newName,
        age: this.newAge
      });
      this.newName = '';
      this.newAge = '';
    },
    deleteItem(index) {
      this.tableData.splice(index, 1);
    },
    editItem(index) {
      this.editingIndex = index;
      this.tempItem = {...this.tableData[index]};
    },
    saveEdit() {
      Object.assign(this.tableData[this.editingIndex], this.tempItem);
      this.cancelEdit();
    },
    cancelEdit() {
      this.editingIndex = -1;
      this.tempItem = {};
    }
  }
}
</script>

使用第三方组件库

如需更强大的表格功能,可以考虑使用Element UI或Ant Design Vue等组件库:

Element UI示例

<template>
  <el-table :data="tableData">
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="age" label="年龄"></el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button @click="editItem(scope.row)">编辑</el-button>
        <el-button @click="deleteItem(scope.$index)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

Ant Design Vue示例

vue实现表格增删改

<template>
  <a-table :columns="columns" :dataSource="tableData">
    <template #action="{ record, index }">
      <a-button @click="editItem(record)">编辑</a-button>
      <a-button @click="deleteItem(index)">删除</a-button>
    </template>
  </a-table>
</template>

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

相关文章

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template>…

vue实现登出

vue实现登出

实现Vue登出功能 登出功能通常涉及清除用户会话、令牌或本地存储的数据,并重定向到登录页面。以下是几种常见的实现方式: 清除用户令牌和状态 在Vuex的store中定义一个logout mutati…

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <i…

vue实现波形

vue实现波形

实现波形效果的方法 在Vue中实现波形效果可以通过多种方式完成,常见的方法包括使用Canvas绘制、CSS动画或第三方库。以下是几种实现方案: 使用Canvas绘制波形 通过Canvas API动态…

vue实现接口

vue实现接口

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

vue 简单实现

vue 简单实现

以下是 Vue 的简单实现示例,涵盖基础模板、数据绑定、方法及生命周期: 基础模板与数据绑定 <div id="app"> <p>{{ message }}</p&…