Angular ui-grid 列表中设置开关按钮

一、gridOptions.columnDefs 设置

      {name: 'isEnabled', displayName: '状态', width: 50, enableColumnMenu: false, cellFilter: 'enable',
        cellTemplate: '<div style="padding: 8px" class="icon-center">'
            + '<span id="back-{{row.entity.id}}" ng-class="grid.appScope.homeCareProject.getSwitchBackClass(row.entity)" '
            + 'uib-tooltip="{{grid.appScope.homeCareProject.getSwitchTip(row.entity)}}" '
            + 'tooltip-placement="bottom">'
              + '<span id="front-{{row.entity.id}}" ng-click="grid.appScope.homeCareProject.toggle(row.entity)" '
              + 'ng-class="grid.appScope.homeCareProject.getSwitchFrontClass(row.entity)"></span>'
            + '</span>'
          + '</div>'
      },

二、按钮触发逻辑代码

  getSwitchBackClass(homeCareProject) {
    return homeCareProject.isEnabled === 1 ? "back-open" : "back-close";
  }

  getSwitchFrontClass(homeCareProject) {
    return homeCareProject.isEnabled === 1 ? "front-open" : "front-close";
  }

  getSwitchTip(homeCareProject) {
    return homeCareProject.isEnabled === 1 ? "开启" : "禁用";
  }

  toggle(homeCareProject) {
    let back = document.getElementById("back-" + homeCareProject.id);
    let front = document.getElementById("front-" + homeCareProject.id);
    back.className = (back.className === "back-close") ? "back-open" : "back-close";
    front.className = (front.className === "front-close") ? "front-open" : "front-close";
    this.updateIsEnabled(homeCareProject);
  }

  updateIsEnabled(homeCareProject) {
    let back = document.getElementById("back-" + homeCareProject.id);
    homeCareProject.isEnabled = (back.className === "back-close") ? 0 : 1;
    this.HomeCareService.addOrUpdateHomeCareProject(homeCareProject)
      .then(res => {
          if (res.data.code === "SF0000") {
            this.toastr.success("修改启用状态成功!", "提示信息");
          } else {
            this.toastr.error(res.data.msg, "提示信息");
          }
        }
      )
  }

三、CSS 代码

.back-open {
    width: 24px;
    height: 14px;
    border-radius: 7px;
    position: absolute;
    background: rgba(0,184,0,0.8);
  }
.back-close {
    width: 24px;
    height: 14px;
    border-radius: 7px;
    position: absolute;
    background: rgba(120, 120, 120, 0.1);
    border:1px solid rgba(60, 60, 60, 0.2);
    border-left: transparent;
  }
.front-open {
    width: 12px;
    height: 12px;
    border-radius: 6px;
    position: absolute;
    background: #ffffff;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
    top: 1px;
    right: 1px;
    outline: none;
  }
.front-close {
    width: 12px;
    height: 12px;
    border-radius: 6px;
    position: absolute;
    background: #ffffff;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
    top: 0;
    left: 1px;
    border:1px solid rgba(0, 0, 0, 0.2);
    outline: none;
  }

四、效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值