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'
        });
      });
    },
相关推荐
源码获取_wx:Fegn08951 分钟前
计算机毕业设计|基于springboot + vue景区管理系统(源码+数据库+文档)
java·vue.js·spring boot·后端·课程设计
徐小夕@趣谈前端1 小时前
NO-CRM 2.0正式上线,Vue3+Echarts+NestJS实现的全栈CRM系统,用AI重新定义和实现客户管理系统
前端·javascript·人工智能·开源·编辑器·echarts
catino1 小时前
图片、文件上传
前端
Mr Xu_1 小时前
Vue3 + Element Plus 实现点击导航平滑滚动到页面指定位置
前端·javascript·vue.js
小王努力学编程1 小时前
LangChain——AI应用开发框架(核心组件1)
linux·服务器·前端·数据库·c++·人工智能·langchain
pas1362 小时前
35-mini-vue 实现组件更新功能
前端·javascript·vue.js
前端达人2 小时前
为什么聪明的工程师都在用TypeScript写AI辅助代码?
前端·javascript·人工智能·typescript·ecmascript
快乐点吧2 小时前
使用 data-属性和 CSS 属性选择器实现状态样式控制
前端·css
EndingCoder2 小时前
属性和参数装饰器
java·linux·前端·ubuntu·typescript
小二·2 小时前
Python Web 开发进阶实战:量子机器学习实验平台 —— 在 Flask + Vue 中集成 Qiskit 构建混合量子-经典 AI 应用
前端·人工智能·python