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'
        });
      });
    },
相关推荐
汉堡大王95279 分钟前
Vue 3 + TS + Element Plus 实战:如何从零搭建企业级违章记录管理 SaaS 前端
前端·javascript·vue.js
minimoon_jojo11 分钟前
Ant Design 树形表格渲染原理
前端
paopaokaka_luck17 分钟前
基于springboot3+vue3的音乐推荐系统(协同过滤算法、Echarts图形化分析)
前端·echarts
八号当铺1 小时前
使用 Figma Agent Kit:插件 + MCP + 还原 Skill,打通本地设计协作
前端·人工智能·ai编程
一心只读圣贤书1 小时前
AI 辅助前端国际化实践:从文案梳理到多语言资源治理
前端
无责任此方_修行中2 小时前
搓了一个国产大模型与 AI Agent 比价工具
前端·后端·ai编程
szephyr2 小时前
腾讯云 ADP 智能体的 Skills 配置变更后没有走灰度,直接全量替换出了问题怎么定位?
java·前端·腾讯云·腾讯云adp
夏雪coding2 小时前
nginx SPA 回落把 CSS 变成了 HTML:一个返回 200 却让样式失效的坑
前端·nginx
光影少年2 小时前
React Native网络、存储与原生能力
前端·react native·react.js
bonibabi2 小时前
基于 CopilotKit + Java SSE 构建 AI Agent 的前端实践指南
前端·agent