当前位置:网站首页>【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史
2022-07-29 23:22:00 【drhrht】
???欢迎来到???
??魔术之家!!??该文章收录专栏
?-- 2022微信小程序京东商城实战 --?专栏内容
?-- 京东商城uni-app项目搭建 --?
?-- 京东商城uni-app 配置tabBar & 窗口样式 --?
?-- 京东商城uni-app开发之分包配置 --?
?-- 京东商城uni-app开发之轮播图 --?
?-- 京东商城uni-app之分类导航区域 --?
?-- 京东商城uni-app 首页楼层商品 --?
?-- 京东商城uni-app 商品分类页面(上) --?
?-- 京东商城uni-app 商品分类页面(下) --?
?-- 京东商城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; } }
}
效果
1.1 按需显示
- (搜索时只显示建议,不显示历史)
解决(添加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>
二、处理历史搜索关键词
需要做到:
- 添加关键词 (push)
- 最近时间查询的放在数组第一位(reverse,unshfit)
注意:因为列表是可变的,如果直接对列表使用
reverser()
,第二两翻转时第二个就变成倒数第二个了,原因在于你翻转之后push元素,应该在列表不变情况push,解决办法有两种,
第一种:翻转显示后,在进行push之前按,再reverse翻转回去在push
第二种:...
展开列表reverse(此时不改变原列表),此时可以在computed(计算属性,类似数据监听器和过滤器,有缓存机制)中返回reverse的值
- 建议 使用
unshift
直接添加第一项
- 去重(使用集合性质Set)
代码实现(在调取数据成功时调用一下函数)
// 添加到历史
addhistory(){
this.histortSearch.unshift(this.inputString)
// this.histortSearch.reverse()
const res = new Set(this.histortSearch) //创建set对象 需要用new
this.histortSearch = Array.from(res)
}
- 效果:
三、保存历史记录到本地
由于每次编译都会被清空,所以我们需要保存记录到本地缓存
使用
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))
}
},在
onLoad
初始化 导入本地数据onLoad() {
this.histortSearch = JSON.parse(uni.getStorageSync (‘history’) || ‘[]’) // 通过键得到值,JSON解析字符串为数组对象 不存在对象则为空数组
},
- 效果
四、按下trash键清空历史
绑定事件处理函数
clearhistory
<uni-icons type=“trash” size=“17” @click=“clearHistory”>
clearhistory
函数定义// 清空历史
clearHistory() {
this.histortSearch = []
uni.setStorageSync(‘history’,[‘’]) //必须重新赋值为空数组,只能为数组数据类型
},效果
五、点击搜索历史跳转到商品详情页
每个标签绑定
click
事件<uni-tag :text=“item” custom-style=“background-color:#f9f9f9; color: black;” inverted=“true” @click=“gotogoodslist(item)”>
定义事件函数
// 点击tag事件
gotogoodslist(item){
uni.navigateTo({
url:‘/subpackages/goods_list/goods_list?query=’ + item效果
六、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分支
?谢谢你的阅读,您的点赞和收藏就是我创造的最大动力!?
边栏推荐
- Super RVRT
- 线性表之顺序表(干货满满的分享来啦~内含顺序表全部函数代码~
- cached_network_image crashes with multiple images
- 2022年最新甘肃建筑施工焊工(建筑特种作业)模拟题库及答案解析
- 在树莓派上安装 PyCharm
- 单片机ds1302时钟程序(51单片机液晶显示程序)
- The second round of the real offer harvester~ How does the big factory inspect the candidates?(with detailed answer)
- Elementary C language - first understanding of C language
- Raspberry pie wiringPi 2.6 installed on solving gpio readall command mistakes
- DNA偶联二维过渡金属硫化物|DNA修饰贵金属纳米颗粒|使用方法
猜你喜欢
MySQL主备切换
新版微信小程序发布指南
jenkins使用维护
Qt uses QSortFilterProxyModel for sorting and filtering in QML
JetsonNano learning (5) JetsonNano installs PyTorch and Torchvision
Raspberry pie wiringPi 2.6 installed on solving gpio readall command mistakes
一个print函数,挺会玩啊?
JVM初探- 内存分配、GC原理与垃圾收集器
MQTT over QUIC:下一代物联网标准协议为消息传输场景注入新动力
437. 路径总和 III ●●
随机推荐
Super RVRT
Android 11 : 隐私和安全
很遗憾,没有一篇文章能讲清楚分布式事务
Qt之在QML中使用QSortFilterProxyModel进行排序和过滤
MySQL主备切换
[C] list explanation (headless ChanXiangFei cycle)
idea设置自动去除未引用(不再引用)的引用
华为14天-(3)内核开发
Farmers on the assembly line: I grow vegetables in a factory
Embedded system driver primary [1] - kernel module _ compilation method
【2023校招刷题】常见面试问题总结(七、常见总线协议篇)(随后续面试不断更新....)
devops学习(五) Jenkins 简单完成持续部署
设计消息队列存储消息的MySQL表格
bgp基础配置和宣告
devops学习(四) Jenkins CI 持续集成
437. 路径总和 III ●●
Brute force recursion to dynamic programming 04 (digital string conversion)
DNA脱氧核糖核酸修饰四氧化三铁|DNA修饰氧化锌|使用方法
重写并自定义依赖的原生的Bean方法
Design for failure常见的12种设计思想