微信小程序实现搜索商品和历史记录的功能

本文主要基于微信小程序实现和uni-app实现搜索商品和历史记录的功能。 不详细介绍,主看代码注释即可!!

1、搜索组件页面代码块

<template>
  <view>
    <!-- uni的搜索框 -->
    <view class="search-box">
      <uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="searchResults.length !== 0">
      <view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="gotoDatail(item)">
        <view class="goos-name"> {{item.goods_name}} </view>
        <uni-icons type="arrowright" size="17" ></uni-icons>
      </view>
    </view>
    <!-- 搜索历史盒子 -->
    <view class="history-box" v-else>
      <!-- 历史标题区域 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
      </view>
      <!-- 历史记录列表区域 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="(item,i) in historyList" :key="i"></uni-tag>
      </view>
    </view>
  </view>
</template>

页面实现效果图如下图:

2、样式代码块

<style lang="scss">
  .search-box {
    position: sticky; //搜索框黏性定位
    top: 0;
    z-index: 999;
    
    .uni-searchbar {
      background-color: #C00000 !important;
    }
  }
//搜索列表样式
  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      display: flex;
      align-items: center;
      justify-content: space-between; //两端对其
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #EEEEEE;

      .goos-name {
        white-space: nowrap; // 文字不允许换行
        overflow: hidden;
        text-overflow: ellipsis; //文本超出内容用。。。隐藏
      }
    }
  }
//搜索历史样式
  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between; //两侧对齐
      height: 40px;
      align-items: center;
      font-size: 13px;
      border: 1px solid #efefef;

      .history-list {
        display: flex;
        flex-wrap: wrap;

        .uni-tag {
          margin: 0 2px;
        }
      }
    }
  }
</style>

3、逻辑代码块

<script>
  export default {
    data() {
      return {
        timer: null, //接收定时器
        kw: '', //input的最新值
        searchResults: [], // 搜索的结果列表
        historyList: [], // 搜索历史的数组
      };
    },
    onLoad() { // 页面开始加载获取本地搜索历史存储数据
     this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]') //页面加载获取搜索历史
    },
    methods: {
      input(e) { // input 输入事件的处理函数
        // console.log(e) //可以拿到最新的值
        clearTimeout(this.timer) // 清除定时器
        // 节流处理
        this.timer = setTimeout(() => { //开启定时器
          // console.log(e)
          this.kw = e // 输入框最新值 赋值给kw
          this.getSearchList() // 调用获取搜索
        }, 500)
      },
      // 获取搜索联想建议方法
      async getSearchList() {
      // 判断input的值是否为空
        if (this.kw.length === 0) {
          this.searchResults = [] // 清空搜索结果
          return // 停止执行
        }
        // 获取搜索结果
        const {
          data: res
        } = await uni.$http.get('/api/.....', {
          query: this.kw
        })
        // 判断是否成功获取数据
        if (res.meta.status !== 200) return uni.$showMsg()
        // 获取成功把结果赋值
        this.searchResults = res.message
        this.saveSearchHistory() // 调用保存搜索历史记录
      },
      // 搜索联想列表详细页跳转方法
      gotoDatail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
      // 保存搜索历史记录并持久化历史搜索方法
      saveSearchHistory() { 
      // 查找搜索历史结果数组中,重复的搜索
        const index = this.historyList.findIndex(x => x === this.kw) // 返回结果  -1代表没有
        // 判断是否大于0 大于等于存在
        if (index >= 0) {
        // 删除找到记录
          this.historyList.splice(index, 1)
        }
        // 把input新值向数组开头添加
        this.historyList.unshift(this.kw)
        //持久化搜索历史
        uni.setStorageSync('kw', this.historyList)
      },
      // 清空搜索历史记录方法
      cleanHistory() { 
        // 清空 data 中保存的搜索历史
        this.historyList = []
        // 清空本地存储中记录的搜索历史
        uni.setStorageSync('kw', '[]')
      }
    }
  }
</script>

以上就是实现小程序搜索功能,历史记录功能的实现,当然这也是一种实现思路方法,没有完全正确的。

