亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

微信小程序?qū)崿F(xiàn)搜索功能

 更新時(shí)間:2020年03月10日 15:23:17   作者:舊城tk  
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)搜索功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在頁(yè)面search.wxml中,定義一個(gè)input輸入框以及搜索的點(diǎn)擊按鈕,分別為它們綁定點(diǎn)擊事件handleInputChange()handleSearch()的事件,同時(shí)在它們的下面定義搜索的列表,代碼如下所示:

<view class="search-header">
 <input class="search-input" bindtap="handleInputChange" />
 <view class="search-btn" bindtap="handleSearch">搜索</view>
</view>

<view>
 <view wx:for="{{list}}" wx:key="{{index}}" class="item" id="{{item.id}}" bindtap="handleItemTap">
 <view>{{ item }}</view>
 <view class="item-message">{{ item.message }}</view>
 </view>
</view>

在邏輯文件search.js中,在data中存放list的數(shù)據(jù),為空數(shù)組,存放搜索列表的數(shù)據(jù),同時(shí)定義staticData,在里面定義inputValue,里面為空字符串,是input輸入框的值,代碼如下所示:

data: {
 list: []
},
staticData: {
 inputValue: ""
}

在search.js的onLoad()生命周期函數(shù)中,調(diào)用請(qǐng)求數(shù)據(jù)的函數(shù)getSearchResult(),這樣在一進(jìn)入頁(yè)面的時(shí)候就會(huì)獲取到所有的數(shù)據(jù),不過(guò)由于并沒(méi)有關(guān)鍵字keyword,需要傳空字符串,代碼如下所示:

onLoad() {
 this.getSearchResult("");
 },
getSearchResult(keyword) {
 wx.request({
  url: 'xxxxxx',
  data: {
  keyword: this.staticData.inputValue
  },
  method: "POST",
  header: {
  'content-type': 'application/x-www-form-urlencoded' 
  },
  success: this.getSearchResultSucc.bind(this)
 })
},

在search.js中,定義一個(gè)響應(yīng)成功后的函數(shù)getSearchResultSucc(),判斷響應(yīng)的數(shù)據(jù)是否存在。如果存在通過(guò)this.setData()方法將響應(yīng)后的數(shù)據(jù)賦值給list,如果不存在,list仍然為空數(shù)組,代碼如下所示:

getSearchResultSucc(res) {
 // console.log(res)
 if (res.data.ret) {
  const result = res.data.data;
  this.setData({
  list: result
  })
 } else {
  this.setData({
  list: []
  })
 }
}

在search.js中,定義handleInputChange()函數(shù),這個(gè)函數(shù)也是input輸入框綁定的函數(shù),傳入事件對(duì)象e,然后通過(guò)e.detail.value賦值給staticData的inputValue,代碼如下所示:

handleInputChange(e) {
 this.staticData.inputValue = e.detail.value;
}

在search.js中,定義handleSearch()函數(shù),這個(gè)函數(shù)也是之前搜索按鈕所綁定的函數(shù),在這個(gè)里面重新發(fā)起一次請(qǐng)求,攜帶keyword關(guān)鍵字發(fā)起請(qǐng)求,代碼如下所示:

handleSearch (keyword) {
 this.getSearchResult(keyword)
}

如果想要點(diǎn)擊在搜索以后顯示的列表,可以在列表中綁定handleItemTap()事件,傳入事件對(duì)象e,通過(guò) e.currentTaret.id去獲取到點(diǎn)擊的id,然后再通過(guò) wx.navigateTo()方法跳轉(zhuǎn)到相應(yīng)的詳情頁(yè),代碼如下所示:

handleItemTap(e) {
 wx.navigateTo({
  url: '/pages/detail/detail?id=' + e.currentTaret.id
 })
}

知識(shí)點(diǎn)補(bǔ)充:微信小程序云開(kāi)發(fā)模糊查找功能實(shí)現(xiàn)

//連接數(shù)據(jù)庫(kù)
const db = wx.cloud.database()
var that = this
db.collection(‘newsname').where({
//使用正則查詢(xún),實(shí)現(xiàn)對(duì)搜索的模糊查詢(xún)
_name: db.RegExp({
regexp: value,
//從搜索欄中獲取的value作為規(guī)則進(jìn)行匹配。
options: ‘i',
//大小寫(xiě)不區(qū)分
})
}).get({
success: res => {
console.log(res)
that.setData({
search_list: res.data
})
}
})

總結(jié)

到此這篇關(guān)于微信小程序?qū)崿F(xiàn)搜索功能的文章就介紹到這了,更多相關(guān)微信小程序搜索功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論