解决多选删除页面不同步问题

多选删除一般有两种情况:

1,删除接口支持传多个id,这是最理想的方法,建议大家积极与后端进行沟通解决。之后只需要判断接口回调刷新页面即可!

2,删除接口不支持传多个id,这就是接下来我们要处理的情况,利用promise.all来解决,亲测有效,接下来尽量用最通俗的语言和代码为大家解释!

复制代码
if (this.selectRows.length > 0) {        //以下为多选方法*******
          const ids = this.selectRows.map((item) => item.id);//取出所有需要删除的id*****
          this.$confirm("你确定要删除选中项吗", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            center: true,
          })
            .then(() => {
              var promiseList = new Set();//生成一个唯一的集合******
              ids.forEach(async (item) => {//循环操作添加promise*****
                let promise = function () {
                  return new Promise(async (resolve) => {
                    let res = await crmCustomerDel(item);//crmCustomerDel为删除api ******
                    if (res.code == 200) {
                      resolve();
                    }
                  });
                };
                promiseList.add(promise());
              });
              Promise.all(promiseList)
                .then(async (result) => {
                  this.getInfo();//为页面初始化方法******
                  console.log(result);
                })
                .catch((error) => {
                  console.log(error);
                });
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            })
            .catch(() => {
              this.$message({
                type: "error",
                message: "删除失败",
              });
            });

之后大家复制这段代码,更改为自己的删除api即可,最后希望帮到你,回来帮我点个赞!!!

相关推荐
MageGojo9 分钟前
做节日活动页时,如何用 API 快速生成对联内容
javascript·python·节日·对联生成
达达爱吃肉14 分钟前
claude 接入deepseek 运行报错
java·服务器·前端
jingling55520 分钟前
Flutter | Dio网络请求实战
android·开发语言·前端·flutter
向上的车轮24 分钟前
Next.js 入门指南:从零到一构建全栈应用
开发语言·javascript·ecmascript
freeinlife'26 分钟前
精准秒表计时器实现---基于js
开发语言·前端·javascript
MaCa .BaKa36 分钟前
55-宠物爱心救助领养系统-宠物救助领养系统
java·vue.js·tomcat·maven·springboot·宠物救助领养系统
王文?问37 分钟前
ESP32-S3 实战教程:本地语音识别控制 Web 塔防游戏,从固件到前端完整跑通
前端·游戏·语音识别
Hoshizola1 小时前
uniapp与蓝牙设备连接详细步骤
前端·uni-app
优雅格子衫1 小时前
uniapp 拍照相册选取后超级好用的裁剪组件,增加水印完全自定义
开发语言·前端·javascript·uni-app·vue
Dxy12393102161 小时前
HTML如何写鼠标事件
前端·html·计算机外设