希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 微信小程序实现搜索功能

    在页面search.wxml中,定义一个input输入框以及搜索的点击按钮,分别为它们绑定点击事件handleInputChange()和handleSearch()的事件,同时在它们的下面定义搜索的列表,代码如下所示: <view class="search-header"> <input class="search-input" bindtap="handleInputChange" /> <view class

  • 微信小程序下拉框搜索功能的实现方法

    最近在做一个项目的时候,需要用到下拉框搜索,网上搜了半天没找到想要的,决心自己动手写一个. 首先看下效果图: 左边是输入框,可以进行模糊查询,右边图标进行选择. 代码部分,我定义了五个参数,和一个自定义的方法, list:下拉框数组, _width:组件宽度, _height:组件高度, bind:action: 自定义方法 考虑到每个人用的时候用的对象数组结构不同我预留了两个字段,做自定义(可不写),怎么样是不是很强大. actualvalue:下拉框实际值, showvalue:下拉框显示值

  • 微信小程序搜索框样式并实现跳转到搜索页面(小程序搜索功能)

    上效果图: 一:搜索框功能实现 1.在首页做一个搜索框的样式并实现跳转到搜索页面 <view class='page_row' bindtap="suo"> <view class="search"> <view class="df search_arr"> <icon class="searchcion" size='20' type='search'></icon>

  • 微信小程序 搜索框组件代码实例

    这篇文章主要介绍了微信小程序 搜索框组件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码如下 search.wxml <view class="header"> <view class="search"> <icon type="search" size="18" color=""> </icon>

  • 微信小程序实现全局搜索代码高亮的示例

    需求 最近在做微信小程序的时候,需要实现在搜索框的输入内容的时候实现全局匹配实现高亮效果,目前的思路是,递归后台来返回的数据,并将对象的value值替换为需要的dom节点,并且通过rich-text来实现,高亮效果. 代码 wxml: <view class='homePage'> <input bindinput="bindKeyInput"></input> <view wx:for="{{newJson}}" wx:f

  • 微信小程序首页的分类功能和搜索功能的实现思路及代码详解

    就在昨天,微信宣布了微信小程序开发者工具新增"云开发"功能 下载最新的开发者工具,现在无需服务器即可实现小程序的快速迭代! 分类功能和搜素功能的效果图 1.首页分类功能的实现 boxtwo方法(.js文件) boxtwo: function (e) { var index = parseInt(e.currentTarget.dataset.index) this.setData({ HomeIndex: index }) }, 当在首页点击 分类导航时,会触发这个方法,并传回当前点击

  • 微信小程序实现搜索历史功能

    结合了微信给的资料,马马虎虎摸索出了一些东西,下面说下一下微信小程里序搜索历史功能的实现,下图是实现效果. 首先,定义历史记录的显示风格和方式,在下用的是列表模式,没有使用什么比较酷炫的套路. <view wx:for="{{sercherStorage}}" wx:key="item.id"> <view class="liclass" style="color:#9E9E9E;border-bottom:0;fon

  • 微信小程序实现搜索框功能

    本文实例为大家分享了微信小程序实现搜索框功能的具体代码,供大家参考,具体内容如下 效果: wxml文件: <view class="search_input" > <navigator url="/pages/search/search" open-type="navigate" class="navigator"> <text class="iconfont icon-guanbi&q

  • 微信小程序实现搜索功能并跳转搜索结果页面

    本文实例为大家分享了微信小程序实现搜索功能,并跳转搜索结果页面,供大家参考,具体内容如下 搜索页面: search.wxml页面: <view class="form"> <input class="searchInput" value='{{keyWord}}' bindconfirm='goSearch' placeholder="请输入搜索关键字" type="text" /> </view

  • 微信小程序搜索组件wxSearch实例详解

    wxSearch优雅的微信小程序搜索框 一.功能 支持自定义热门key 支持搜索历史 支持搜索建议 支持搜索历史(记录)缓存 二.使用 1.将wxSearch文件夹整个拷贝到根目录下 2.引入 // wxml中引入模板 <import src="/wxSearch/wxSearch.wxml"/> <template is="wxSearch" data="{{wxSearchData}}"/> // wxss中引入 @i

随机推荐