多选单选混合 element-ui

本文介绍了在Vue.js项目中使用Element-UI库实现多选和单选组件混用的方法,探讨了可能的实现策略,并邀请读者分享更优的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * 工具描述: 用于实现多选单选混合
 * @param {Array} arr 多选值
 * @param {Array} nlt 单选值
 * @param {Array} _this 数据绑定对象
 * @param {Array} last 最后点击的选项
 * 使用方法:
 * 1. 引入: import { checkedFn } from "@/utils/multiSelect.js"
 * 2. 使用:用于element=>checkbox-group 容器中 标签添加 @change="val => Checked(val, type(区分值))" 事件
 */

// 示例
// 单选多选
// Checked (item, type) {
//   let arr = []
//   let nlt = []
//   let _this = null

//   // 平台
//   if (type === 'platform') {
//     arr = ['AUDIENCE_IOS', 'AUDIENCE_ANDROID']
//     nlt = ['AUDIENCE_UNLIMITED', 'AUDIENCE_PC']
//     _this = this.ruleForm.platform
//   }

//   _this = checkedFn(arr, nlt, _this, item)
// }

export const checkedFn = function (arr = [], nlt = [], _this = [], last) {
  let va = last[last.length - 1]
  if (nlt.indexOf(va) !== -1) {
    arr.forEach(item => {
      if (_this.indexOf(item) !== -1) _this.splice(_this.indexOf(item), 1)
    })
    nlt.forEach(item1 => {
      if (_this.indexOf(item1) !== -1) if (item1 !== va) _this.splice(_this.indexOf(item1), 1)
    })
  } else {
    arr.forEach(item2 => {
      if (_this.indexOf(item2) !== -1) {
        if (_this.indexOf(nlt) !== -1) _this.splice(_this.indexOf(nlt), 1)
        nlt.forEach(item3 => {
          if (_this.indexOf(item3) !== -1) _this.splice(_this.indexOf(item3), 1)
        })
      }
    })
  }
  return _this
}

如果有大佬知道更好的方法可以教教我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值