前端(vue)实现模糊搜索,类似百度搜索

本文介绍了如何使用Vue.js在前端实现模糊搜索功能,类似百度搜索的体验。通过示例代码,展示了具体的实现步骤和关键点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

展示:

 

 

代码实现:

<el-form-item label="物资编码:" prop="materialCode">
          <el-input
            @input="autoSearch"
            v-model.trim="newForm.materialCode"
            placeholder="请填写物资编码"
            :disabled="diaTitle == '编辑物资'"
          />
        </el-form-item>
        <ul v-if="showList">
          <p @click="chooseCode(index)" style="height:30px;line-height:30px;margin-left:70px" v-for="(it,index) in this.searchList" :key="index">
            {{it}}
          </p>
          
        </ul>
data() {
    return {
      showList:true,
}
methods: {
// 模糊搜索功能
    autoSearch(eve) {
      console.log(eve);
      this.showList = true  // 选了编码后已经关闭可选内容,如改变搜索条件需要重启showList
      materiaGetType().then(res => {
        if (res.code === 0) {
          this.searchList = res.data;
          this.newSearchList = res.data
        }
      });
      this.autoShow()
    },
    autoShow() {
      var allowPass = true;
      if (!allowPass) {
        return;
      }
      setTimeout(() => {
        allowPass = false;
        console.log('来了没')
        this.searchList = [];
        this.newSearchList.forEach(ele => {
          if(ele.indexOf(this.newForm.materialCode)!== -1){
            console.log('是吗')
            this.searchList.push(ele)
          }
        });
      }, 1000);
    },
chooseCode(ind){
      this.showList = false
      this.newForm.materialCode = this.searchList[ind]
    },
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Math.random( ) ;

如果你刚好喜欢,则可以给我点动

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

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

打赏作者

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

抵扣说明:

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

余额充值