微信小程序?qū)崿F(xiàn)歷史搜索功能的全過程(h5同理)
1.實現(xiàn)效果

2.實現(xiàn)原理
微信小程序中將數(shù)據(jù)存在storage中,除非用戶手動觸發(fā),否則不會過期,同理,若想在瀏覽器中實現(xiàn),只需將數(shù)據(jù)存在localStorage中即可。
將數(shù)據(jù)存儲在本地緩存中指定的 key 中。會覆蓋掉原來該 key 對應的內(nèi)容。除非用戶主動刪除或因存儲空間原因被系統(tǒng)清理,否則數(shù)據(jù)都一直可用。單個 key 允許存儲的最大數(shù)據(jù)長度為 1MB,所有數(shù)據(jù)存儲上限為 10MB。 存儲的內(nèi)容,只支持原生類型、Date、及能夠通過
JSON.stringify序列化的對象
注意:wx.setStorage是異步的,wx.setStorageSync是同步的,即要等待執(zhí)行完才會去執(zhí)行其他的代碼
將已搜索的數(shù)據(jù)存在本地緩存中。
wx.setStorageSync('search_history', JSON.stringify(this.data.list))選擇存在本地緩存中的前15條數(shù)據(jù)顯示在頁面中。
if (wx.getStorageSync('search_history') ){
this.setData({
list:JSON.parse(wx.getStorageSync('search_history') ).slice(0, 15)
})
}每次選擇歷史數(shù)據(jù)的時候,將選擇的數(shù)據(jù)移到數(shù)組的第一條。
this.data.list.unshift(data);
點擊'清空歷史'按鈕,wx.removeStorage清空存在本地緩存中的歷史記錄列表。
從本地緩存中移除指定 key,是wx.removeStorage 的同步版本。
3.實現(xiàn)步驟
- 定義熱門搜索列表,搜索歷史列表,搜索名稱
hot_list:['杜甫','李白','李清照','姜子牙','萬事如意,心想事成'],//熱門搜索 list:[],//搜索歷史列表 search: "",//當前搜索內(nèi)容
- 在onShow事件中,判斷本地緩存是否存在key為search_history的數(shù)據(jù),如果有就選取前15條數(shù)據(jù)
onShow: function () {
if (wx.getStorageSync('search_history') ){
this.setData({
list:JSON.parse(wx.getStorageSync('search_history') ).slice(0, 15)
})
}
},- 為搜索框添加bindconfirm事件,如果搜索的數(shù)據(jù)已存于歷史列表中,先刪除,然后unshift進數(shù)組的頭部,并截取前15個存進本地緩存中
handleInput(e) {
let data = e.detail.value.replace(/(^\s*)|(\s*$)/g, "");//去掉前后的空格
if (data.trim() != '') {
this.handleData(data)
}
},- 處理存入數(shù)據(jù)
handleData(e) {
this.data.list.forEach((item, index) => {
if (item == e) {
this.data.list.splice(index, 1);
}
})
this.data.list.unshift(e);
this.setData({
list: this.data.list.slice(0, 15)
})
wx.setStorageSync('search_history', JSON.stringify(this.data.list))
},- 點擊熱門搜索中的數(shù)據(jù),添加事件,如果選中的數(shù)據(jù)已存于歷史列表中,先刪除,然后unshift進數(shù)組的頭部,并截取前15個存進本地緩存中
handleIHotItem(e) {
let { index } = e.currentTarget.dataset, { hot_list } = this.data;
let search = hot_list[index]
this.setData({
search,
})
// 將標簽存到歷史搜索中
this.handleData(search)
},- 點擊搜索歷史列表中的數(shù)據(jù),添加事件,如果選中的數(shù)據(jù)已存于歷史列表中,先刪除,然后unshift進數(shù)組的頭部,并截取前15個存進本地緩存中
handleIHisItem(e) {
let { index } = e.currentTarget.dataset, { list } = this.data;
let search = list[index]
this.setData({
search: search
})
this.handleData(search)
},- 點擊'清空歷史'按鈕,添加清空事件
clearHistory() {
this.setData({
list:[]
})
wx.removeStorageSync('search_history')
},4.實現(xiàn)代碼
<view class="head flex-row">
<view class="head_input">
<image src="/img/search_icon.png" class="search_icon"></image>
<input type="text" placeholder="搜索" placeholder-class="place_holder" bindconfirm="handleInput" value="{{search}}"></input>
</view>
<view class="sha_icon" catchtap="clear_input">取消</view>
</view>
<view class="con">
<view class="title">熱門搜索</view>
<view class="flex-row list">
<block wx:for="{{hot_list}}" wx:key="index">
<view class="c_item color" data-index="{{index}}" catchtap="handleIHotItem">{{item}}</view>
</block>
</view>
<view wx:if="{{list.length>0}}">
<view class="flex-row j_b">
<view class="title">搜索歷史</view>
<view catchtap="clearHistory" class="clear">清空歷史</view>
</view>
<view class="flex-row list">
<block wx:for="{{list}}" wx:key="index">
<view class="c_item" data-index="{{index}}" catchtap="handleIHisItem">{{item}}</view>
</block>
</view>
</view>
</view>Page({
data: {
hot_list: ['杜甫', '李白', '李清照', '姜子牙', '萬事如意,心想事成'],
list: [],
search: "",//當前搜索內(nèi)容
},
onShow: function () {
if (wx.getStorageSync('search_history')) {
this.setData({
list: JSON.parse(wx.getStorageSync('search_history')).slice(0, 15)
})
}
},
handleInput(e) {
let data = e.detail.value.replace(/(^\s*)|(\s*$)/g, "");//去掉前后的空格
if (data.trim() != '') {
this.handleData(data)
}
},
handleData(e) {
this.data.list.forEach((item, index) => {
if (item == e) {
this.data.list.splice(index, 1);
}
})
this.data.list.unshift(e);
this.setData({
list: this.data.list.slice(0, 15)
})
wx.setStorageSync('search_history', JSON.stringify(this.data.list))
},
handleIHotItem(e) {
let { index } = e.currentTarget.dataset, { hot_list } = this.data;
let search = hot_list[index]
this.setData({
search,
})
// 將標簽存到歷史搜索中
this.handleData(search)
},
handleIHisItem(e) {
let { index } = e.currentTarget.dataset, { list } = this.data;
let search = list[index]
this.setData({
search: search
})
this.handleData(search)
},
// 清空輸入文字
clear_input() {
this.setData({
search: ''
})
},
//清空歷史
clearHistory() {
this.setData({
list: []
})
wx.removeStorageSync('search_history')
},
})總結(jié)
到此這篇關(guān)于微信小程序?qū)崿F(xiàn)歷史搜索功能(h5同理)的文章就介紹到這了,更多相關(guān)微信小程序歷史搜索功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS 將偽數(shù)組轉(zhuǎn)換成數(shù)組的實現(xiàn)示例
這篇文章主要介紹了JS 將偽數(shù)組轉(zhuǎn)換成數(shù)組,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
JavaScript中find()和?filter()方法的區(qū)別小結(jié)
js中find和filter方法大家在工作中會經(jīng)常遇到,那么他們有什么區(qū)別呢?這篇文章主要給大家介紹了關(guān)于JavaScript中find()和?filter()方法區(qū)別的相關(guān)資料,需要的朋友可以參考下2021-12-12
js調(diào)用百度地圖及調(diào)用百度地圖的搜索功能
本文給大家介紹js調(diào)用百度地圖的方法以及調(diào)用百度地圖的搜索功能,有需要的朋友可以跟著腳本之家的小編一起學習2015-09-09
JS中利用swiper實現(xiàn)3d翻轉(zhuǎn)幻燈片實例代碼
大家都知道Swiper(Swiper master)是目前應用較廣泛的移動端網(wǎng)頁觸摸內(nèi)容滑動js插件。下面這篇文章主要給大家介紹了在JS中利用swiper實現(xiàn)3d翻轉(zhuǎn)幻燈片的相關(guān)資料,文中給出了完整的示例代碼供大家參考學習,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08

