1. 程式人生 > >微信小程式的開發之搜尋功能

微信小程式的開發之搜尋功能

就直接貼程式碼啦~

wxml
<view class="weui-search-bar">
   <view class="weui-search-bar__form">
    <view class="weui-search-bar__box">
      <icon class="weui-icon-search_in-box" type="search" size="14"></icon>
      <input type="text" class="weui-search-bar__input" placeholder
="請輸入搜尋內容" value="
{{wxSearchData}}" bindinput="wxSearchInput" bindconfirm="wxSearchConfirm" /> <view class="weui-icon-clear" wx:if="{{wxSearchData.value.length > 0}}" bindtap="wxSearchClear"> <icon type="clear" size="14"></icon> </view> </view> </view
>
<view class="weui-search-bar__cancel-btn" bindtap="back"> <text data-key='back'>返回</text> </view> </view>
js

/**
   * 頁面的初始資料
   */
  data: {
    lists: [],              // 接收搜尋的內容
    wxSearchData: '',       // 輸入的值
  },
/**
   * 搜尋
   */
  wxSearchInput: function (value) {
    var
that = this; if (value.detail.value.length > 0) { wx.request({ url: '', data: { value: value.detail.value }, header: { 'content-type': 'application/x-www-form-urlencoded' }, method: 'POST', dataType: json, responseType: text, success: function (res) { if (res.code) { var data = that.data.lists; for (let i = 0; i < res.data.length; i++) { data.push(res.data[i]); } that.setData({ wxSearchData: value.detail.value, lists: data }) } }, fail: function (res) { }, complete: function (res) { }, }) } }, /** * 監聽軟鍵盤確認鍵 */ wxSearchConfirm: function (e) { }, /** * 返回 */ back: function (e) { wx:wx.navigateBack({ delta: 1, }) }