【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史

简介: 【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史

文章目录

一、搜索历史的基本结构

  • 在data 定义数据 存贮搜索历史
  data() {
      return {
        // 输入数据
        inputString: '',
        // 延时器
        timer: null,
        // 搜索建议
        searchSuggest: '',
        // 搜索历史
        histortSearch: ['a','apple','money']
      };
    },
  • 渲染UI结构
 <!-- 搜索历史容器 -->
    <view class="history-list-container">
      <!-- 标题区域 -->
      <view class="history-head-box">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-item-container">
        <view class="history-item" v-for="(item,i) in histortSearch" :key="i">
          <uni-tag :text="item" custom-style="background-color:#f9f9f9; color: black;" inverted="true"></uni-tag>
        </view>
      </view>
    </view>
  • 美化样式
  .history-list-container {
    .history-head-box {
      display: flex;
      justify-content: space-between;
      padding: 18rpx;
      border-bottom: 3rpx solid #ebebeb;
      font-size: 28rpx;
      align-items: center;
    }
    .history-item-container {
      display: flex;
      .history-item {
        margin: 10rpx;
      }
    }
  }
  • 效果图片.png

1.1 按需显示

  • (搜索时只显示建议,不显示历史)

图片.png

  • 解决(添加block 判断条件)
    <!-- 搜索建议列表容器 -->
    <block v-if="inputString.length != 0">
      <view class="sgg-list-container">
        <navigator class="sgg-item" v-for="(product,i) in searchSuggest" v-bind:key="i"
          :url="'/subpackages/goods_detail/goods_detail?cat_id=' + product.goods_id">
          <view class="sgg-name">{{product.goods_name}}</view>
          <uni-icons type="right"></uni-icons>
        </navigator>
      </view>
    </block>
    <!-- 搜索历史容器 -->
    <block v-if="inputString.length === 0">
      <view class="history-list-container">
        <!-- 标题区域 -->
        <view class="history-head-box">
          <text>搜索历史</text>
          <uni-icons type="trash" size="17"></uni-icons>
        </view>
        <!-- 列表区域 -->
        <view class="history-item-container">
          <view class="history-item" v-for="(item,i) in histortSearch" :key="i">
            <uni-tag :text="item" custom-style="background-color:#f9f9f9; color: black;" inverted="true"></uni-tag>
          </view>
        </view>
      </view>
    </block>

图片.png

二、处理历史搜索关键词

需要做到:

  1. 添加关键词 (push)
  2. 最近时间查询的放在数组第一位(reverse,unshfit)

注意:因为列表是可变的,如果直接对列表使用reverser(),第二两翻转时第二个就变成倒数第二个了,原因在于你翻转之后push元素,应该在列表不变情况push,解决办法有两种,

第一种:翻转显示后,在进行push之前按,再reverse翻转回去在push

第二种:...展开列表reverse(此时不改变原列表),此时可以在computed(计算属性,类似数据监听器和过滤器,有缓存机制)中返回reverse的值

  • 建议 使用unshift直接添加第一项
  1. 去重(使用集合性质Set)

代码实现(在调取数据成功时调用一下函数)

// 添加到历史
      addhistory(){
        this.histortSearch.unshift(this.inputString)
        // this.histortSearch.reverse()
        const res = new Set(this.histortSearch) //创建set对象 需要用new
        this.histortSearch = Array.from(res)
      }
  • 效果:39.gif

三、保存历史记录到本地

由于每次编译都会被清空,所以我们需要保存记录到本地缓存

  1. 使用 uni.setStorageSync(键,值) 将数据存贮在本地
  // 添加到历史
      addhistory() {
        this.histortSearch.unshift(this.inputString)
        // this.histortSearch.reverse()
        const res = new Set(this.histortSearch) //创建set对象 需要用new
        this.histortSearch = Array.from(res)
        // 将存贮数据存贮在本地
        uni.setStorageSync('history', JSON.stringify(this.histortSearch))
      }
    },
  1. 在onLoad 初始化 导入本地数据
