当前位置:首页 > VUE

vue表格实现增删改

2026-02-23 13:35:49VUE

Vue表格实现增删改

基础表格结构

使用el-table组件构建基础表格,绑定数据源并定义列:

<template>
  <el-table :data="tableData" style="width: 100%">
    <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="handleEdit(scope.row)">编辑</el-button>
        <el-button @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

数据初始化

data中定义表格数据和临时表单对象:

data() {
  return {
    tableData: [
      { id: 1, name: '张三', age: 25 },
      { id: 2, name: '李四', age: 30 }
    ],
    form: {
      id: '',
      name: '',
      age: ''
    },
    dialogVisible: false
  }
}

新增功能实现

添加新增按钮和对话框表单:

<el-button @click="handleAdd">新增</el-button>
<el-dialog v-model="dialogVisible" title="表单">
  <el-form :model="form">
    <el-form-item label="姓名">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="年龄">
      <el-input v-model="form.age"></el-input>
    </el-form-item>
  </el-form>
  <template #footer>
    <el-button @click="dialogVisible = false">取消</el-button>
    <el-button @click="submitForm">确定</el-button>
  </template>
</el-dialog>

新增方法逻辑:

methods: {
  handleAdd() {
    this.form = { id: '', name: '', age: '' }
    this.dialogVisible = true
  },
  submitForm() {
    if (this.form.id) {
      // 更新逻辑
    } else {
      this.form.id = Date.now()
      this.tableData.push({...this.form})
    }
    this.dialogVisible = false
  }
}

编辑功能实现

编辑方法逻辑:

handleEdit(row) {
  this.form = { ...row }
  this.dialogVisible = true
}

更新submitForm方法中的更新逻辑:

if (this.form.id) {
  const index = this.tableData.findIndex(item => item.id === this.form.id)
  this.tableData.splice(index, 1, {...this.form})
}

删除功能实现

删除方法逻辑:

handleDelete(row) {
  this.$confirm('确认删除?', '提示').then(() => {
    const index = this.tableData.findIndex(item => item.id === row.id)
    this.tableData.splice(index, 1)
  })
}

完整组件示例

<template>
  <div>
    <el-button @click="handleAdd">新增</el-button>
    <el-table :data="tableData">
      <!-- 表格列定义 -->
    </el-table>

    <!-- 对话框表单 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      form: {},
      dialogVisible: false
    }
  },
  methods: {
    // 所有方法实现
  }
}
</script>

关键注意事项

  • 使用v-model实现表单双向绑定
  • 操作数组时注意Vue的响应式规则,使用可触发视图更新的方法
  • 对于复杂场景可考虑使用Vuex管理状态
  • 实际项目中建议将表单验证逻辑加入

以上实现使用了Element UI组件库,如需使用其他UI库或原生实现,原理相同但具体API会有差异。

vue表格实现增删改

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

相关文章

vue实现点击页面切换

vue实现点击页面切换

实现点击页面切换功能 在Vue中实现点击页面切换功能,可以通过路由跳转或组件动态渲染两种方式完成。以下是具体实现方法: 使用Vue Router实现页面跳转 安装Vue Router后,在项目中…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm ins…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templat…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现gps

vue实现gps

Vue 中实现 GPS 定位功能 在 Vue 中实现 GPS 定位功能通常依赖于浏览器的 Geolocation API 或第三方地图服务(如高德、百度地图等)。以下是两种常见的实现方式: 使用浏览…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户…