当前位置:首页 > 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示例

<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实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <d…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现关键词轮播

vue实现关键词轮播

Vue 实现关键词轮播 基础实现方案 使用 Vue 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keywor…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue 实现

vue 实现

Vue 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…