欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

<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ù),同時定義staticData,在里面定義inputValue,里面為空字符串,是input輸入框的值,代碼如下所示:

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

在search.js的onLoad()生命周期函數(shù)中,調(diào)用請求數(shù)據(jù)的函數(shù)getSearchResult(),這樣在一進入頁面的時候就會獲取到所有的數(shù)據(jù),不過由于并沒有關(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中,定義一個響應(yīng)成功后的函數(shù)getSearchResultSucc(),判斷響應(yīng)的數(shù)據(jù)是否存在。如果存在通過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ù),這個函數(shù)也是input輸入框綁定的函數(shù),傳入事件對象e,然后通過e.detail.value賦值給staticData的inputValue,代碼如下所示:

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

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

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

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

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

知識點補充:微信小程序云開發(fā)模糊查找功能實現(xiàn)

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

總結(jié)

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

相關(guān)文章

最新評論