微信小程序搜索

作者: 白衣诗人 | 来源:发表于2019-04-11 10:44 被阅读0次
    <view class="app pad">
      <form class='start' bindsubmit="formSubmit">
        <view class='flex justify-content-between'>
          <view class='search'>
            <input type='text' class='search-input' name="search" placeholder='大头菜' value='{{inputValue}}' bindinput='inputBind' />
            <image src="/imgs/icon37.png" class="search-image" mode="widthFix"/>
          </view>
          <view class='search-bind' data-name='{{inputValue}}' bindtap='bindSearch'>搜索</view>
        </view>
      </form>
      <view class='end'>
        <view class='end-list'>
          <view class='end-h flex justify-content-between align-items-center'>
            <view class='end-p flex justify-content-start align-items-center'>
              <image src="/imgs/icon38.png" mode="widthFix"/>
              最近搜索
            </view>
            <image class='del' src="/imgs/icon40.png" mode="widthFix" bindtap='bindDel' />
          </view>
          <view class='end-ul'>
            <view class='end-li' wx:for="{{lately}}" wx:key="{{index}}" data-name='{{item}}' bindtap='bindSearch'>{{item}}</view>
          </view>
        </view>
        <view class='end-list'>
          <view class='end-h flex justify-content-between align-items-center'>
            <view class='end-p flex justify-content-start align-items-center'>
              <image src="/imgs/icon39.png" mode="widthFix" />
              热门搜索
            </view>
          </view>
          <view class='end-ul'>
            <view class='end-li' wx:for="{{hot}}" wx:key="{{index}}" data-name='{{item}}' bindtap='bindSearch'>{{item}}</view>
          </view>
        </view>
      </view>
      <!-- 模糊搜索展示列表 -->
      <view class='vague' wx:if="{{vague.length>0}}" style='height:{{windowHeight}}px;'>
        <view class='vague-list' wx:for="{{vague}}" wx:key="{{index}}" data-name='{{item.name}}' bindtap='bindSearch' >
          {{item.name}}
        </view>
      </view>
    
    
      <!-- 搜索结果 -->
      
      <view class='list-li pad' wx:if="{{list.length>0}}" style='height:{{windowHeight}}px;'>
        <view class='index-shap' wx:for="{{list}}" wx:key="{{index}}">
          <navigator class=' flex justify-content-start ' url='../../index/detail/index?id={{item.id}}'>
              <view class='index-shap-l flex justify-content-center align-items-center'>
                <image src='{{item.img}}' mode='widthFix'></image>
              </view>
              <view class='index-shap-r'>
                <view class='h1 flex justify-content-between align-items-center'>
                  {{item.title}}
                  <view>月售{{item.number}}</view>
                </view>
                <view class='h2'>{{item.article}}</view>
                <view class='p'>{{item.weight}}</view>
                <view class='span flex justify-content-start align-items-center'>
                  ¥{{item.price}}
                  <view class='em'>¥{{item.money}}</view>
                </view>
              </view>
          </navigator>
          <image src='/imgs/icon-add.png' mode='widthFix' class='shapAdd' bindtap='bindAdd' />
        </view>
      </view>
    
    </view>
    
    
    
    .app{
      padding-top: 120rpx;
    
    }
    .start{
      position: fixed;
      top: 0;
      left:0;
      width: 100%;
      height: 120rpx;
      padding: 20rpx;
      box-sizing: border-box;
      background:  #fff;
      z-index: 100;
    }
    .search{
      width: 588rpx;
      height: 80rpx;
      border-radius: 6rpx;
      background: #eeeeee;
      position: relative;
    }
    .search-input{
      width: 100%;
      height: 100%;
      border: 0;
      background: transparent;
      padding: 0 16rpx 0 76rpx;
      box-sizing: border-box;
      font-size: 28rpx;
      color: #313131;
    }
    .search-input::-webkit-input-placeholder{
      color: #b5b5b5;
    }
    .search-image{
      width: 28rpx;
      position: absolute;
      left: 30rpx;
      top: 50%;
      transform: translateY(-50%);
      z-index: 10;
    }
    .search-bind{
      width: 120rpx;
      height: 80rpx;
      line-height: 80rpx;
      text-align: center;
      color: #313131;
      font-size: 32rpx;
    }
    
    /* 最近搜索 */
    .end-h{
      height: 58rpx;
      width: 100%;
      
    }
    .end-p{
      height: 100%;
      font-size: 30rpx;
      color: #b5b5b5;
    }
    .end-p image{
      width: 28rpx;
      margin-right: 14rpx;
    }
    .del{
      width: 24rpx;
    }
    .end-ul{
      margin-top: 15rpx;
    }
    .end-li{
      height: 80rpx;
      line-height: 80rpx;
      border-radius: 80rpx;
      font-size: 30rpx;
      color: #313131;
      padding: 0 40rpx;
      background: #eeeeee;
      display: inline-block;
      margin-right: 30rpx;
      margin-bottom: 25rpx;
    }
    
    /* 模糊搜索列表 */
    .vague{
      width: 100%;
      background: #fff;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 50;
      overflow: hidden;
      overflow-y: scroll;
      padding-top: 120rpx;
      box-sizing: border-box;
    }
    .vague-list{
      width: 100%;
      height: 80rpx;
      line-height: 80rpx;
      border-bottom: 1rpx solid #eee;
      box-sizing: border-box;
      padding: 0 20rpx;
      font-size: 30rpx;
      color: #313131;
    }
    .vague-list:first-child{
      border-top: 1rpx solid #eee;
    }
    
    
    
    /* 搜索结果 */
    .list-box{
      width: 100%;
      background: #fff;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 10;
      overflow: hidden;
      overflow-y: scroll;
      padding-top: 120rpx;
      box-sizing: border-box;
    }
    
    .list{
      width: 100%;
      height: 198rpx;
      overflow: hidden;
      padding: 20rpx;
      box-sizing: border-box;
      box-shadow:2.5rpx 4.33rpx 14.88rpx 1.12rpx rgba(255, 100, 69, 0.22);
      margin-top: 20rpx;
      background: #fff;
    }
    .list-img{
      width: 158rpx;
      height: 158rpx;
      overflow: hidden;
    }
    .list-img image{
      width: 100%;
    }
    .list-r{
      width: 490rpx;
    }
    .list-r-p{
      line-height: 30rpx;
    }
    .list-r-h{
      font-size: 28rpx;
      color: #333333;
      width: 370rpx;
    }
    .list-r-em{
      font-size: 24rpx;
      color: #fa361b
    }
    .list-r-span{
      font-size: 24rpx;
      color: #888888;
      width: 100%;
      line-height: 30rpx;
      margin-top: 9rpx;
      height: 70rpx;
    }
    .list-r-i{
      font-size: 30rpx;
      color: #fa361b;
    }
    .list-r-i view{
      font-size: 24rpx;
      color: #888888;
    }
    
    
    .list-li{
      width: 100%;
      background: #fff;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 6;
      overflow: hidden;
      overflow-y: scroll;
      padding-top: 120rpx;
      box-sizing: border-box;
    }
    .index-shap{
      width: 100%;
      height: 296rpx;
      position: relative;
      padding: 35rpx 0 35rpx;
      box-sizing: border-box;
      border-bottom: 1rpx solid #eee;
    }
    .index-shap-l{
      width: 240rpx;
      height: auto;
      overflow: hidden;
    }
    .index-shap-l image{
      width: 180rpx;
    }
    .shapAdd{
      width: 36rpx;
      position: absolute;
      right: 30rpx;
      bottom: 48rpx;
    }
    .index-shap-r{
      width: 470rpx;
    }
    .h1{
      color: #1b1b1b;
      font-size: 32rpx;
      height: 38rpx;
    }
    .h1 view{
      font-size: 20rpx;
      color: #bfbfbf;
    }
    .h2{
      font-size: 24rpx;
      color: #a0a0a0;
      line-height: 38rpx;
      margin-top: 10rpx;
    }
    .p{
      color: #a0a0a0;
      font-size: 22rpx;
      display: inline-block;
      height: 36rpx;
      line-height: 36rpx;
      background: #eeeeee;
      padding: 0 10rpx;
      box-sizing: border-box;
      margin-top: 9rpx;
    }
    .span{
      color: #ff6e17;
      font-size: 30rpx;
      height: 38rpx;
      margin-top: 38rpx;
    }
    .em{
      display: inline-block;
      font-size: 24rpx;
      color: #a0a0a0;
      text-decoration:line-through;
      padding-left: 10rpx;
    }
    
    
    // pages/index/search/index.js
    Page({
    
      /**
       * 页面的初始数据
       */
      data: {
        /**最近搜索 */
        lately:"",
        /**热门搜索 */
        hot: "",
        /**搜索内容 */
        inputValue:"",
        /**模糊搜索返回数据 */
        vague:"",
        /**屏幕高度 */
        windowHeight:"",
        /**搜索结果 */
        list:[],
      },
    
      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
        var that = this,
          /**返回最近搜索和热门搜索 */
          lately = ['木门', '地板', '窗户', '合金门', '厨具'],
          hot = ['木门', '地板', '窗户', '合金门', '木门', '地板', '窗户', '厨具'],
          /**获取当前屏幕高度 */
          height = wx.getSystemInfoSync().windowHeight;
        that.setData({
          windowHeight: height,
          lately: lately,
          hot:hot,
        })
      },
    
      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function () {
    
      },
    
      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
    
      },
    
      /**
       * 生命周期函数--监听页面隐藏
       */
      onHide: function () {
    
      },
    
      /**
       * 生命周期函数--监听页面卸载
       */
      onUnload: function () {
    
      },
    
      /**
       * 页面相关事件处理函数--监听用户下拉动作
       */
      onPullDownRefresh: function () {
    
      },
    
      /**
       * 页面上拉触底事件的处理函数
       */
      onReachBottom: function () {
    
      },
    
      /**
       * 用户点击右上角分享
       */
      onShareAppMessage: function () {
    
      },
      /**
       * 输入内容,实现模糊查询
       */
      inputBind:function(e){
        var that = this,
          value = e.detail.value;
        this.search(value);
        that.setData({
          inputValue:value,
        })
      },
    
      search(val){
        var that = this;
       var vagueList = [""];
        if (val) {
          vagueList = [
            {
              name: '精品儿菜',
            }, {
              name: '精品金针菇',
            }, {
              name: '精品豇豆',
            }, {
              name: '精品豆芽',
            }, {
              name: '精品莲藕',
            }];
        } else {
          vagueList = val;
        }
        that.setData({
          vague: vagueList,
        })
      },
      /**
       * 最近搜索和热门搜索
       */
      bindSearch:function(e){
        var that = this;
        var inputValue = e.currentTarget.dataset.name;//搜索框内容
    
        var vague = [];
        that.setData({
          inputValue: inputValue,
          vague: vague,
        });
        this.formSubmit(inputValue);
      },
      /**
       * 搜索
       */
    
      formSubmit:function(e){
        var that = this;
        var list = [
          {
            id: "0",
            img: "/imgs/capping.png",
            title: "精品菜头",
            article: "野生自然 无添加剂",
            weight: "100g/份",
            price: "9.00",
            number:"2000",
            money: "10.00",
            num: "1"
          }, {
            id: "1",
            img: "/imgs/capping.png",
            title: "精品菜头",
            article: "野生自然 无添加剂",
            weight: "100g/份",
            price: "9.00",
            number: "2000",
            money: "10.00",
            num: "1"
          }, {
            id: "2",
            img: "/imgs/capping.png",
            title: "精品菜头",
            article: "野生自然 无添加剂",
            weight: "100g/份",
            number: "2000",
            price: "9.00",
            money: "10.00",
            num: "1"
          }
        ];
    
        that.setData({
          list:list
        })
      },
      /**
     * 添加购物车
     */
      bindAdd: function (e) {
        var that = this;
        wx.showModal({
          title: '提示',
          content: '是否将本产品加入购物车',
          success: function (res) {
            if (res.confirm) {
              wx.showToast({
                title: '添加成功',
                icon: 'success',
                duration: 1000
              })
            }
          }
        })
      },
      bindDel:function(e){
        var that = this;
        var lately = this.data.lately;
        wx.showModal({
          title: '提示',
          content: '是否确定删除最近搜索',
          success:function(res){
            if(res.confirm){
              lately = [];
              that.setData({
                lately: lately
              });
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 1000
              })
            }
          }
        })
       
      }
    
    })
    

    相关文章

      网友评论

        本文标题:微信小程序搜索

        本文链接:https://www.haomeiwen.com/subject/pigziqtx.html