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

小程序?qū)崿F(xiàn)搜索框

 更新時(shí)間:2020年06月19日 11:27:33   作者:smile@qingqing  
搜索框無論是在電商網(wǎng)站還是小程序中都很常見,這篇文章主要就為大家詳細(xì)介紹了小程序如何實(shí)現(xiàn)搜索框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

小程序中搜索框的簡單實(shí)現(xiàn),供大家參考,具體內(nèi)容如下

搜索框

搜索框無論是在電商網(wǎng)站還是小程序中是很常見的,那么在小程序中是如何實(shí)現(xiàn)的呢,我們一起來看看吧(過程遇到很多問題)。

思路

在搜索框中輸入關(guān)鍵詞時(shí),應(yīng)該會(huì)向服務(wù)器發(fā)送請(qǐng)求,因?yàn)闆]有相關(guān)接口,所以我就模擬數(shù)據(jù)啦,用文檔中API中的setStorage和getStorage在本地存儲(chǔ)數(shù)據(jù)和讀取數(shù)據(jù),在搜索框中輸入時(shí)若能匹配到則顯示,若匹配不到,則顯示“沒有數(shù)據(jù)”。

模糊搜索

search.wxml

<!--pages/search/search.wxml-->
<view class='search'>
 <input type='text' 
 placeholder='請(qǐng)輸入您要搜索的內(nèi)容'
 bindinput='input' 
 bindconfirm='confirm'/>
 <icon type='search' class='icons'></icon>
 <view wx:for="{{list}}" wx:key='' class='lists'>
 <text wx:if="{{item.show}}">{{item.name}}</text>
 </view>
</view>

search.wxss

/* pages/search/search.wxss */
.search{
 position: relative;
}
.search input{
 border:1px solid #ccc;
 border-radius: 6px;
 height: 30px;
}
.icons{
 position: absolute;
 right: 20px;
 top:5px;
}
.lists{
 text-align: center;
 margin-top: 20px;
 color: rgb(230, 124, 25);
}

search.js

// pages/search/search.js
Page({

 /**
 * 頁面的初始數(shù)據(jù)
 */
 data: {
 list:[] 
 },
 //鍵盤輸入時(shí)實(shí)時(shí)調(diào)用搜索方法
 input(e){
 // console.log(e)
 this.search(e.detail.value)
 },
 //點(diǎn)擊完成按鈕時(shí)觸發(fā)
 confirm(e){
 this.search(e.detail.value)
 },
 search(key){
 var that=this;
 //從本地緩存中異步獲取指定 key 的內(nèi)容
 var list=wx.getStorage({
 key: 'list',
 //從Storage中取出存儲(chǔ)的數(shù)據(jù)
 success: function(res) {
 // console.log(res)
 if(key==''){ //用戶沒有輸入時(shí)全部顯示
 that.setData({
 list:res.data
 })
 return;
 }
 var arr=[]; //臨時(shí)數(shù)組,用于存放匹配到的數(shù)組
 for(let i in res.data){
 res.data[i].show=false; //所有數(shù)據(jù)隱藏
 if (res.data[i].search.indexOf(key)>=0){
 res.data[i].show = true; //讓匹配到的數(shù)據(jù)顯示
 arr.push(res.data[i])
 }
 }
 if(arr.length==0){
 that.setData({
 list:[{show:true,name:'沒有相關(guān)數(shù)據(jù)!'}]
 })
 }else{
 that.setData({
 list: arr
 })
 }
 },
 })
 },
 /**
 * 生命周期函數(shù)--監(jiān)聽頁面加載
 */
 onLoad: function (options) {
 var list=[
 {name: "西安市第一人民醫(yī)院", show: true, search:"西安市第一人民醫(yī)院"},
 {name: "西安市第二人民醫(yī)院", show: true, search: "西安市第二人民醫(yī)院" },
 {name: "蘭州市第一人民醫(yī)院", show: true, search: "蘭州市第一人民醫(yī)院" },
 {name: "蘭州市第二人民醫(yī)院", show: true, search: "蘭州市第二人民醫(yī)院" }
 ]
 wx.setStorage({
 key: 'list',
 data: list
 })
 this.setData({
 list:list
 })
 },
})

效果圖

條件搜索

searchif.wxml

<!--pages/searchif/searchif.wxml-->
<view class='search'>
 <input type='text'
 bindblur='input'/>
 <button type='primary' class='btn' size='mini'>搜索</button>
 <view wx:for="{{list}}" wx:key='' class='lists'>
 <text wx:if="{{list}}">{{item.name}}</text>
 </view>
</view>

searchif.wxss

/* pages/searchif/searchif.wxss */
.search{
 padding-left: 10px;
}
.search input{
 border:1px solid #ccc;
 border-radius: 6px;
 height: 30px;
 display: inline-block;
 padding-left: 5px;
}
.btn{
 height: 32px;
 margin-left: 10px;
}
.lists{
 text-align: center;
 margin-top: 20px;
 color: rgb(230, 124, 25);
}

searchif.js

// pages/searchif/searchif.js
Page({
 /**
 * 頁面的初始數(shù)據(jù)
 */
 data: {
 list: []
 },
 //鍵盤輸入時(shí)實(shí)時(shí)調(diào)用搜索方法
 input(e) {
 this.search(e.detail.value)
 },
 search(key) {
 var that = this;
 //從本地緩存中異步獲取指定 key 的內(nèi)容
 var list = wx.getStorage({
 key: 'list',
 //從Storage中取出存儲(chǔ)的數(shù)據(jù)
 success: function (res) {
 // console.log(res)
 if (key == '') { //用戶沒有輸入時(shí)全部顯示
 that.setData({
 list: res.data
 })
 return;
 }
 var arr = []; //臨時(shí)數(shù)組,用于存放匹配到的數(shù)組
 for (let i in res.data) {
 if (res.data[i].name.indexOf(key) >= 0) {
 arr.push(res.data[i])
 }
 }
 if (arr.length == 0) {
 that.setData({
 list: [{ name: '沒有相關(guān)數(shù)據(jù)!' }]
 })
 } else {
 that.setData({
 list: arr
 })
 }
 },
 })
 },
 /**
 * 生命周期函數(shù)--監(jiān)聽頁面加載
 */
 onLoad: function (options) {
 var list = [
 { name: "西安市第一人民醫(yī)院"},
 { name: "西安市第二人民醫(yī)院"},
 { name: "蘭州市第一人民醫(yī)院"},
 { name: "蘭州市第二人民醫(yī)院"}
 ]
 wx.setStorage({
 key: 'list',
 data: list
 })
 this.setData({
 list: list
 })
 },
})

效果圖

遇到的問題

小程序文檔中的setStorage里面的代碼是這樣寫的:

wx.setStorage({
 key:"key",
 data:"value"
})

在此過程中,我在data后面的值也加了引號(hào),結(jié)果會(huì)出錯(cuò),數(shù)據(jù)拿不到,因此,要注意此處的坑吆! \color{red}{在此過程中,我在data后面的值也加了引號(hào),結(jié)果會(huì)出錯(cuò),數(shù)據(jù)拿不到,因此,要注意此處的坑吆!}在此過程中,我在data后面的值也加了引號(hào),結(jié)果會(huì)出錯(cuò),數(shù)據(jù)拿不到,因此,要注意此處的坑吆!

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論