微信小程序开发实现搜索功能
对于搜索都是会有一些需要的,所以搜索页面还是可以复用的。主页面就是下面这样:
这个页面在pages/components/component2/component2.wxml
点击页面中 黄色的input就可以跳转到真正的搜索页面:pages/components/component2/search/search.wxml
搜索页面中也是有个input搜索框,旁边有个小叉号,可以清除input里的文字。
下面主要讲下search页面的逻辑:其实也非常简单。
搜索input绑定bindInput函数,
bindInput:function(e){
this.setData({
inputValue:e.detail.value
})
console.log(\'bindInput\'+this.data.inputValue)
},
将输入的值存在inputValue中,搜索button 用bindtap绑定setSearchStorage函数
setSearchStorage:function(){
let data;
let localStorageValue = [];
if(this.data.inputValue != \'\'){
//调用API从本地缓存中获取数据
var searchData = wx.getStorageSync(\'searchData\') || []
searchData.push(this.data.inputValue)
wx.setStorageSync(\'searchData\', searchData)
wx.navigateTo({
url: \'../result/result\'
})
}else{
console.log(\'空白的你搜个蛋!\')
}
// this.onLoad();
},
这个函数主要就是先判断输入的值是否不为空,再通过getStorageSync获取到key为searchData的localStorage,
如果第一次还没有set过这个key就获取[],再将用户inputValue存的想要搜索的值放进searchData,之后再跳转到result页面。这里我只放了个案例页面。
如果在真正的生产环境中,这个函数可以通过wx.request向服务器发送请求,再把数据放进result页面中,实现真正的搜索功能。
删除inputValue的button功能实现也很简单,setData将inputValue设置为空字符串就可以了。
放点击result页面左上角的返回时,你就可以发现,你刚才搜索的结果已经放到了search的页面中。
当你想要删除缓存数据的时候,可以点击清空浏览记录按钮,会弹出个对话框:
点击确认删除之后,会自动刷新页面(重定向到本页面),将之前的key为searchData的localStorage重置为空数组。
modalChangeConfirm:function(){
wx.setStorageSync(\'searchData\',[])
this.setData({
modalHidden:true
})
wx.redirectTo({
url: \'../search/search\'
})
},
这里的清除不是应用wx.clearStorage()删除的,以为clearStorage会将所有的localStorage都删掉,慎用!这样,搜索的功能就做好了!
一:搜索框功能实现
1.在首页做一个搜索框的样式并实现跳转到搜索页面
[html] view plain copy
- <view class='page_row' bindtap="suo">
- <view class="search">
- <view class="df search_arr">
- <icon class="searchcion" size='20' type='search'></icon>
- <input class="" disabled placeholder="请输入关键字" value="{{searchValue}}"/>
- </view>
- </view>
- <view class='sousuo'>搜索</view>
- </view>
[css] view plain copy
- .search{
- width: 80%;
- }
- .search_arr {
- border: 1px solid #d0d0d0;
- border-radius: 10rpx;
- margin-left: 20rpx;
- }
- .search_arr input{
- margin-left: 60rpx;
- height: 60rpx;
- border-radius: 5px;
- }
- .bc_text {
- line-height: 68rpx;
- height: 68rpx;
- margin-top: 34rpx;
- }
- .sousuo {
- margin-left: 15rpx;
- width: 15%;
- line-height: 150%;
- text-align: center;
- border: 1px solid #d0d0d0;
- border-radius: 10rpx;
- }
- .page_row{
- display: flex;
- flex-direction: row
- }
- .searchcion {
- margin: 10rpx 10rpx 10rpx 10rpx;
- position: absolute;
- left:25rpx;
- z-index: 2;
- width: 20px;
- height: 20px;
- text-align: center;
- }
js.点击跳转到搜索的页面
[javascript] view plain copy
- suo: function (e) {
- wx.navigateTo({
- url: '../search/search',
- })
- },
2.搜索页面实现搜索功能
[html] view plain copy
- <!--pages/search/search.wxml-->
- <view class="search page_row">
- <input class="df_1" placeholder="请输入你有搜索的内容" value="{{searchValue}}" bindinput="searchValueInput" />
- <button bindtap="suo" data-id='1'>
- 媒婆
- </button>
- <button bindtap="suo" data-id='2'>
- 单身
- </button>
- </view>
- <view class="search_no" wx:if="{{!centent_Show}}">
- <text>很抱歉,没有找到您要搜索的资料/(ㄒoㄒ)/~~</text>
- </view>
- <import src="../index/card/card.wxml" />
- <template is="nanshen_card" data="{{nanshen_card,img}}" />
[javascript] view plain copy
[javascript] view plain copy
- var app = getApp();
- var searchValue =''
- // pages/search/search.js
- Page({
- data: {
- centent_Show: true,
- searchValue: '',
- img: '',
- nanshen_card:''
- },
- onLoad: function () {
- },
- searchValueInput: function (e) {
- var value = e.detail.value;
- this.setData({
- searchValue: value,
- });
- if (!value && this.data.productData.length == 0) {
- this.setData({
- centent_Show: false,
- });
- }
- },
- suo:function(e){
- var id= e.currentTarget.dataset.id
- var program_id = app.program_id;
- var that = this;
- wx.request({
- url: 'aaa.php',//这里填写后台给你的搜索接口
- method: 'post',
- data: { str: that.data.searchValue, program_id: program_id, style:id },
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- success: function (res) {
- if(res.data.length ==0){
- that.setData({
- centent_Show: false,
- });
- }
- that.setData({
- nanshen_card: res.data,
- });
- },
- fail: function (e) {
- wx.showToast({
- title: '网络异常!',
- duration: 2000
- });
- },
- });
- }
- });
[css] view plain copy
- /* pages/search/search.wxss */
- @import "../index/card/card";
- .searchcion{
- width: 24px;
- height: 24px;
- text-align: center;
- margin-top: 5rpx
- }
- .search{
- padding: 1% 3%;
- background: #D0D0D0;
- }
- .search input{
- width: 85%;
- border-radius: 5px;
- background: #fff;
- border: none;
- font-size: 12px;
- padding:1% 2.5%;
- margin-right: 5px;
- }
- .search button{
- line-height:30px;
- text-align: center;
- border: none;
- font-size: 28rpx;
- background: white
- }