element-ui输入框如何实现回显的多选样式?

  废话不多说直接上效果🧐

效果图
<template>
  <div>
    <el-form
        :model="params"
        ref="queryForm"
        size="small"
        :inline="true"
        label-width="68px"
      >
      <el-form-item label="标签" prop="tag">
        <el-input
          v-model="inputContent"
          clearable
          readonly
        >
          <template slot="prefix">
            <el-tag slot="prepend" type="info" size="small" v-if="checkboxGroup.length>=1" closable  @close="handleClose(checkboxGroup[0])">{{ checkboxGroup[0] }}</el-tag>
            <el-tag slot="prepend" type="info" size="small" v-if="checkboxGroup.length>1">+{{ checkboxGroup.length-1 }}</el-tag>
          </template>
          <template slot="suffix">
            <i class="el-input__icon el-icon-menu" @click="openTags"></i>
          </template>
        </el-input>
      </el-form-item>
    </el-form>

    <el-dialog title="选择标签" :visible.sync="tagsDialog" width="824px" append-to-body>
      <div class="select">
        <div class="title">已选择标签:</div>
        <el-tag
          :key="tag"
          v-for="tag in checkboxGroup"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
          effect="plain">
          {{tag}}
        </el-tag>
      </div>
      <el-divider></el-divider>
      <el-checkbox-group v-model="checkboxGroup">
        <el-checkbox v-for="item in dynamicTags" :key="item" :label="item" border></el-checkbox>
      </el-checkbox-group>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTags">确 定</el-button>
        <el-button @click="tagsDialog = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'InputCheckbox',
  data() {
    return {
      params: {
        tag: [],
      },
      inputContent: '',
      tagsDialog:false,
      checkboxGroup:[],
      dynamicTags: ['标签一', '标签二', '标签三','标签四'],
    };
  },
  computed: {  
    // 计算并设置输入框的显示值
    formattedDisplayValue() {
      if(!this.checkboxGroup.length) return;
      return this.checkboxGroup.length >= 1 ? `${this.checkboxGroup.slice(0, 1).join(', ')} +${this.checkboxGroup.length - 1}`:this.checkboxGroup[0];
    },
  },
  watch:{
    checkboxGroup(){
      this.params.tag = this.formattedDisplayValue;
    }
  },
  methods: {
    openTags(){
      this.tagsDialog = true;
    },
    submitTags(){
      this.tagsDialog = false;
    },
    handleClose(tag) {
      this.checkboxGroup.splice(this.checkboxGroup.indexOf(tag), 1);
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-checkbox{
  margin-right: 0;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.el-input__icon{
  cursor: pointer;
}
.select{
  .title{
    margin-bottom: 10px;
  }
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五秒法则

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值