步骤:
- 把全部数据渲染到页面上
- 定义一个新的数组searchValueData存放查找到的数据
- 遍历所有的数据,比对输入框中现有的值与需要做比较的字段,如果存在,把这个值存在searchValueData数组中
- 每次输入框中的值改变(值改变监听函数中)调用搜索方法
由于每次输入值改变都会调用搜索函数,太占内存,所以在监听值改变函数中使用了防抖,使函数在触发过后的一定时间后再执行,提高了程序运行效率
输入框:
<el-input
clearable
placeholder="输入关键字搜索"
prefix-icon="el-icon-search"
v-model="searchText"
@input = "inputChange"
>
</el-input>
搜索方法
searchValue(str){
this.searchValueData = []
this.newtableData.forEach((el)=>{
if(el.username.indexOf(str)!==-1 || el.class.indexOf(str)!==-1 || el.week.indexOf(str)!==-1 || el.uid.indexOf(str)!==-1){
this.searchValueData.push(el)
}
})
}
调用值改变触发函数
inputChange(str){
//定时器,防抖
if(this.timer!==''){
clearTimeout(this.timer)
this.timer=''
}
this.timer = setTimeout(()=>{
this.searchValue(str)
this.timer = ''
},500)
}