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

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

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即可,最后希望帮到你,回来帮我点个赞!!!

猜你喜欢

转载自blog.csdn.net/m0_71231013/article/details/134593711
今日推荐