From 53bd290da362c529611d242ffbee0b08543621c2 Mon Sep 17 00:00:00 2001 From: yvan.ni Date: Wed, 23 Jun 2021 17:11:32 +0800 Subject: [PATCH] 小程序自定义搜索框希望能搜索历史的 --- components/diy_searchbox/diy_searchbox.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- components/diy_searchbox/diy_searchbox.wxml | 28 ++++++++++++++-------------- components/diy_searchbox/diy_searchbox.wxss | 2 +- 3 files changed, 70 insertions(+), 26 deletions(-) diff --git a/components/diy_searchbox/diy_searchbox.js b/components/diy_searchbox/diy_searchbox.js index a6a6014..ef93b4c 100644 --- a/components/diy_searchbox/diy_searchbox.js +++ b/components/diy_searchbox/diy_searchbox.js @@ -19,6 +19,8 @@ Component({ inputShowed:1, inputval:'在店铺内搜索', keyword:'', + + searchRecord: [], }, ready: function() { var th=this; @@ -27,7 +29,27 @@ Component({ if(e.hot_keywords) th.setData({keyword:e.hot_keywords}); }) + + var arr=wx.getStorageSync('searchRecord') || [] //若无存储则为空 + + this.setData({ + searchRecord: arr + }); + + }, + + + pageLifetimes:{ + //要处理一下,游客登录后的界面的变化,主要还该是改变会员 + show: function () { + var th=this; + var arr=wx.getStorageSync('searchRecord') || [] //若无存储则为空 + this.setData({ + searchRecord: arr,inputval:'',first:1,is_his:0 + }); + } }, + methods: { //智能跳转 go_url:function (e) { @@ -41,17 +63,23 @@ Component({ return false; } this.setData({first:0,inputShowed:1,inputval:''}); - - - - - }, + + }, + inputFocus:function(){ + if(this.data.is_init) return false; + this.setData({is_his:1,}) + }, - onblur:function(){ - var val=this.data.inputval; - if(val==''){ - this.setData({first:1,}); - } + onblur:function(){ + var th=this; + setTimeout(function(){ + if(th.data.click_sear) return false; + var val=th.data.inputval; + if(val==''){ + th.setData({first:1,}); + } + th.setData({is_his:0,}) + },300) }, set_input:function(e){ var val=e.detail.value; @@ -60,7 +88,23 @@ Component({ sear:function(){ var url="/pages/goods/search/search?s_key="+this.data.inputval; getApp().goto(url); - } + }, + + del_his:function(){ + this.setData({ + searchRecord: [] + }); + wx.setStorage({ key: 'searchRecord', data:[]}); + }, + + sear_key:function(e){ + this.data.click_sear=1; + var str=e.currentTarget.dataset.item; + var url="/pages/goods/search/search?s_key="+str; + this.data.click_sear=0; + getApp().goto(url); + + }, } }) \ No newline at end of file diff --git a/components/diy_searchbox/diy_searchbox.wxml b/components/diy_searchbox/diy_searchbox.wxml index 9637569..11956e2 100644 --- a/components/diy_searchbox/diy_searchbox.wxml +++ b/components/diy_searchbox/diy_searchbox.wxml @@ -3,7 +3,7 @@ - + 在店铺内搜索 @@ -13,14 +13,14 @@ - - - 2121 - 2121 - 2121 - 清除 + + + {{item}} + + 清除 @@ -45,20 +45,20 @@ - - - 2121 - 2121 - 2121 - 清除 + + + {{item}} + + 清除 - + diff --git a/components/diy_searchbox/diy_searchbox.wxss b/components/diy_searchbox/diy_searchbox.wxss index 523f371..0c5a08d 100644 --- a/components/diy_searchbox/diy_searchbox.wxss +++ b/components/diy_searchbox/diy_searchbox.wxss @@ -140,7 +140,7 @@ .item { padding: 0 20rpx; border-bottom: 2rpx solid #f0f0f0; - line-height: 60rpx; + line-height: 60rpx; text-align: left; } /* .item ~ .item { border-top: 2rpx solid #f0f0f0; -- libgit2 0.21.4