正常状态下
handleSearch() {
let newPersons = []
this.persons.filter(item => {
if (item.name.includes(this.keyword)) {
newPersons.push(item)
// this.persons.push(item)
}
})
this.persons = newPersons
}
还有删除输入框的名字之后怎么能还原,原来的数据呀,求帮助!,万分感谢
就像1楼所说的那样,使用两个变量originalData
、showData
,其中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