vue~el-autocomplete实现组件化
  2xk0JyO908yA 2023年11月01日 33 0
Vue

el-autocomplete核心参数

可以实现异步的数据拉取,从异步返回的数据中,选择需要的结果,并回显到文本框中。

fetch-suggestions

回调列表,异步的方式获取数据列表,显示在列表框中

@select

当选中某一项时,会触发这个方法,将数据获取到,这时,我们可以将数据回显,或者赋值给父页面上的元素,如果希望赋值父页面上v-model绑定的元素,可以通过v-model原理中说的,绑定input事件,将当前值进行传递即可。

实例代码

子组件代码

<template>
  <div>
    <el-autocomplete
      :fetch-suggestions="fetchSuggestions"
      v-model="dataValue"
      @select="handleSelect"
    >
      <template slot-scope="{ item }">{{ item.label }}</template>
    </el-autocomplete>
  </div>
</template>
<script>
export default {
  name: 'SearchLawfirmName',
  //allInfos是父组件传来的值,如果allInfos不是父组件传来的就不用这样写
  props: ["allInfos"],
  data() {
    return {dataValue: null}
  },
  methods: {
    fetchSuggestions(queryString, cb) {
      let results = this.allInfos;
      results = queryString
        ? results.filter(this.createFilter(queryString))
        : results;
      cb(results);
    },
    createFilter(queryString) {
      return (item) => {
        return item.value.toUpperCase().match(queryString.toUpperCase());
      };
    },
    handleSelect(item) {
      this.dataValue = item.label; // 回调在文框中显示的值
      this.$emit('input', item.value);// 绑定到父组件的值
    },
  }
};
</script>

父页面上引用它

<SearchLawfirmName v-model="searchLawfirmName" :allInfos="allData"></SearchLawfirmName>
import SearchLawfirmName from "@/components/SearchLawfirmName/index.vue";
export default {
data() {
    return {
      allData:[
        { value: 'result1', label: 'Result 1' },
        { value: 'result2', label: 'Result 2' },
        { value: 'result3', label: 'Result 3' }
      ],
      searchLawfirmName:null
     }
   }   
}

上面代码中,我们通过import引入了自定义组件,再将组件选中的当前值赋给页面属性searchLawfirmName,我们将异步的数据列表allData通过allInfos参数进行传递。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  gYl4rku9YpWY   2024年05月17日   57   0   0 Vue
  JZjRRktyDDvK   2024年05月02日   80   0   0 Vue
  VlNAKfyhjjp9   2024年04月30日   77   0   0 Vue
  JNTrZmaOQEcq   2024年04月30日   54   0   0 Vue
  onf2Mh1AWJAW   2024年05月17日   59   0   0 Vue
  3A8RnFxQCDqM   2024年05月17日   58   0   0 Vue
2xk0JyO908yA