element ui select框实现ctrl+c和ctrl+v

//多选框的形式


<template>
  <div>
    <el-select
      ref="select"
      placeholder="请选择主食"
      multiple
      filterable
      collapse-tags
      id="food"
      @keydown.ctrl.67.native="copyFocusedBoxContent"
      v-model="formData.food"
      clearable
      @input.native="dealInput"
    >
      <el-option
        v-for="item in options"
        :key="item.label"
        :label="item.label"
        :value="item.code"
      >
      </el-option>
    </el-select>
  </div>
</template>

<script>
export default {
  methods: {
    dealInput() {
      const inputValue = this.$refs.select.$refs.input.value.split(",");
      const recordIndex = [];
      for (const e of this.options) {
        recordIndex.push(e.code);
      }

      for (const f of inputValue) {
        if (recordIndex.includes(f) && !this.formData.food.includes(f)) {
          this.formData.food.push(f);
        }
      }
    },

    copyFocusedBoxContent() {
      this.$copyText(this.formData.food).then(
        (e) => {
          this.$message({
            message: "Copied",
            duration: 200,
          });
        },
        (e) => {
          this.$message({
            message: "Can not copy",
            duration: 200,
          });
        }
      );
    },
  },
  data() {
    return {
      formData: {
        food: [],
      },
      options: [
        {
          code: "11",
          label: "11-黄金糕",
        },
        {
          code: "12",
          label: "12-双皮奶",
        },
        {
          code: "13",
          label: "13-蚵仔煎",
        },
        {
          code: "14",
          label: "14-龙须面",
        },
        {
          code: "15",
          label: "15-北京烤鸭",
        },
      ],
    };
  },
};
</script>


单选框的形式

<template>
  <div>
    <el-select
      ref="select"
      placeholder="请选择主食"
      filterable
      id="food"
      @keydown.ctrl.67.native="copyFocusedBoxContent"
      v-model="formData.food"
      clearable
      @focus="dealFocus"
    >
      <!-- multiple -->
      <!-- collapse-tags -->
      <el-option
        v-for="item in options"
        :key="item.label"
        :label="item.label"
        :value="item.code"
      >
      </el-option>
    </el-select>
  </div>
</template>

<script>
export default {
  methods: {
    dealFocus() {
      //由于select是单选了这里需要通过this.$refs.select.$children[0].value;获取用户的输入值
      //还需要通过给最外层对象添加原生的事件this.$refs.select.$refs.reference.$refs.input.onblur
      this.$refs.select.$refs.reference.$refs.input.onblur = () => {
        const inputValue = this.$refs.select.$children[0].value;
        const recordIndex = [];
        for (const e of this.options) {
          recordIndex.push(e.code);
        }
        if (recordIndex.includes(inputValue)) {
          this.formData.food = inputValue;
        }
      };
    },

    copyFocusedBoxContent() {
      this.$copyText(this.formData.food).then(
        (e) => {
          this.$message({
            message: "Copied",
            duration: 200,
          });
        },
        (e) => {
          this.$message({
            message: "Can not copy",
            duration: 200,
          });
        }
      );
    },
  },
  data() {
    return {
      formData: {
        food: "",
      },
      options: [
        {
          code: "11",
          label: "11-黄金糕",
        },
        {
          code: "12",
          label: "12-双皮奶",
        },
        {
          code: "13",
          label: "13-蚵仔煎",
        },
        {
          code: "14",
          label: "14-龙须面",
        },
        {
          code: "15",
          label: "15-北京烤鸭",
        },
        {
          code: "115",
          label: "115-xxxx",
        },

        {
          code: "111",
          label: "111-ssss",
        },
      ],
    };
  },
};
</script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值