微信小程序?qū)崿F(xiàn)歷史搜索功能的全過(guò)程(h5同理)
1.實(shí)現(xiàn)效果
2.實(shí)現(xiàn)原理
微信小程序中將數(shù)據(jù)存在storage中,除非用戶(hù)手動(dòng)觸發(fā),否則不會(huì)過(guò)期,同理,若想在瀏覽器中實(shí)現(xiàn),只需將數(shù)據(jù)存在localStorage中即可。
將數(shù)據(jù)存儲(chǔ)在本地緩存中指定的 key 中。會(huì)覆蓋掉原來(lái)該 key 對(duì)應(yīng)的內(nèi)容。除非用戶(hù)主動(dòng)刪除或因存儲(chǔ)空間原因被系統(tǒng)清理,否則數(shù)據(jù)都一直可用。單個(gè) key 允許存儲(chǔ)的最大數(shù)據(jù)長(zhǎng)度為 1MB,所有數(shù)據(jù)存儲(chǔ)上限為 10MB。 存儲(chǔ)的內(nèi)容,只支持原生類(lèi)型、Date、及能夠通過(guò)
JSON.stringify
序列化的對(duì)象
注意:wx.setStorage是異步的,wx.setStorageSync是同步的,即要等待執(zhí)行完才會(huì)去執(zhí)行其他的代碼
將已搜索的數(shù)據(jù)存在本地緩存中。
wx.setStorageSync('search_history', JSON.stringify(this.data.list))
選擇存在本地緩存中的前15條數(shù)據(jù)顯示在頁(yè)面中。
if (wx.getStorageSync('search_history') ){ this.setData({ list:JSON.parse(wx.getStorageSync('search_history') ).slice(0, 15) }) }
每次選擇歷史數(shù)據(jù)的時(shí)候,將選擇的數(shù)據(jù)移到數(shù)組的第一條。
this.data.list.unshift(data);
點(diǎn)擊'清空歷史'按鈕,wx.removeStorage清空存在本地緩存中的歷史記錄列表。
從本地緩存中移除指定 key,是wx.removeStorage 的同步版本。
3.實(shí)現(xiàn)步驟
- 定義熱門(mén)搜索列表,搜索歷史列表,搜索名稱(chēng)
hot_list:['杜甫','李白','李清照','姜子牙','萬(wàn)事如意,心想事成'],//熱門(mén)搜索 list:[],//搜索歷史列表 search: "",//當(dāng)前搜索內(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進(jìn)數(shù)組的頭部,并截取前15個(gè)存進(jìn)本地緩存中
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)) },
- 點(diǎn)擊熱門(mén)搜索中的數(shù)據(jù),添加事件,如果選中的數(shù)據(jù)已存于歷史列表中,先刪除,然后unshift進(jìn)數(shù)組的頭部,并截取前15個(gè)存進(jìn)本地緩存中
handleIHotItem(e) { let { index } = e.currentTarget.dataset, { hot_list } = this.data; let search = hot_list[index] this.setData({ search, }) // 將標(biāo)簽存到歷史搜索中 this.handleData(search) },
- 點(diǎn)擊搜索歷史列表中的數(shù)據(jù),添加事件,如果選中的數(shù)據(jù)已存于歷史列表中,先刪除,然后unshift進(jìn)數(shù)組的頭部,并截取前15個(gè)存進(jìn)本地緩存中
handleIHisItem(e) { let { index } = e.currentTarget.dataset, { list } = this.data; let search = list[index] this.setData({ search: search }) this.handleData(search) },
- 點(diǎn)擊'清空歷史'按鈕,添加清空事件
clearHistory() { this.setData({ list:[] }) wx.removeStorageSync('search_history') },
4.實(shí)現(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">熱門(mén)搜索</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: ['杜甫', '李白', '李清照', '姜子牙', '萬(wàn)事如意,心想事成'], list: [], search: "",//當(dāng)前搜索內(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, }) // 將標(biāo)簽存到歷史搜索中 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS 將偽數(shù)組轉(zhuǎn)換成數(shù)組的實(shí)現(xiàn)示例
這篇文章主要介紹了JS 將偽數(shù)組轉(zhuǎn)換成數(shù)組,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07如何寫(xiě)出一個(gè)驚艷面試官的JavaScript深拷貝
淺拷貝是面試中經(jīng)常會(huì)被問(wèn)到的問(wèn)題,這篇文章主要給大家介紹了關(guān)于如何寫(xiě)出一個(gè)驚艷面試官的JavaScript深拷貝的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05微信小程序多表聯(lián)合查詢(xún)的實(shí)現(xiàn)詳解
小程序設(shè)計(jì)中,通常會(huì)根據(jù)業(yè)務(wù)來(lái)做多表的拆分,多表拆分一般是根據(jù)業(yè)務(wù)的特點(diǎn)進(jìn)行拆分。比如我們?cè)谖恼玛P(guān)注的業(yè)務(wù)中,會(huì)將文章和關(guān)注信息拆分成一對(duì)多的表關(guān)系。初學(xué)者可能對(duì)一對(duì)一、一對(duì)多、多對(duì)多的設(shè)計(jì)概念不是特別清楚2022-08-08JavaScript中find()和?filter()方法的區(qū)別小結(jié)
js中find和filter方法大家在工作中會(huì)經(jīng)常遇到,那么他們有什么區(qū)別呢?這篇文章主要給大家介紹了關(guān)于JavaScript中find()和?filter()方法區(qū)別的相關(guān)資料,需要的朋友可以參考下2021-12-12js調(diào)用百度地圖及調(diào)用百度地圖的搜索功能
本文給大家介紹js調(diào)用百度地圖的方法以及調(diào)用百度地圖的搜索功能,有需要的朋友可以跟著腳本之家的小編一起學(xué)習(xí)2015-09-09又一款js時(shí)鐘!transform實(shí)現(xiàn)時(shí)鐘效果
又一款js時(shí)鐘!這篇文章主要為大家詳細(xì)介紹了transform實(shí)現(xiàn)的時(shí)鐘效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08JS中利用swiper實(shí)現(xiàn)3d翻轉(zhuǎn)幻燈片實(shí)例代碼
大家都知道Swiper(Swiper master)是目前應(yīng)用較廣泛的移動(dòng)端網(wǎng)頁(yè)觸摸內(nèi)容滑動(dòng)js插件。下面這篇文章主要給大家介紹了在JS中利用swiper實(shí)現(xiàn)3d翻轉(zhuǎn)幻燈片的相關(guān)資料,文中給出了完整的示例代碼供大家參考學(xué)習(xí),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08