上个页面过滤筛选
一般Vue的话使用模糊查询很简单很方便,但是小程序要如何实现呢
这里讲解的是无后台时过滤关键字进行查询搜索
如果想添加请求后台可以直接添加请求的数据就可以了
wxml
<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="../../img/icon/login/icon_close_orange.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
page{
background-color: white;
}
.top {
width: 100%;
background-color: #f7f7f7;
}
.topsearch {
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;
}
.topsearch>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
var util = require('../../utils/server'),
header = util.reqHeader();
Page({
//清除历史记录
cleanhistory: function(e) {
this.setData({
history: false, //隐藏历史记录
historyArray: [], //清空历史记录数组
newArray: [],
shoopingtext: "" //清空搜索框
})
},
//搜索
search: function(e) {
var searchtext = this.data.shoopingtext; //搜索框的值
var sss = true;
if (searchtext != "") {
//将搜索框的值赋给历史数组
this.data.historyArray.push(searchtext);
//模糊查询 循环查询数组中的title字段
for (var index in this.data.shoopingarray) {
var num = this.data.shoopingarray[index].title.indexOf(searchtext);
let temp = 'shoopingarray[' + index + '].status';
if (num != -1) { //不匹配的不显示
this.setData({
[temp]: 1,
})
sss = false //隐藏未找到提示
}
}
this.setData({
history: false, //隐藏历史记录
noneview: sss, //隐藏未找到提示
shoppinglist: true, //显示商品列表
newArray: this.data.historyArray //给新历史记录数组赋值
})
} else {
this.setData({
noneview: true, //显示未找到提示
shoppinglist: false, //隐藏商品列表
history: false, //隐藏历史记录
})
}
},
data: {
shoopingtext: "", //搜索框的值
history: false, //显示历史记录
noneview: false, //显示未找到提示
shoppinglist: false, //显示商品列表
historyArray: [], //历史记录数组,
newArray: [], //添加历史记录数组
shoopingarray:[],
},
//搜索框的值
shoppinginput: function(e) {
//当删除input的值为空时
if (e.detail.value == "") {
this.setData({
history: true, //显示历史记录
shoppinglist: false //隐藏商品列表
});
//所有商品列表的状态改为0
for (var index in this.data.shoopingarray) {
let temp = 'shoopingarray[' + index + '].status';
this.setData({
[temp]: 0,
})
}
}
this.setData({
shoopingtext: e.detail.value
})
},
onLoad: function (options) {
var that = this
util.sendRrquest('https://cnodejs.org/api/v1/topics', 'get','data', header)
.then(function (res) {
console.log(res)
that.setData({
shoopingarray: res.data.data
//然后在使用的时候这样使用
})
}, function (error) {
console.log(error);
})
},
//点击历史记录赋值给搜索框
textfz: function(e) {
this.setData({
shoopingtext: e.target.dataset.text
})
}
})
json
{
"usingComponents": {
"van-popup": "@vant/weapp/popup/index"
},
"navigationBarTitleText": "小组"
}
我这里运用了vant框架写的样式 调取的是cnode接口