实现功能:点击搜索框,有搜索记录时以下拉菜单显示,点击下拉子菜单,将数据赋值到搜索框,点击搜索图标搜索,支持清空历史记录,可手动输入和清空查询关键字,

UI:

wxml:

<!--查询历史记录数据-->
<view class="ddclass" style="margin-left: 50rpx;z-index:80" hidden="{{!StorageFlag}}" style="z-index:100">
  <view wx:for="{{sercherStorage}}" wx:key="item.id">
    <view class="liclass" style="color:#ec9e14;border-bottom:0;" id="{{item.id}}" bindtap="tapSercherStorage">
      <text style="width:100rpx">{{item.name}}</text>
    </view>
  </view>
  <view wx:if="{{sercherStorage.length!==0}}" style="text-align:center;" bindtap="clearSearchStorage">
    <text style="text-align:center;color:red;font-size:28rpx">清空历史记录</text>
  </view>
</view>

wxss:

/*二级菜单外部容器样式*/
   .ddclass {
 position: absolute;
 width: 100%;
 margin-top: 10px;
 left: 0;
 
}

/*二级菜单普通样式*/
 
 .liclass {
 font-size: 14px;
 line-height: 34px;
 color: #575757;
 height: 34px;
 display: block;
 padding-left: 18px;
 background-color: #fff;
 border-bottom: 1px solid #dbdbdb;
}
 
/*二级菜单高亮样式*/
 
 li.highlight {
 background-color: #f4f4f4;
 color: #48c23d;
}

js:

data:{

sercherStorage: [],

inputValue: "",             //搜索框输入的值

StorageFlag: false,         //显示搜索记录标志位

}

//获取输入框的输入信息
  bindInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

//清楚输入框数据
  clearInput:function(){
    this.setData({
      inputValue: ""
    })
  },
  //清楚缓存历史并关闭历史搜索框
  clearSearchStorage: function () {
    wx.removeStorageSync('searchData')
    this.setData({ sercherStorage: [],
      StorageFlag: false,  })
  },
  //点击缓存搜索列表
  tapSercherStorage:function(e)
{
    var that = this;
    var index = parseInt(e.currentTarget.id);
    for (var j = 0; j < that.data.sercherStorage.length; j++) {
      if (j == index) {
        //将所选的搜索历史加到搜素框
        this.setData({
          inputValue: that.data.sercherStorage[j].name,
          StorageFlag: false,   
        })
      }}
    if (this.data.inputValue != '') {
    //请求搜索记录
    }

},  
//打开历史记录列表
  openLocationsercher:function(e)
  {
    this.setData({
      sercherStorage: wx.getStorageSync('searchData') || [],   //调用API从本地缓存中获取数据
      StorageFlag: true,
      listFlag: true,
    })
  },
//添加搜索记录并搜索
  setSearchStorage: function () {
    //let data;
    var that=this;
  //let localStorageValue = [];
    if (this.data.inputValue != '') {
      //将搜索记录更新到缓存
      var searchData = that.data.sercherStorage;
      searchData.push({
        id: searchData.length,
        name: that.data.inputValue})
      wx.setStorageSync('searchData', searchData);
      that.setData({ StorageFlag: false,})
      
      //请求搜索
      /*wx.request({
        url: '',
        data: {SercherValue:that.data.inputValue,
            SercherTypeNum:that.data.SercherTypeNum,
            typeItem:that.data.typeItem },
        header: {},
        method: '',
        dataType: '',
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })*/
      //wx.navigateTo({
      //  url: '../result/result'
     // })
      // console.log('马上就要跳转了!')
    } else {
      console.log('空白的你搜个jb')
    }
    // this.onLoad();
  },

最新文章

  1. MySQL使用技巧收集,持续更新中......
  2. ios中图片拉伸用法
  3. javascript 正则表达式使用
  4. Django 的开始之路
  5. appt查看apk信息
  6. SAP中主数据和单据的删除
  7. CSS中应用position的absolute和relative的属性制作浮动层
  8. Linux学习小结(转)
  9. Android develop tricks——整理自国外的一些Blog
  10. (转载)Linux下IPTABLES配置详解
  11. JMeter使用记录1 -- JDBC測试
  12. Nginx 负载均衡-加权轮询策略剖析
  13. WPF学习(3)布局
  14. (中等) HDU 3416 Marriage Match IV,SPFA+SAP。
  15. SQL server 存储过程的建立和调用
  16. RTX临界段,中断锁与任务锁
  17. oracle 11g rac asm磁盘组增加硬盘
  18. caffe matlab接口编译遇到的问题记录
  19. 关于ubuntu14.04,忘记root密码解决方案(经测试,内核3.13和4.2可行)。
  20. c 常见错误

热门文章

  1. nagios-plugins安装报错--with-mysql: no
  2. Python中的staticmethod和classmethod
  3. 10分钟看懂, Java NIO 底层原理
  4. php xmlrpc使用示例
  5. sed 和awk结合取ip 地址
  6. spring 配置bean-自己主动装配
  7. 应用索引技术优化SQL 语句(转)
  8. C ~ 指针的运算
  9. 使用git连接到Github
  10. Proftpd mysql认证配置文档