搜索历史记录方法(本地存储)

本地存储(Storage)
 
 
初始化本地存储:
  onLoad: function (options) {
    let that = this
    wx.getStorage({
      key: 'searchlist',
      success: function (res) {
        console.log(res)
        that.setData({
          searchhistory:res.data
        })
      },
      fail(e){
        let searcharry = []
        wx.setStorage({
          key: 'searchlist',
          data: searcharry
        })
      }
    })
  }

  根据input点击:

  1.修改本地存储

  2.初始化存储

  3.进行重复字检索和空字符排除

  4.待优化:查询到初始化存储失败,重新进行初始化并进行检索、排空并进行存储

  最下面是删除:

  1.删除本地存储并进行重新初始化

  inputvalue:function(e){
    let that = this,
    value = e.detail.value

    wx.getStorage({
      key: 'searchlist',
      success: function(res) {
        let listarrey = res.data,
        searchS = listarrey.indexOf(value)
        if (searchS == -1 && value != ""){
          listarrey.push(value)
          wx.setStorage({
            key: 'searchlist',
            data: listarrey
          })
          wx.getStorage({
            key: 'searchlist',
            success: function(res) {
              that.setData({
                searchhistory: res.data
              })
            }
          })
        }
      },fail(e){
        let searcharry = []
        wx.setStorage({
          key: 'searchlist',
          data: searcharry
        })
      }
    })
}
  deletehistory:function(){
    let that = this
    wx.removeStorage({
      key: 'searchlist',
      success(res){
        let searcharry = []
        wx.setStorage({
          key: 'searchlist',
          data: searcharry
        })
        that.setData({
          searchhistory: searcharry
        })
      }
    })
  }

猜你喜欢

转载自www.cnblogs.com/Everythingisobject/p/12769720.html
今日推荐