当前位置:首页 > 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方法根据索引删除:

vue实现表格增删改

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>

完整组件示例

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

vue实现表格增删改

<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 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template…

vue 移动端实现签名

vue 移动端实现签名

Vue 移动端签名实现方法 在移动端实现签名功能,可以通过HTML5的Canvas结合触摸事件完成。以下是具体实现步骤: 使用Canvas绘制签名 安装vue-signature-pad插件或直接使…

vue实现按卡片轮播

vue实现按卡片轮播

实现卡片轮播的基本思路 在Vue中实现卡片轮播可以通过结合v-for指令和动态样式绑定完成。核心是维护一个当前显示卡片的索引,通过CSS过渡效果实现平滑切换。 基础实现步骤 模板部分 使用v-for…

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const BaseC…

vue手写签名如何实现

vue手写签名如何实现

实现手写签名的基本思路 在Vue中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。…

vue实现生成二维码

vue实现生成二维码

使用qrcode.vue库生成二维码 安装qrcode.vue库: npm install qrcode.vue --save 在Vue组件中使用: <template> <…