Vue2.0+element-ui实现表格的增删查改

vue2做了个表格的demo,有增删改查的功能,记录一下,喜欢就点个赞收藏一下吧~

效果:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码:

1.主文件list-page.vue 列表页

<template>
  <div>
    <div class="btn-box">
      <el-button type="primary" @click="add">添加</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="180"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="edit(scope.row, scope.$index)"
            >修改</el-button
          >
          <el-button type="text" @click="del(scope.row, scope.$index)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <operation-dialog ref="operaDialog" />
  </div>
</template>

<script>
import OperationDialog from "./operation-dialog.vue";
export default {
  name: "ListPage",
  components: { OperationDialog },
  data() {
    return {
      tableData: [],
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    getTableData() {
      // 模拟请求回来的数据 实际操作中这里应该调接口发请求获取数据
      this.tableData = [
        {
          id: 1,
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          id: 2,
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          id: 3,
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          id: 4,
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ];
    },
    // 添加弹窗打开
    add() {
      this.$refs.operaDialog.open();
    },
    // 添加弹窗打开
    edit(row, index) {
      this.$refs.operaDialog.open(row, index);
    },
    // 删除
    del(row, index) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 这两句模拟接口删除返回的结果
          this.tableData.splice(index, 1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          //   // 实际操作调接口请求删除当前选中数据
          //   delRow(row.id).then((res) => {
          //     if (res.code === 200) {
          //       this.$message({
          //         type: "success",
          //         message: res.msg,
          //       });
          //     }
          //   });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

2.弹窗页面(新增/编辑公用一个弹窗页面)

<template>
  <el-dialog
    v-if="visible"
    :title="form.id?'修改':'新增'"
    :visible.sync="visible"
    modal
    :close-on-click-modal="false"
    width="30%"
    :before-close="close"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      label-width="100px"
      class="form"
    >
      <el-form-item label="日期" prop="date">
        <el-date-picker value-format="yyyy-MM-dd" format="yyyy-MM-dd" v-model="form.date" type="date" placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="form.address" placeholder="请输入地址"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      form: {
        date:'',
        name:'',
        address:''
      },
      rules: {
        date: [
          { required: true, message: "请选择日期", trigger: "change" },
        ],
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        address: [{ required: true, message: "请输入地址", trigger: "blur" }],
      },
    };
  },
  methods: {
    open(row, index) {
      this.visible = true;
      //   有id则是编辑 否则则是新增
      if (row && row.id) {
        this.form = JSON.parse(JSON.stringify(row));
        // 因为要模拟修改 此处需要索引 实际项目操作不需要index
        this.form.index = index;
      }
    },
    close() {
      this.visible = false;
      this.form = {};
      // 移除校验结果并重置数据
      this.$refs.form.resetFields()
    },
    confirm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            // 编辑的提交
            // 调用修改接口 并在接口返回成功后调用父组件的getList()方法
            // editApi(this.form).then(res=>{if(res.code===200) {this.$message.sucess(res.msg);this.$parent.getList();this.close()}})

            // 模拟修改-(以下四句实际操作不需要 逻辑处理在接口返回 参考上面的代码)
            let cloneForm=JSON.parse(JSON.stringify(this.form))
            this.$parent.tableData.splice(this.form.index, 1, cloneForm);
            this.close();
          } else {
            // 新增的提交
            // 调用新增接口 并在接口返回成功后调用父组件的getList()方法
            // addApi(this.form).then(res=>{if(res.code===200) {this.$message.sucess(res.msg);this.$parent.getList();this.close()}})

            // 模拟添加-(以下四句实际操作不需要 逻辑处理在接口返回 参考上面的代码)
            this.form.id=this.$parent.tableData.length+1
            let cloneForm=JSON.parse(JSON.stringify(this.form))
            this.$parent.tableData.push(cloneForm);
            this.close();
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
引用。对于增加和修改表单,可以使用`useForm`函数来处理。该函数接收一个包含URL、转换函数和请求方式的对象,然后返回一个表单对象和一个提交函数。提交函数可以根据请求方式调用相应的API进行表单的提交。具体的使用方法可以参考上面的代码示例。关于进阶的API管理,可以将API地址集中管理,使用一个独立的文件来管理API列表。可以创建一个`Api`对象,将每个API的名称和对应的URL进行映射。这样在其他地方使用API时,只需通过`Api`对象来引用相应的URL即可。例如,`Api.xxx`将返回`/api/xxx`。这种方式可以更好地管理和维护API地址。以上是实现增删查改的一些基本方法和技巧,希望对你有所帮助。123 #### 引用[.reference_title] - *1* [vue3 增删改查](https://blog.csdn.net/ZDM_9999/article/details/128477766)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *2* *3* [vue3 组合函数中优雅的复用增删改查](https://blog.csdn.net/qq_36990263/article/details/128787750)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值