微信小程序 历史记录和长按清除

历史记录

wxml代码:

<view class="boxseace">
	<view class="seaces">
		<input bindconfirm="clickes" placeholder="{{sous}}" placeholder-class="dos" confirm-type="done" bindfocus="jujiao" bindblur="shijiao" value="{{values}}"></input>
	</view>
</view>


<view class="tag" hidden="{{show}}">
  <!--  标签页面 -->
  <view class="doubans"><text>搜索记录</text></view>
	<view class="doubanq" catchtap="qchc"><text>清除缓存</text></view>
  <view class="labelbox">
    <view wx:for="{{listes}}" wx:key="key" class="laelitem" bindlongpress="deleteImage" data-idx="{{index}}" catchtap="dianjishijian">{{item}}</view>
  </view>
</view>


<view wx:for="{{rewq.data.subjects}}" wx:key="key" class="box" data-index="{{index}}" catchtap="clicklist">
	<view class="boxs">
		<image src="{{item.pic.normal}}"></image>
	</view>
	<view class="box1">
		<view class="box2">{{item.title}}</view>
		<grade value="{{item.rating.value}}" onoff="{{true}}"></grade>
    <view class="box3">{{item.card_subtitle}}</view>
	</view>
</view>

js代码:

var list=[];
var temporary="";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sous:"搜索",                /**input内的字符 */
    rewq:{},                  /**跳转准备的参数 */
    listes:[],                /**缓存的内容 */
    show:false,                  /**控制是否显示/隐藏 */
    values:''
  },
  jujiao:function(res){                     /**input聚焦的回调 */
    // console.log("聚焦",res);
    this.setData({
      show:true
    })
  },
  shijiao:function(res){                    /**input失焦的回调 */
    // console.log("失焦",res);
    if(temporary == ""){
      this.setData({
        show:false
      })
    }else{
    this.setData({
      show:true
    })
  }
  },
  clicklist:function(res){                          /**跳转传参 */
    // console.log("点击事件res.currentTarget.dataset.index是点击的哪个的数",res);
    var i = parseInt(res.currentTarget.dataset.index);
    console.log(i);
    var moves={
      type:this.data.rewq.data.subjects[i].type,
      id:this.data.rewq.data.subjects[i].id
    }
    console.log("要传递的电影详细信息",moves);
    var model = encodeURIComponent(JSON.stringify(moves));
    wx.navigateTo({
      url: "/index/details/details?model="+model,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    console.log("mores",mores);
    // console.log("rewq",this.data.rewq);
  }, 
  
  clickes:function(e){                                        /***文本框完成触发函数 */
    // console.log("触发事件",e.detail.value);
    temporary = e.detail.value;
    var ques = e.detail.value;
    if(ques == "" || ques == " "){
    }else{
      list.push(ques);
    };
    wx.setStorage({                                           /**缓存 */
      data: list,
      key: 'key',
    })
    wx.getStorage({                                           /**读取缓存 */
      key: 'key',
      success(res) {
        // console.log("getStorage", res)
        that.setData({
          listes: res.data
        })
      }
    })

    var that = this;  
    wx.request({                                                              /**搜索触发api */
      url: 'https://m.douban.com/rexxar/api/v2/search',
      data: {
        start: 0,
        count:10,
        q:ques
      },
      header: {},
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        console.log("成功的返回值:", res);
        that.setData({
          rewq: res
        })
        // console.log(that.data.rewq);
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  qchc:function(){                                        /**清除缓存api */
    wx.clearStorage({
      success:function(res){
        // console.log(res);
        list=[];
      }
    }),
    this.setData({
      listes:null
    })
  },
  deleteImage:function(res){                            /**长按事件 */
    // console.log("长按事件de  xia  biao",res.currentTarget.dataset.idx);
    var i = res.currentTarget.dataset.idx;
    wx.getStorage({                                           /**读取缓存 */
      key: 'key',
      success:(res)=> {
        // console.log("getStorage", res)
        list = res.data;
        // console.log("数组",res);
        list.splice(i,1);
        this.setData({
          listes: list          /**** ------------------------------------------- */
        })
        wx.setStorage({                                           /**缓存 */
          data: list,
          key: 'key',
        })
      }
    })
  },
  dianjishijian:function(res){                          /**点击事件 */
    console.log("点击事件",res);
    var i = res.currentTarget.dataset.idx;
    var that = this;
    wx.getStorage({                                           /**读取缓存 */
      key: 'key',
      success:(res)=> {
        var ques = res.data[i];
        // console.log("getStorage", ques);
        that.setData({
          values:ques
        })
      }
        })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var arr =["1515554165","sahdsahshds","85131","我的","撒娇","试试","的规划","是撒会"];
    arr.splice(2,1);               //从start的位置开始向后删除delCount个元素
    console.log(arr)
  },

你可能感兴趣的:(清除缓存,长按清除)