antd select 多选限制个数

直接给出源码

<template>
       <a-select
              v-model="college"
              mode="multiple"
              :maxTagCount="2"
              autocomplete="off"
              style="width: 252px;margin-top:10px;margin-right: 28px"
              :getPopupContainer="triggerNode => triggerNode.parentNode"
            >
              <a-select-option
                v-for="item in collegeList"
                :key="item.id"
                :value="item.name"
                :disabled="college.length >= 5 && college.findIndex(o => o === item.name) === -1"
              >
                {{ item.name }}
              </a-select-option>
          </a-select>
</template>
 <script>  
	 data() {
	    return { 
	      collegeList: [],
	      college: []
	    }
	  },
 </script>  

其中核心代码在于disabled
其中 collegeList数据结构

[
  {
    "city": "北京市",
    "code": "4111010001",
    "createBy": null,
    "createTime": null,
    "department": "教育部",
    "id": 1,
    "level": "本科",
    "name": "北京大学",
    "updateBy": null,
    "updateTime": null
  },
  {
    "city": "北京市",
    "code": "4111010002",
    "createBy": null,
    "createTime": null,
    "department": "教育部",
    "id": 2,
    "level": "本科",
    "name": "中国人民大学",
    "updateBy": null,
    "updateTime": null
  },
  {
    "city": "北京市",
    "code": "4111010003",
    "createBy": null,
    "createTime": null,
    "department": "教育部",
    "id": 3,
    "level": "本科",
    "name": "清华大学",
    "updateBy": null,
    "updateTime": null
  }
]

再看一个复杂一点的例子:

		<a-select
              v-model="safeGroup"
              mode="multiple"
              placeholder="请选择安全组"
              class="long-input"
              :getPopupContainer="triggerNode => triggerNode.parentNode"
              @change="handleSafeChange"
            >
              <a-select-option
                v-for="item in SafeGroupList"
                :key="item.safeGroupId"
                :value="JSON.stringify(item)"
                :disabled="
                  safeGroup.length >= 5 &&
                  safeGroup.findIndex(o => JSON.parse(o).safeGroupId === item.safeGroupId) === -1
                "
              >
                <ellipsis :length="20"> {{ item.safeGroupName }}</ellipsis>
              </a-select-option>
             
            </a-select>

其中对v-model绑定的数据处理在@chang方法里

handleSafeChange(e) {
    if (e) {
      this.dispalySafe = false
      this.safeGroup = e
      const arr1 = e.map(x => {
        return JSON.parse(x)
      })
      const arr2 = e.map(x => {
        return JSON.parse(x).safeGroupId
      })
      this.$emit('handleSafeChange', arr2, arr1)
    }
  },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值