Vue3初学之商品的增,删,改功能

用一个商品的后台管理进行增,删,改的实现。

案例进行学习:

typescript 复制代码
<template>
  <div>
    <el-button type="primary" @click="handleNew">新增商品</el-button>
    <el-table :data="goodsList" style="width: 100%">
      <el-table-column prop="id" label="商品ID" />
      <el-table-column prop="name" label="商品名称" />
      <el-table-column prop="price" label="价格" />
      <el-table-column prop="stock" label="库存" />
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{row}">
          <el-button type="text" size="small" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑商品弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="form" label-width="100px">
        <el-form-item label="商品名称">
          <el-input v-model="form.name" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input-number v-model="form.price" placeholder="请输入价格" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="库存">
          <el-input-number v-model="form.stock" placeholder="请输入库存" :min="0"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessageBox } from 'element-plus';

const goodsList = ref([
  { id: 1, name: '商品1', price: 100, stock: 50 },
  { id: 2, name: '商品2', price: 200, stock: 30 }
]);

const dialogVisible = ref(false);
const dialogTitle = ref('');
const form = ref({ name: '', price: 0, stock: 0 });

// 新增商品
const handleNew = () => {
  dialogTitle.value = '新增商品';
  form.value = { name: '', price: 0, stock: 0 };
  dialogVisible.value = true;
};

// 编辑商品
const handleEdit = (row) => {
  dialogTitle.value = '编辑商品';
  form.value = { ...row };
  dialogVisible.value = true;
};

// 删除商品
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该商品吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const index = goodsList.value.findIndex(item => item.id === row.id);
    goodsList.value.splice(index, 1);
  });
};

// 提交表单
const handleSubmit = () => {
  if (dialogTitle.value === '新增商品') {
    const newId = goodsList.value.length + 1;
    goodsList.value.push({ ...form.value, id: newId });
  } else {
    const index = goodsList.value.findIndex(item => item.id === form.value.id);
    goodsList.value[index] = { ...form.value };
  }
  dialogVisible.value = false;
};
</script>

效果:


相关推荐
Auc248 分钟前
OJ判题系统第6期之判题逻辑开发——设计思路、实现步骤、代码实现(策略模式)
java·开发语言·docker·容器·策略模式
向日葵xyz14 分钟前
Qt5与现代OpenGL学习(十一)OpenGL Widget鼠标控制直线旋转
开发语言·qt·学习
智慧地球(AI·Earth)17 分钟前
OpenAI for Countries:全球AI基础设施的“技术基建革命”
开发语言·人工智能·php
不学无术の码农21 分钟前
《Effective Python》第1章 Pythonic 思维总结——编写优雅、高效的 Python 代码
开发语言·python
肥肥呀呀呀1 小时前
在Flutter上如何实现按钮的拖拽效果
前端·javascript·flutter
双叶8361 小时前
(C语言)超市管理系统(测试版)(指针)(数据结构)(二进制文件读写)
c语言·开发语言·数据结构·c++
PXM的算法星球1 小时前
使用CAS操作实现乐观锁的完整指南
开发语言
TDengine (老段)1 小时前
基于 TSBS 标准数据集下 TimescaleDB、InfluxDB 与 TDengine 性能对比测试报告
java·大数据·开发语言·数据库·时序数据库·tdengine·iotdb
付朝鲜2 小时前
用自写的jQuery库+Ajax实现了省市联动
java·前端·javascript·ajax·jquery
coderYYY2 小时前
多个el-form-item两列布局排齐且el-select/el-input组件宽度撑满
前端·javascript·vue.js·elementui·前端框架