vue + elementUI模糊搜索的使用


<el-autocomplete
    class="inline-input"
    v-model="enterpriseForm.companyName"
    :fetch-suggestions="querySearch"
    @focus="groupListMe"
    placeholder="请填写企业名称"
    style="width: 400px"
    @select="handleSelect"
></el-autocomplete>

watch:{
    
    
    'enterpriseForm.companyName': {
    
    
      deep: true,
      handler: function (newVal,oldVal){
    
    
        console.log(newVal, oldVal)
        this.groupArr = [];//这是定义好的用于存放下拉提醒框中数据的数组
        var len = this.groupList.length;//groupList
        var arr = [];
        for (var i = 0; i < len; i++) {
    
    //根据输入框中的值进行模糊匹配
          if (this.groupList[i].companyName.indexOf(newVal) >= 0) {
    
    
            arr.push({
    
    
              value: this.groupList[i].companyName,
              id: this.groupList[i].id
            })
          }
        }
        this.groupArr = arr
      }
    }
 },
methods: {
    
    
	getCompanyInfoList(companyName) {
    
    
      getCompanyInfo({
    
    
        page: 1,
        size: 999,
        companyName
      })
      .then(res => {
    
    
        if (res.code == 1) {
    
    
          this.groupList = []
          this.groupArr = []
          this.groupList = res.data
          for(let item of this.groupList) {
    
    
            this.groupArr.push({
    
    
              value: item.name,
              id: item.id
            })
          }
        } 
    })
},
  querySearch(queryString, cb) {
    
    
    var groupArr = this.groupArr;
    cb(groupArr);
  },
  groupListMe() {
    
    
    this.getCompanyInfoList(this.enterpriseForm.companyName)
  },
}

猜你喜欢

转载自blog.csdn.net/lannieZ/article/details/114634698
今日推荐