当前位置:首页 > 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 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <template&g…

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(如…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

django vue实现

django vue实现

Django与Vue.js整合实现方案 Django作为后端框架与Vue.js作为前端框架的整合,可以通过以下方式实现: 分离式开发 前后端完全分离,Django仅提供API接口,Vue.js通过a…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

vue实现mouseout

vue实现mouseout

Vue 实现 mouseout 事件的方法 在 Vue 中实现 mouseout 事件可以通过多种方式完成,以下是几种常见的方法: 使用 v-on 指令 Vue 提供了 v-on 指令(或简写为 @…