vue实现Input输入框模糊查询方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

vue实现Input输入框模糊查询方法

  2021-04-02 我要评论

原理:原生js的indexOf() 方法,该方法将从头到尾地检索数组,看它是否含有对应的元素。开始检索的位置在数组 start 处或数组的开头(没有指定 start 参数时)。如果找到一个 item,则返回 item 的第一次出现的位置。开始位置的索引为 0。

如果在数组中没找到指定元素则返回 -1。

下面先看示例:

搜索前:

搜索后:

实现方法:

methods:{
 // 点击搜索工程
 search(){
 // 支持模糊查询
 // this.xmgcqkJsonsData:用于搜索的总数据
   // toLowerCase():用于把字符串转为小写,让模糊查询更加清晰
 let _search = this.jobNo.toLowerCase();
 let newListData = []; // 用于存放搜索出来数据的新数组
 if (_search) {
 this.xmgcqkJsonsData.filter(item => {
  if (item.code.toLowerCase().indexOf(_search) !== -1) {
  newListData.push(item);
  }
 }) 
 }
 this.xmgcqkJsonsData = newListData;
 // console.log(‘新数组',newListData)
 }, 
}

以上方法是根据 工单号/流水号 进行查询的,如果在当前基础上增加对其它条件的搜索,比如 项目/工程名称,那只需要在原来的代码上增加一个判断条件即可,如:

if (item.code.toLowerCase().indexOf(_search) !== -1 || item.name.toLowerCase().indexOf(_search) !== -1) {
 newListData.push(item);
 }

这就是如何实现vue input输入框模糊查询的方法。

关于vue.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们