国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 熱點(diǎn) > 微信 > 正文

微信小程序結(jié)合Storage實(shí)現(xiàn)搜索歷史效果

2024-07-22 01:18:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)搜索歷史效果的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)目標(biāo)

代碼實(shí)現(xiàn)

集合wx.setStorageSync()和wx.getStorageSync()這兩個(gè)同步函數(shù)來(lái)實(shí)現(xiàn)這個(gè)功能實(shí)際上非常簡(jiǎn)單。

<!-- wxml --><view class="search-box"> <view class='icon'>  <image src='../../assets/search.png' mode='widthFix'></image>  <!-- 使用bindinput屬性綁定getSearchKey函數(shù)獲取input組件中的值-->  <!-- 使用bindblur屬性綁定routeToSearchResPage函數(shù)處理input失去焦點(diǎn)事件-->  <input placeholder='搜索你想購(gòu)買的商品' bindinput='getSearchKey' bindblur='routeToSearchResPage'></input> </view> <text>取消</text></view><view class='options'> <text>歷史搜索記錄</text> <text bindtap='clearHistory'>清空</text></view><view class='options'><!-- 遍歷 history 數(shù)組 --> <text class='item' wx:for='{{history}}' data-index='{{index}}' bindtap='routeToSearchResPage'>{{item}}</text></view>

樣式表 可無(wú)視

/* wxss */.search-box { background-color: #142341; overflow: hidden; padding: 3%;}.search-box .icon { width: 80%; padding-left: 2%; background-color: #fff; float: left; border-radius: 1rem;}.search-box .icon image { width: 1rem; height: 1rem; display: block; margin: 0.5rem 0; float: left;}.search-box input { display: block; font-size: 0.8rem; height: 2rem; line-height: 2rem; float: left; margin-left: 5%;}.search-box text { width: 18%; float: left; color: #fff; line-height: 2rem; text-align: center; font-size: 0.8rem;}.options { width: 94%; margin: 3%; font-size: 0.8rem; color: #999;}.options text:last-child { color: #1268bb; float: right;}.options .item { padding: 0.2rem 0.5rem; background-color: #eee; float: left !important; color: #565656 !important; border-radius: 0.1rem; margin: 3%;}

JavaScript

//index.jsPage({ data: {  searchKey: "",  history: [] }, //獲取input文本 getSearchKey: function(e) {  this.setData({   searchKey: e.detail.value  }) }, // 清空page對(duì)象data的history數(shù)組 重置緩存為[] clearHistory: function() {  this.setData({   history: []  })  wx.setStorageSync("history", []) }, // input失去焦點(diǎn)函數(shù) routeToSearchResPage: function(e) {  //對(duì)歷史記錄的點(diǎn)擊事件 已忽略  let _this = this;  let _searchKey = this.data.searchKey;  if (!this.data.searchKey) {   return  }  let history = wx.getStorageSync("history") || [];  history.push(this.data.searchKey)  wx.setStorageSync("history", history); }, //每次顯示鉤子函數(shù)都去讀一次本地storage onShow: function() {  this.setData({   history: wx.getStorageSync("history") || []  }) }})            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 库车县| 盈江县| 阳信县| 和林格尔县| 卫辉市| 班玛县| 崇阳县| 林口县| 临沧市| 三门县| 小金县| 桑日县| 信宜市| 始兴县| 油尖旺区| 呼伦贝尔市| 颍上县| 称多县| 林西县| 波密县| 武邑县| 丹巴县| 千阳县| 丘北县| 景洪市| 桑植县| 临海市| 吉首市| 罗定市| 灵山县| 北宁市| 宣化县| 皋兰县| 神农架林区| 铜梁县| 太谷县| 镇赉县| 胶州市| 高雄县| 乌什县| 阜阳市|