vue element ui tag标签组,获取选中值,存入数组

我的tag标签组,是通过数据字典遍历获取的。通过点击,使选中的值的type改变,再次点击,type变回灰色,并将选中的值,存入数组。

效果图与选中的值打印如下:

 

原理是通过设置flag为true,选中的值存入数组,再次点击,flag为false,从数组中当前值的下标移除。

代码如下:

html部分:

<dict-tag v-for="dict in this.getDictDatas(DICT_TYPE.CONTRACT_LABEL)" :value="dict.value"
                        :key="dict.value"
                        :type="DICT_TYPE.CONTRACT_LABEL" @click.native="typeVal(dict.colorType,dict.value)"></dict-tag>

js部分:

// 合同标签点击
      typeVal(type, id) {
        this.contractData[id].colorType = 'primary'
        // if (type == this.contractData[id].colorType) {//当type==primary
        if (this.contractTagList.includes(id)) {//当type==primary
          this.contractData[id].colorType = 'info'
          this.flag = false
          this.contractTagList.splice(this.contractTagList.indexOf(id), 1)
        } else {
          this.flag = true
          this.contractTagList.push(id)
        }
        console.log(this.contractTagList,'this.contractTagList')
      },

方法二:通过data获取(不过也有问题,最好是通过调后端接口数据获取,后面在沟通)前一个方法是获取id,之后详情页显示不好展示,故这两种方法都改成存值

代码如下:

html部分:

<el-tag v-for="(item,index) in tagList" :type="item.type" :key="index" @click.native="tagVal(item,index)">
                {{ item.value }}
              </el-tag>

js部分:

//data数据
//合同标签
       tagList: [
          {type: 'info', value: '临时合同'},
          {type: 'info', value: '工位合同'},
          {type: 'info', value: '公寓合同'},
          {type: 'info', value: '短期合同'},
          {type: 'info', value: '长期合同'},
          {type: 'info', value: '不含税合同'},
          {type: 'info', value: '含税合同'},
          {type: 'info', value: '优惠合同'},
          {type: 'info', value: '有意向续约'},
        ],


//点击事件
tagVal(item, index) {
        this.tagList[index].type = 'primary'
        if (this.contractTagList.includes(item.value)) {//当type==primary
          this.tagList[index].type = 'info'
          this.flag = false
          this.contractTagList.splice(this.contractTagList.indexOf(item.value), 1)
        } else {
          this.flag = true
          this.contractTagList.push(item.value)
        }
        console.log(this.contractTagList, 'this.contractTagList')
        this.$set(this.form, 'contractTagList', this.contractTagList)
      },

效果图如下:

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值