element input历史查询(模糊查询)以及防抖处理

4 篇文章 0 订阅

element input历史查询(模糊查询)以及防抖处理

在main.js里添加全局-----防抖处理

Vue.prototype.debounce = function(fn, delay) {
	//防抖
	let timer = null; //声明一个timer
	return function () {
	  if (timer) {
		//如果timer有值的话代码已经执行过了。执行了的话是不是要清空
		clearTimeout(timer);
	  }
	  timer = setTimeout(() => {
		fn.apply(null, arguments);
	  }, delay); //如果没有的话代表还未在规定的时间内执行
	};
  }
  • html
<template>
	<div>
					<el-autocomplete
                    v-model="item.tp004"
                    :fetch-suggestions="inputChange"
                    placeholder="请输入证件号码"
                    @select="handleSelect($event, item)"
                  ></el-autocomplete>
	</div>
</template>
  • js
methods:{
//---------------------------------------------
//防抖处理
	inputChange(name, callback) {
      this.debounce(this.querySearch(name, callback), 1000)
    },
    //查询搜索处理
    //身份证号码
    querySearch(name, callback) {
      this.restaurants = [];//重新定义,防止累加
      if (name != "" && name.length > 0) {
        //获取搜索数据
        this.$get(this.api.getIdCard, {
          IdCard: name,
        })
          .then((res) => {
            if (res.data == undefined) {
              callback([
                {
                  id: "暂无数据",
                  value: "暂无数据",
                },
              ]);
            } else {
              res.data.forEach((item) => {
                this.restaurants.push({
                  id: item.tp001,
                  value: item.tp004,
                });
              });
              // 调用 callback 返回建议列表的数据,是一个数组类型
              callback(this.restaurants);
            }
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
    handleSelect(e, item) {
    //根据选中的身份证号所属的id,查出这个人所有身份信息,回显
      this.$get(this.api.getPersonInfo, {
        tp001: e.id,
      })
        .then((res) => {
          if (res.data.tp001) {
            item.tp002 = res.data.tp002;
            item.tp003 = res.data.tp003;
            item.tp004 = res.data.tp004;
            item.tp006 = res.data.tp006;
            item.tp005 = res.data.tp005;
            item.tp007 = res.data.tp007;
            item.tp010 = res.data.tp010;
            item.tp008 = res.data.tp008;
            item.tp009 = res.data.tp009;
            item.tp011 = res.data.tp011;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
}
//------------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中使用element-ui的Input组件实现模糊查询,你可以通过v-model绑定输入框的值,并监听输入框的change事件,在事件处理函数中调用接口进行查询操作。另外,你可以使用element-ui的Autocomplete组件来实现带有下拉提示的模糊查询效果。 示例代码如下: ```html <template> <div> <el-input v-model="keyword" @change="handleSearch" placeholder="请输入关键字"></el-input> <el-autocomplete v-model="selectedValue" :fetch-suggestions="querySearch" placeholder="请输入关键字" @select="handleSelect"> <template slot-scope="{ item }">{{ item }}</template> </el-autocomplete> </div> </template> <script> export default { data() { return { keyword: '', selectedValue: '', suggestions: [] }; }, methods: { handleSearch() { // 调用接口进行查询操作,将查询结果更新到suggestions数组中 // 示例代码中直接使用了一个固定的suggestions数组作为演示,请根据实际需求进行修改 this.suggestions = ['结果1', '结果2', '结果3']; }, querySearch(queryString, cb) { // 在这里可以根据输入的queryString调用接口获取匹配的结果,并通过cb回调函数返回给Autocomplete组件 // 示例代码中直接使用了一个固定的suggestions数组作为演示,请根据实际需求进行修改 const results = queryString ? this.suggestions.filter(item => item.includes(queryString)) : this.suggestions; cb(results); }, handleSelect(item) { // 处理选中某个下拉项的逻辑 console.log('选中的项:', item); } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值