Commit 340b4e27e0994126c5a175c8a9c7e5be886c68d6
1 parent
134c16eb
1. 搜索添加回车搜索
2. 最新搜索的关键字要放在前面 3. 要去掉关键字搜索后面的空格
Showing
2 changed files
with
30 additions
and
7 deletions
pages/goods/search/search.js
| ... | ... | @@ -101,6 +101,7 @@ Page({ |
| 101 | 101 | //扫一扫过来,显示搜索的内容 |
| 102 | 102 | var s_key = t.s_key; |
| 103 | 103 | if (s_key){ |
| 104 | + s_key=s_key.trim(); | |
| 104 | 105 | this.search(s_key); this.openSearchModal(); |
| 105 | 106 | return; |
| 106 | 107 | } |
| ... | ... | @@ -190,10 +191,16 @@ Page({ |
| 190 | 191 | }); |
| 191 | 192 | }, |
| 192 | 193 | submitSearch: function(t) { |
| 193 | - console.log(111); | |
| 194 | - this.search(t.detail.value.word); | |
| 195 | - | |
| 196 | - | |
| 194 | + console.log(111,t); | |
| 195 | + var val=t.detail.value.word; | |
| 196 | + if(!val) val=t.detail.value; | |
| 197 | + val=val.trim(); | |
| 198 | + if(!val){ | |
| 199 | + a.showWarning("请输入搜索关键词"); | |
| 200 | + } | |
| 201 | + | |
| 202 | + this.search(val); | |
| 203 | + | |
| 197 | 204 | }, |
| 198 | 205 | |
| 199 | 206 | //热搜关键字 |
| ... | ... | @@ -282,13 +289,29 @@ Page({ |
| 282 | 289 | |
| 283 | 290 | historyRecord() { |
| 284 | 291 | let self = this; |
| 285 | - let keyword = this.data.key_str; | |
| 292 | + let keyword = this.data.key_str.trim(); | |
| 286 | 293 | let searchRecord = this.data.searchRecord; |
| 287 | 294 | if(!searchRecord || searchRecord.length==0) searchRecord=wx.getStorageSync('searchRecord') || []; |
| 288 | 295 | |
| 289 | 296 | if(keyword) { |
| 290 | - if(searchRecord.indexOf(keyword) > -1) { | |
| 297 | + | |
| 298 | + var index=searchRecord.indexOf(keyword); | |
| 299 | + if(index > -1) { | |
| 291 | 300 | // 已经存在该值 |
| 301 | + searchRecord.splice(index,1); | |
| 302 | + searchRecord.unshift(keyword); | |
| 303 | + | |
| 304 | + // 将历史记录添加到缓存中 | |
| 305 | + wx.setStorage({ | |
| 306 | + key: 'searchRecord', | |
| 307 | + data: searchRecord, | |
| 308 | + success: function(res) { | |
| 309 | + self.setData({ | |
| 310 | + searchRecord, | |
| 311 | + }) | |
| 312 | + } | |
| 313 | + }) | |
| 314 | + | |
| 292 | 315 | return; |
| 293 | 316 | } else { |
| 294 | 317 | if(searchRecord.length >= 10) { | ... | ... |
pages/goods/search/search.wxml
| ... | ... | @@ -162,7 +162,7 @@ |
| 162 | 162 | <view class="search-modal"> |
| 163 | 163 | <view class="search-bar"> |
| 164 | 164 | <form bindsubmit="submitSearch"> |
| 165 | - <input autoFocus class="search-input" name="word" placeholder="搜索商品" bindinput="getInput"></input> | |
| 165 | + <input autoFocus class="search-input" name="word" placeholder="搜索商品" bindconfirm="submitSearch" bindinput="getInput"></input> | |
| 166 | 166 | <button class="search-btn" formType="submit"> |
| 167 | 167 | <image class="wh100 search-img" src="{{url}}/miniapp/images/sea.png"></image> |
| 168 | 168 | </button> | ... | ... |