自定义el-tag标签

自定义el-tag标签
在这里插入图片描述

<template>
  <div>
    <el-tag
      v-for="(tag,index) in dynamicTags"
      :closable="index>4"
      :disable-transitions="false"
      @close="handleClose(tag)">
      {{ tag.label }}
    </el-tag>

    <div v-if="inputVisible" style="margin-top: 10px">
      <el-row :gutter="0.5">
        <div style="float: left">
          过去
          <el-input
            class="input-new-tag"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
          >
          </el-input>
        </div>
        <div style="float: left">
          <el-select ref="saveTagInput2" clearable v-model="period" style="width:45%;margin-left: 2px">
            <el-option v-for="(item,index) of periodList" :label="item.value" :value="item.key" :key="item.key">
              {{ item.value }}
            </el-option>
          </el-select>
        </div>
      </el-row>
    </div>
    <el-button v-else class="button-new-tag" size="small" @click="showInput">自定义周期</el-button>
    <el-button class="button-new-tag" size="small" @click="handleInputConfirm">确定</el-button>

  </div>

</template>


<script>
export default {
  data() {
    return {
      period: "",
      periodList: [
        {
          key: "HOUR",
          value: '小时'
        },
        {
          key: "DAY",
          value: '天'
        },
        {
          key: "WEEK",
          value: '星期'
        },
        {
          key: "MONTH",
          value: '月'
        },
        {
          key: "YEAR",
          value: '年'
        }
      ],
      dynamicTags: [{
        value: 'INTERVAL -1 HOUR',
        label: '过去1小时'
      }, {
        value: 'INTERVAL -1 DAY',
        label: '过去1天'
      }, {
        value: 'INTERVAL -1 WEEK',
        label: '过去1星期'
      },
        {
          value: 'INTERVAL -1 MONTH',
          label: '过去1月'
        },
        {
          value: 'INTERVAL -1 YEAR',
          label: '过去1年'
        }],
      inputVisible: false,
      inputValue: ''
    };
  },
  methods: {
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      //回调改变后的值并渲染
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });

    },

    handleInputConfirm() {
      // console.log("period=====>", this.period)
      // console.log("period=====>",typeof this.period)
      // console.log("period=====>", this.inputValue)
      // console.log("period=====>",typeof this.inputValue)
      if (this.period != '' && this.inputValue != '') {
        // console.log("period=====>", this.period)
        // console.log("inputValue=====>", this.inputValue)
        // value: 'INTERVAL -1 YEAR',
        //   label: '过去1年'
        let temp = {}
        temp.value = 'INTERVAL -' + this.inputValue + ' ' + this.period
        let periodName = ''
        this.periodList.forEach((item) => {
          if (item.key === this.period)
            periodName = item.value
        })
        // console.log("periodName=====>", periodName)
        temp.label = '过去 ' + this.inputValue+periodName
        console.log("temp=====>",temp)
        this.dynamicTags.push(temp);
        // console.log("=======>submit=====>")
      }
      // let inputValue = this.inputValue;
      // if (inputValue) {
      //   this.dynamicTags.push(inputValue);
      // }
      this.inputVisible = false;
      this.inputValue = '';
      this.period='';
    }
  }
}
</script>
<style>
.el-tag + .el-tag {
  margin-left: 10px;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值