minbaowang / SSeffects

some small effects in my code's history
1 stars 0 forks source link

微信小程序输入框历史搜索纪录 #7

Open minbaowang opened 5 years ago

minbaowang commented 5 years ago

微信小程序输入框历史搜索纪录

1、获取输入框的值

<input type="text" name="searchpage" bindinput='getValue' class="spage" placeholder="输入搜索内容" bindtap='search' />
//获取输入框的值
getValue(e){
var that=this;
that.setData({
search : e.detail.value//获取bindinput的值
})
}

2、点击搜索按钮将输入框的值写进缓存

<button class="btn" bindtap="goSearch">搜索</button>
//点击搜索进入搜索加入缓存记录
search(){
console.log(this.data.searchStorage)
//控制搜索历史
var self = this;
if (self.data.search != '') {
//将搜索记录更新到缓存
var searchData = self.data.searchStorage;
searchData.push({
id: searchData.length,
name: self.data.search
})
wx.setStorageSync('searchData', searchData);
self.setData({ StorageFlag: false, })
}
}

3、进来页面获取缓存,并点击输入框出现历史记录或者常在


<view class="history" wx:for="{{searchStorage}}">{{item.name}}</view>

data: { search:"", searchStorage:[], StorageFlag:false }

//打开历史记录列表 openLocationsercher: function () { this.setData({ searchStorage: wx.getStorageSync('searchData') || [], StorageFlag: true, listFlag: true, }) } /**

>2、
```js
//打开历史记录列表
  openHistorySearch: function () {
    this.setData({
      searchRecord: wx.getStorageSync('searchRecord') || [],//若无储存则为空
    })
  },
    /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.openHistorySearch()
  },

3、清除记录


<button bindtap='historyDelFn'>清空历史记录</button>

//清除历史记录 historyDelFn: function () { wx.clearStorageSync('searhRecord') this.setData({ searchRecord: [] }) },