有時候后端他把數(shù)據(jù)丟給你奈籽,就靠你直接去本地做篩選,因為數(shù)據(jù)不是很多嘛慎框,那么小程序本地怎么做篩選功能呢
因為渲染你只能用一個數(shù)據(jù)去渲染疾宏,所以如果你去把拿到的數(shù)據(jù)去篩選就會導(dǎo)致原數(shù)據(jù)被破壞,一開始有想過用深拷貝愤钾,可能是自己太菜瘟滨,搞不定,所以我用的另一種方法能颁,就是把數(shù)據(jù)本地備份一份
在請求拿到數(shù)據(jù)的時候備份一份
mzghxxcx(res.data.data).then(res => {//請求數(shù)據(jù)
this.setData({
mzghxxcx:res.dataJson,//備份一份作為篩選使用
screening:res.dataJson
})
}
然后再點擊搜索后
let filter = []
let val = this.data.value//搜索框的值
let mzghxxcx = this.data.mzghxxcx//總數(shù)據(jù)
let screening = this.data.screening//篩選備份數(shù)據(jù)
if(mzghxxcx == [] || mzghxxcx == ""){//初始數(shù)據(jù)為空
Toast.fail("查詢結(jié)果失敗");
return mzghxxcx
}else{
console.log(this.data.value);
if(this.data.value == "" || this.data.value == null){//輸入框為空或者null賦值總數(shù)據(jù)
this.setData({
screening:mzghxxcx//備份的數(shù)據(jù)這時候就用上了
})
}else {
screening.filter(its => {//在將得到的所有數(shù)據(jù)遍歷
if(its.NAME.indexOf(val) !== -1) filter = [...filter,its]//數(shù)據(jù)中是否有搜索關(guān)鍵字杂瘸,有則返回
})
this.setData({
screening:filter//過濾后的數(shù)據(jù)
})
}
}
},