微信小程序云開發(fā)實(shí)現(xiàn)搜索功能
微信小程序云開發(fā)實(shí)現(xiàn)搜索功能,供大家參考,具體內(nèi)容如下
微信小程序使用云開發(fā)實(shí)現(xiàn)搜索功能有兩種情況,一種是簡單的搜索用關(guān)鍵字來查詢數(shù)據(jù),另一種是模糊查詢關(guān)于關(guān)鍵字的全部數(shù)據(jù)查詢。廢話不用多說直接上代碼部分。
簡單搜索功能實(shí)現(xiàn)
WXML代碼段
<view class="sousuokuang"> ? ? <view class="sousuo"> ? ? ? ? <view class="shurukuang"> ? ? ? ? ? ? <input type="text" placeholder="搜索" value="" bindinput="GetSearchInput"></input> ? ? ? ? </view> ? ? ? ? <view class="sousuo_anniu" bindtap="ToSearch"> ? ? ? ? ? ? <text>搜索</text> ? ? ? ? ? ? <icon type="search" size="20"></icon> ? ? ? ? </view> ? ? </view> </view>
WXSS代碼段
.sousuokuang {
? ? width: 100%;
? ? height: 100rpx;
? ? display: flex;
? ? flex-direction: column;
? ? align-items: center;
? ? background-color: white;
}
.sousuo {
? ? width: 92%;
? ? height: 100rpx;
? ? display: flex;
? ? flex-direction: row;
? ? align-items: center;
? ? justify-items: center;
}
.shurukuang {
? ? width: 80%;
? ? height: 64rpx;
? ? border-radius: 32rpx;
? ? display: flex;
? ? align-items: center;
? ? justify-content: center;
? ? background-color: #f6f6f6;
}
.shurukuang input {
? ? width: 90%;
? ? height: 100%;
? ? font-size: 32rpx;
}
.sousuo_anniu {
? ? width: 20%;
? ? height: 64rpx;
? ? display: flex;
? ? align-items: center;
? ? justify-content: center;
}
.sousuo_anniu text {
? ? font-size: 30rpx;
}JavaScript代碼段
const db = wx.cloud.database()
Page({
? ? data: {
? ? ? ? search: ''
? ? },
? ? onLoad: function (options) {
? ? },
? ? GetSearchInput: function(e) {
? ? ? ? this.setData({
? ? ? ? ? ? search: e.detail.value
? ? ? ? })
? ? },
? ? ToSearch: function(e) {
? ? ? ? if(this.data.search == '') {
? ? ? ? ? ? wx.showToast({
? ? ? ? ? ? ? title: '請(qǐng)輸入',
? ? ? ? ? ? ? icon: 'none'
? ? ? ? ? ? })
? ? ? ? ? ? return
? ? ? ? }
? ? ? ? db.collection('輸入你查詢的表名').where({
? ? ? ? ? ? name: this.data.search
? ? ? ? }).get().then(res => {
? ? ? ? ? ? if(res.data.length != 0) {
? ? ? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? ? ? shangpinbiao: res.data
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? wx.showToast({
? ? ? ? ? ? ? ? ? title: '未找到商品',
? ? ? ? ? ? ? ? ? icon: 'none'
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? })
? ? },
})模糊搜索功能實(shí)現(xiàn)
WXML代碼段和WXSS代碼段跟簡單搜索的一樣,JavaScript代碼段如下
Javascript代碼段
const db = wx.cloud.database()
Page({
? ? data: {
? ? ? ? search: ''
? ? },
? ? onLoad: function (options) {
? ? },
? ? GetSearchInput: function (e) {
? ? ? ? this.setData({
? ? ? ? ? ? search: e.detail.value
? ? ? ? })
? ? },
? ? ToSearch: function (e) {
? ? ? ? if (this.data.search == '') {
? ? ? ? ? ? wx.showToast({
? ? ? ? ? ? ? ? title: '請(qǐng)輸入',
? ? ? ? ? ? ? ? icon: 'none'
? ? ? ? ? ? })
? ? ? ? ? ? return
? ? ? ? }
? ? ? ? db.collection('輸入你查詢的表名').where({
? ? ? ? ? ? name: db.RegExp({
? ? ? ? ? ? ? ? regexp: this.data.search,
? ? ? ? ? ? ? ? options: 'i',
? ? ? ? ? ? }),
? ? ? ? }).get().then(res => {
? ? ? ? ? ? if (res.data.length != 0) {
? ? ? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? ? ? shangpinbiao: res.data
? ? ? ? ? ? ? ? })
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? wx.showToast({
? ? ? ? ? ? ? ? ? ? title: '未找到',
? ? ? ? ? ? ? ? ? ? icon: 'none'
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? })
? ? },
})
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序?qū)崿F(xiàn)搜索功能
- 微信小程序首頁的分類功能和搜索功能的實(shí)現(xiàn)思路及代碼詳解
- 微信小程序搜索組件wxSearch實(shí)例詳解
- 微信小程序下拉框搜索功能的實(shí)現(xiàn)方法
- 微信小程序?qū)崿F(xiàn)搜索功能并跳轉(zhuǎn)搜索結(jié)果頁面
- 微信小程序搜索框樣式并實(shí)現(xiàn)跳轉(zhuǎn)到搜索頁面(小程序搜索功能)
- 微信小程序?qū)崿F(xiàn)全局搜索代碼高亮的示例
- 微信小程序?qū)崿F(xiàn)搜索歷史功能
- 微信小程序 搜索框組件代碼實(shí)例
- 微信小程序 (地址選擇1)--選取搜索地點(diǎn)并顯示效果
相關(guān)文章
ES6中Class類的靜態(tài)方法實(shí)例小結(jié)
這篇文章主要介紹了ES6中Class類的靜態(tài)方法,結(jié)合實(shí)例形式簡單總結(jié)分析了ES6中class類中靜態(tài)方法的使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-10-10
使用JS實(shí)現(xiàn)氣泡跟隨鼠標(biāo)移動(dòng)的動(dòng)畫效果
氣泡跟隨鼠標(biāo)移動(dòng),并在每次點(diǎn)擊時(shí)產(chǎn)生不同的變化,效果非常迷人,下面小編給大家?guī)砹?,基于js實(shí)現(xiàn)的氣泡效果實(shí)例代碼,需要的朋友參考下吧2017-09-09
JavaScript 數(shù)組循環(huán)引起的思考
Google array.js 程序中建議改進(jìn)的代碼。思考為什么i-- 到了 i為0時(shí)while循環(huán)結(jié)束了?2010-01-01

