ElementUi el-autocomplete 踩坑 (使用clearable清除,点击输入框下拉条件不再显示)

今天在写组件的时候,用到了el-autocomplete来做模糊搜索。因为要可以清除条件,所以加了clearable属性,然后遇到了个bug。点击清除图标后,如果你已经是聚焦状态了,你在点击输入框,下拉框不会再显示了

在这里插入图片描述
查了一下,是因为有element-ui源码有bug,主要原因是

参考该博客

autocomplete组件在执行清除事件时,将activated置为false。这时候下拉框不会显示了,而在querySearch执行成功后又没有将activated置为true。所以导致了该bug。解决的核心思路就是想办法把this.activated的值设置为true。或者是清除完后让输入框失去焦点。

在这里插入图片描述

解决办法有两种:

1.给el-autocomplete添加一个ref。在清除事件后调用 this.$refs.el_auto.activated = true

         <el-form-item :label="$t('code_begin')" v-if="popoverForm.hasOwnProperty('code_begin')">
            <el-autocomplete
              class="inline-input"
              v-model="popoverForm.code_begin_text"
              :fetch-suggestions="querySearch"
              size="medium"
              :placeholder="$t('code_begin')"
              @select="handleSelect($event,'begin')"
              clearable
              @clear="clearSelect('begin')"
              ref="el_auto"
              value-key="acc_title"
            >
              <i slot="suffix" class="el-input__icon el-icon-notebook-2 icon-style" @click="openChooseAccDialog('begin')" />
            </el-autocomplete>
          </el-form-item>
    // clearable清除事件事件
    clearSelect(type) {
      console.log('type', type)
      if (type === 'begin') {
        this.popoverForm.code_begin_text = ''
        this.popoverForm.code_begin = null
        // 主要代码
        this.$refs.el_auto.activated = true
      } else if (type === 'end') {
        this.popoverForm.code_end_text = ''
        this.popoverForm.code_end = null
      } else if (type === 'acc_no') {
        this.popoverForm.acc_no_text = ''
        this.popoverForm.acc_no = null
      } else if (type === 'acc_subject') {
        this.popoverForm.acc_no_text = ''
        this.popoverForm.acc_no = null
      } else if (type === 'assist_item_no') {
        this.popoverForm.assist_item_text = ''
        this.popoverForm.assist_item_no = null
      }
      this.$forceUpdate()
    },

2.在清除事件里调用document.activeElement.blur()

因为我的控件使用到了多个el-autocomplete。每个都加上ref比较麻烦。所以使用该方法,只有一行代码。
将聚集的输入框失去焦点即可。

 // clearable清除事件事件
    clearSelect(type) {
      console.log('type', type)
      if (type === 'begin') {
        this.popoverForm.code_begin_text = ''
        this.popoverForm.code_begin = null
      } else if (type === 'end') {
        this.popoverForm.code_end_text = ''
        this.popoverForm.code_end = null
      } else if (type === 'acc_no') {
        this.popoverForm.acc_no_text = ''
        this.popoverForm.acc_no = null
      } else if (type === 'acc_subject') {
        this.popoverForm.acc_no_text = ''
        this.popoverForm.acc_no = null
      } else if (type === 'assist_item_no') {
        this.popoverForm.assist_item_text = ''
        this.popoverForm.assist_item_no = null
      }
      console.log('focus', document.activeElement)
      // 主要代码 
      // document.activeElement获得了DOM中被聚焦的元素;.blur()取消聚焦
      document.activeElement.blur()
      this.$forceUpdate()
    },

这里做个记录,el-autocomplete接收的数组需要是 value属性才能显示。
但其实提供了一个value-key 属性,可以指定对应的key。
因为之前不知道有value-key属性,我还将调用接口获得的数据进行了转换。QAQ

在这里插入图片描述

  • 13
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
Vue.js 3中实现自动完成下拉列表的方法很简单,可以使用Vue.js提供的指令和组件的功能来实现。 首先,你需要引入ElementUI库,它是一个基于Vue.js的组件库,提供了非常好用的组件和指令。其中el-autocomplete是用来实现输入框的自动完成下拉列表的组件。 接下来,你需要在Vue.js的template(模板)中使用el-autocomplete组件,最基本的使用方法如下: ```html <template> <el-autocomplete v-model="value" :fetch-suggestions="querySearch" placeholder="请输入" /> </template> ``` 在这里,v-model用来绑定输入框的值,placeholder用来设置输入框的提示语,:fetch-suggestions用来绑定一个函数,该函数会在输入框输入时被调用,它的返回值是一个数组,用来显示下拉列表中的选项。 接下来,你需要在Vue.js的script(脚本)中实现querySearch函数,该函数需要返回一个Promise对象,用来异步获取输入框输入后的联想词,例如: ```javascript <script> import { ref } from 'vue'; import { fetchSuggestions } from '@/api/suggestion.js'; export default { setup() { const value = ref(''); const querySearch = async (queryString) => { return await fetchSuggestions(queryString); }; return { value, querySearch, }; }, }; </script> ``` 在这里,我们用了Vue.js 3中新增的setup函数,在该函数中,我们使用了ref定义了value变量,并定义了querySearch函数,该函数使用async/await实现异步调用了一个api/suggestion.js的函数fetchSuggestions,该函数的接口可以根据自己的实际需求进行实现。 最后,你需要在Vue.js的style(样式)中引入ElementUI的样式,以确保el-autocomplete组件正常显示: ```css @import "element-plus/packages/theme-chalk/src/index.scss"; ``` 综上所述,使用Vue.js 3和ElementUI库实现输入框的自动完成下拉列表功能就这么简单,你只需要引入库、定义组件和指令、实现函数即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值