模糊查询要怎么弄呀!

正常状态下

img


现在是可以查询到的

img


但是当我在查询其他名字的时候就是空的了

img


下面是代码,不知道还需要怎么去写呀,能解决这个

handleSearch() {
  let newPersons = []
  this.persons.filter(item => {
    if (item.name.includes(this.keyword)) {
      newPersons.push(item)
      // this.persons.push(item)
    }
  })
  this.persons = newPersons
}

还有删除输入框的名字之后怎么能还原,原来的数据呀,求帮助!,万分感谢

就像1楼所说的那样,使用两个变量originalDatashowData,其中originalData为原始列表,showData为在页面上的显示列表。

handleSearch() {
  if (!this.keyword) {
      this.showData = this.originalData
      return
  }
  this.showData = this.originalData.filter(item => item.name.includes(this.keyword))
}

展示列表时使用showData

基于你这种操作逻辑,那最好创建两个变量,一个是完整的列表数据data,一个是操作过程中的列表数据tempData。在接口初始获取到列表数据res的时候,赋值data与tempData

this.data = res
this.tempData = this.data

然后页面使用tempData进行渲染,然后这个函数就改成

handleSearch() {
            if (!this.keyword) return this.data
            this.tempData = this.data.filter(i => i.name.includes(this.keyword))
}

tempData 其实就相当于你原本的persons