// pages/search/search.js
const db = wx.cloud.database();//初始化数据库
Page({
data: {
//定义一个空字符串 要搜索的字符
searchVal: "",
//搜索过后商品列表
goodList: []
},
//通过inputTap方法获取输入值
input(even) {
//把输入的值设置为要搜索的字符
this.setData({
searchVal: even.detail.value
})
//console.log(searchVal)
},
//当输入框不为空的时候 显示可清除输入图片
clear: function () {
this.setData({
searchVal: ""
})
},
//商品关键字模糊搜索
search: function () {
//重新给数组赋值为空
this.setData({
goodList: []
})
// 数据库正则对象
db.collection('plant').where({
contents: db.RegExp({
regexp: this.data.searchVal,//做为关键字进行匹配
options: 'i',//不区分大小写
})
})
.get().then(res => {
console.log(res.data)
if (res.data.length == 0) {
wx.showModal({
title: '提示',
content: '暂时没找到对应答案',
showCancel: false,
success: function (res) { }
})
return;}
else
for (var i = 0; i < res.data.length; i++) {
var contents = "goodList[" + i + "].contents"
var id = "goodList[" + i + "].id"
var img_url = "goodList[" + i + "].img_url"
var price = "goodList[" + i + "].price"
var buy_num = "goodList[" + i + "].buy_num"
this.setData({
[contents]: res.data[i].contents,
[id]: res.data[i]._id,
[img_url]: res.data[i].img_url,
[price]: res.data[i].price,
[buy_num]: res.data[i].buy_num,
})
//console.log(this.data.goodList[i].contents)
}
}).catch(err => {
console.error(err)
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var that = this
//输出其他页面传来的值
//console.log(options.searchVal)
if (this.data.searchVal != ' ') {
//console.log(searchVal)
this.setData({
searchVal: that.data.searchVal
})
this.search();
} else {
//that.search();
}
},
buyBtn3(){
wx.switchTab({
url: '../xiadan/xiadan',
})}
})
你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)