小程序云开发多字段模糊查询/搜索

最近做小程序云开发,做一个多字段搜索功能;
本人不会,然后找了一个很牛笔的回答!特此做笔记,以备他日所用!
看完后清楚明了!
下面是对应的跳转链接
跳转
小石头牛笔!
下面是我改后的代码!虽然大体类似
先给大家看看我做出的效果图片:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
html

<view class="woods-input">
  <input bindinput="searchinput" value="{{searchvalue}}" type="text" placeholder="请输入搜索相关文字"></input>
  <button bindtap="suredetail" type="primary" size="mini">确定</button>
</view>
<view class="woods-all">
    <view class="woods-content" wx:for="{{lists}}" wx:for-item='i' wx:key='lists' bindtap="todetail" data-img='{{i.img}}' data-name='{{i.name}}'>
      <image src="{{i.img}}"></image>
      <text>{{i.name}}</text>
    </view>
</view>

js

 data: {
 	lists: [
    ],
    searchvalue:'' 
  },
suredetail: function () {
    var that = this
    let key = that.data.searchvalue;
    console.log("查询的内容", key)
    const db = wx.cloud.database();
    const _ = db.command
    db.collection('woods-wihte').where(_.or([{
      name: db.RegExp({
        regexp: '.*' + key,
        options: 'i',
      })
    },
    {
      classification: db.RegExp({
        regexp: '.*' + key,
        options: 'i',
      })
    },
    {
      parameter: db.RegExp({
        regexp: '.*' + key,
        options: 'i',
      })
    },
    {
      remarks: db.RegExp({
        regexp: '.*' + key,
        options: 'i',
      })
    },

    ])).get({
      success: res => {
        console.log(res)
        that.setData({
          lists: res.data
        })
      },
      fail: err => {
        console.log(err)
      }
    })

  },
  //输入的value值
  searchinput:function(e){
    this.setData({
      searchvalue: e.detail.value
    })
  },
  // 将模糊查询到的数据重新渲染到页面
  todetail:function(e){
    console.log('获取列表数据', e.currentTarget.dataset.img)
      wx:wx.navigateTo({
        url: '/pages/woodsdetail/woodsdetail?img=' + e.currentTarget.dataset.img + '&name=' + e.currentTarget.dataset.name +''
      })
  },
//  页面加载,渲染数据
  onLoad: function(options) {
    var that = this
    const db = wx.cloud.database();
	//woods-wihte 是数据库的集合名称
    db.collection('woods-wihte').get({
      success: function(res) {
        console.log('返回数据:', res.data)
        that.setData({
          lists: res.data

        })
      }
    })
  },

就酱紫!哈哈哈

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值