onLoad() {
   this.histortSearch = JSON.parse(uni.getStorageSync ('history') || '[]') // 通过键得到值,JSON解析字符串为数组对象 不存在对象则为空数组 
 },
  • 效果

四、按下trash键清空历史

  • 绑定事件处理函数clearhistory
<uni-icons type="trash" size="17" @click="clearHistory"></uni-icons>
  • clearhistory函数定义
// 清空历史
  clearHistory() {
    this.histortSearch = []
    uni.setStorageSync('history',['']) //必须重新赋值为空数组,只能为数组数据类型
  },
  • 效果
  • 40.gif

五、点击搜索历史跳转到商品详情页

  • 每个标签绑定click事件
<uni-tag :text="item" custom-style="background-color:#f9f9f9; color: black;" inverted="true" @click="gotogoodslist(item)"></uni-tag>
  • 定义事件函数
 // 点击tag事件
gotogoodslist(item){
    uni.navigateTo({
      url:'/subpackages/goods_list/goods_list?query=' + item
  • 效果

41.gif

六、search分支的提交

  • git branch 查看分支
  • git add . 提交到暂存区
  • git commit -m "完成了search的开发" 提交到本地仓库
  • git push origin -u search 提交到远程仓库的search分支
  • git checkout master 切换到主分支
  • git merge search 合并search分支
  • git push 提交到远程仓库主分支
  • git branch -d search 删除search分支

图片.png

✨谢谢你的阅读,您的点赞和收藏就是我创造的最大动力!✨



相关文章
|
缓存 小程序 前端开发
商城/点餐/家政类小程序源码合集_微信抖音小程序源码开发从入门到精通实战
本文系统讲解如何利用现有源码快速开发商城、点餐、家政类微信/抖音小程序,涵盖环境搭建、核心功能实现、多平台部署与优化,提供完整技术方案。实战导向,助力开发者高效入门与落地。
|
人工智能 监控 小程序
【快递鸟】选择对接你的物流商城/小程序的物流API平台
在电商竞争日益激烈的今天,物流体验已成为影响用户留存和复购的关键因素。一个高效、透明、稳定的物流系统,对于物流商城或小程序来说至关重要。然而,自建物流查询系统需要对接众多快递公司,开发周期长、维护成本高、数据整合困难。
781 0
|
小程序 机器人 开发者
QQ 小程序已发布,但无法被搜索的解决方案
我的 QQ 小程序在 2024 年 8 月就已经审核通过,上架后却一直无法被搜索到。打开后,再在 QQ 上下拉查看 “最近使用”,发现他出现一下又马上消失。
658 2
|
小程序 前端开发 JavaScript
在线课堂+工具组件小程序uniapp移动端源码
在线课堂+工具组件小程序uniapp移动端源码
595 0
在线课堂+工具组件小程序uniapp移动端源码
|
小程序
微信小程序动态tabBar实现:基于自定义组件,灵活支持不同用户角色与超过5个tab自由组合(更新版)
微信小程序动态tabBar实现:基于自定义组件,灵活支持不同用户角色与超过5个tab自由组合(更新版)
3465 1
|
移动开发 小程序
thinkphp+uniapp开发的多端商城系统源码/H5/小程序/APP支持DIY模板直播分销
thinkphp+uniapp开发的多端商城系统源码/H5/小程序/APP支持DIY模板直播分销
1124 0
|
移动开发 Android开发 UED
商城APP开发主要采用哪些方式?
通常手机商城APP定制开发采用的都是Android原生技术+H5混合技术开发的,为什么要采用两种语言混合开发?其实就是考虑到手机商城APP的一些功能特点,这样就可以充分的提取两项开发技术中的优点了。
394 0
|
缓存 移动开发 JavaScript
如何优化UniApp开发的App的启动速度?
如何优化UniApp开发的App的启动速度?
1999 139
|
移动开发 JavaScript weex
UniApp开发的App在启动速度方面有哪些优势和劣势?
UniApp开发的App在启动速度方面有哪些优势和劣势?
953 137