vue elementui 在table里使用el-switch

javascript 复制代码
        <el-table-column
          prop="operationStatus"
          label="状态"
          header-align="center"
          align="center"
        >
          <template slot-scope="scope">
            <el-switch active-value="ENABLE" inactive-value="DISABLE" v-model="scope.row.operationStatus" @change="handleAdStatusChange(scope.row, scope.$index)"></el-switch>
          </template>
        </el-table-column>

    //理论上底部的js并不重要 只是我记录用法
    // 处理广告状态变化
    handleAdStatusChange(row, idx) {
      this.handleStatusChange(row, idx, adupdatestatus, 'adId', 'adIds');
    },

    handleStatusChange(row, idx, updateFunction, idKey, statusKey) {
      let newStatus = row.operationStatus;
      let oldStatus = row.operationStatus === 'ENABLE' ? 'DISABLE' : 'ENABLE';
      let otxt = idKey == 'adId' ? '广告' : '广告组'
      
      // 复制菜单列表,避免直接修改原始数据
      let oarr = JSON.parse(JSON.stringify(this.menuList));
      
      // 收集要更新的ID
      let oids = [row[idKey]]; // 使用传入的idKey来获取正确的ID
      
      // 构造参数对象
      let params = {
        advertiserId: row.advertiserId,
        [statusKey]: oids, // 使用传入的statusKey来确定使用adgroupIds还是adIds
        operationStatus: newStatus,
      };
      
      // 调用更新函数并处理结果
      updateFunction(params).then((response) => {
        if (response.data.code == 0) {
          this.$message({
            message: '更新'+ otxt +'状态成功',
            type: 'success'
          });
          oarr.records[idx].operationStatus = newStatus;
          this.menuList = oarr;
        }
      }).catch((error) => {
        oarr.records[idx].operationStatus = oldStatus;
        this.menuList = oarr;
        this.$message({
          message: '更新状态失败,请重试',
          type: 'error'
        });
      });
    },
相关推荐
风之舞_yjf33 分钟前
Vue基础(14)_列表过滤、列表排序
前端·javascript·vue.js
BillKu1 小时前
scss(sass)中 & 的使用说明
前端·sass·scss
疯狂的沙粒1 小时前
uni-app 项目支持 vue 3.0 详解及版本升级方案?
前端·vue.js·uni-app
Jiaberrr1 小时前
uniapp Vue2 获取电量的独家方法:绕过官方插件限制
前端·javascript·uni-app·plus·电量
Lhuu(重开版2 小时前
Vue:Ajax
vue.js·ajax·okhttp
谢尔登2 小时前
【React】React 18 并发特性
前端·react.js·前端框架
Joker`s smile2 小时前
使用React+ant Table 实现 表格无限循环滚动播放
前端·javascript·react.js
国家不保护废物2 小时前
🌟 React 魔法学院入学指南:从零构建你的第一个魔法阵(项目)!
前端·react.js·架构
import_random2 小时前
[机器学习]svm支持向量机(优势在哪里)
前端
国家不保护废物2 小时前
从刀耕火种到现代框架:DOM编程 vs Vue/React 进化史
前端·vue.js·react.js