微信小程序实现简单搜索功能

网友投稿 694 2023-11-11

本文实例为大家分享了微信小程序实现简单搜索功能的具体代码,供大家参考,具体内容如下

微信小程序实现简单搜索功能

搜索效果图

实现功能如下

(1) 未找到商品时显示提示信息,找到商品时显示商品列表

(2) 清空搜索框时显示搜索历史记录,历史记录可清除,点击历史记录赋值到搜索框

.wxml代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<view class="top">
<view class="topsearch">
<view class="frame">
<input value="{{shoopingtext}}" bindinput="shoppinginput"></input>
</view>
<text bindtap="search">搜索</text>
</view>
</view>
<view class="history" wx:if="{{history}}">
<view class="history_title">
<text>历史搜索</text>
<image src="/images/delete.png" mode="widthFix" style="width:5%;" bindtap="cleanhistory"></image>
</view>
<view class="history_text">
<text wx:for="{{newArray}}" wx:key="key" data-text="{{item}}" bindtap="textfz">{{item}}</text>
</view>
</view>
<view class="none" wx:if="{{noneview}}">
<image src="/images/null.png" mode="widthFix" style="width:20%"></image>
<text>抱歉,没有相关商品</text>
</view>
<view class=swiper_con wx:if="{{shoppinglist}}">
<view class=swiper_con_view wx:for="{{shoopingarray}}" wx:key="id" wx:if={{item.status=="1"?true:false}}>
<image src="{{item.images}}" mode="widthFix" style="width:100%" data-status="{{item.status}}"></image>
<view style="width:90%;margin:5%;">
<text style="font-size:24rpx">{{item.title}}</text>
<view class="swiper_con_view_view">
<view style="width:80%;">
<text style="font-size:24rpx;color:red;">¥{{item.money}}</text>
<text style="font-size:18rpx;color:#B8B8B8;margin-left:5%;">已售{{item.sold}}</text>
</view>
<image src="/images/cart.png" mode="widthFix" style="width:10%;position:relative;left:8%;"></image>
</view>
</view>
</view>
</view>

.wxss代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
page{
background-color: white;
}
- {
width: 100%;
background-color: #f7f7f7;
}
-search {
width: 90%;
margin-left: 5%;
display: flex;
padding: 2% 0;
align-items: center;
}
.frame {
background-color: white;
width: 75%;
border-radius: 20rpx;
padding: 0 3%;
}
.frame>input {
font-size: 24rpx;
margin: 6rpx 0;
}
-search>text {
width: 10%;
margin-left: 5%;
color: #a8a7a7fa;
}
.history {
background-color: white;
padding: 4%;
}
.history_title {
font-size: 30rpx;
display: flex;
justify-content: space-between;
color: #a8a7a7fa;
align-items: center;
}
.history_text {
padding: 4% 0;
display: flex;
flex-wrap: wrap;
}
.history_text>text {
background-color: #f7f7f7;
padding: 1% 3%;
margin: 2%;
border-radius: 40rpx;
font-size: 30rpx;
}
.history_text>text:first-child{
margin-left: 0;
}
.none{
margin-top: 10%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.swiper_con {
width: 96%;
margin-left: 2%;
display: flex;
flex-wrap: wrap;
}
.swiper_con_view {
width: 48%;
height: 530rpx;
background-color: white;
margin: 10rpx 0;
}
.swiper_con_view:nth-child(even) {
margin-left: 4%;
}
.swiper_con_view_view {
margin-top: 5%;
display: flex;
align-items: center;
}

.js代码

tips:

(1) 本篇博客为显示效果,在搜索时通过修改商品列表中的status字段值来显示商品列表,建议在实际开发过程中传入搜索框内容到后台,后台查询后返回商品列表,以免占用过多资源。

(2) 每次进入搜索页面历史记录为空,建议将搜索历史记录放入缓存中,下次进入搜索页面时显示搜索历史记录。

以上就是本文的全部内容,希望对大家的学习有所帮助

您可能感兴趣的文章:微信小程序实现搜索功能微信小程序首页的分类功能和搜索功能的实现思路及代码详解微信小程序搜索组件wxSearch实例详解微信小程序下拉框搜索功能的实现方法微信小程序实现搜索功能并跳转搜索结果页面微信小程序搜索框样式并实现跳转到搜索页面(小程序搜索功能)微信小程序实现全局搜索代码高亮的示例微信小程序实现搜索历史功能微信小程序 搜索框组件代码实例微信小程序实现搜索框功能

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序中this.data与this.setData的区别详解
下一篇:微信公众平台开发入门教程(图文详解)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~