小程序?qū)崿F(xiàn)搜索框
小程序中搜索框的簡單實(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)文章
JS實(shí)戰(zhàn)例子之實(shí)現(xiàn)自動(dòng)打字機(jī)動(dòng)效
什么是打字機(jī)效果呢?打字機(jī)效果即為文字逐個(gè)輸出,實(shí)際上就是一種Web動(dòng)畫,下面這篇文章主要給大家介紹了關(guān)于JS實(shí)戰(zhàn)例子之實(shí)現(xiàn)自動(dòng)打字機(jī)動(dòng)效的相關(guān)資料,需要的朋友可以參考下2023-01-01淺析BootStrap中Modal(模態(tài)框)使用心得
Bootstrap Modals(模態(tài)框)是使用定制的 Jquery 插件創(chuàng)建的。本文給大家分享BootStrap中Modal(模態(tài)框)使用心得,一起看看吧2016-12-12跟我學(xué)習(xí)javascript的prototype原型和原型鏈
跟我學(xué)習(xí)javascript的prototype原型和原型鏈,感興趣的小伙伴們可以參考一下2015-11-11javascript 二維數(shù)組的實(shí)現(xiàn)與應(yīng)用
javascript沒有二維數(shù)組.所有自定義了一個(gè)數(shù)組類,下面是實(shí)例代碼,需要的朋友可以參考下。2010-03-03基于javascript實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了基于javascript實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果的相關(guān)資料,動(dòng)態(tài)顯示系統(tǒng)當(dāng)前時(shí)間,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02JavaScript學(xué)習(xí)筆記之函數(shù)記憶
這篇文章主要介紹了JavaScript學(xué)習(xí)筆記之函數(shù)記憶,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09js實(shí)現(xiàn)滑動(dòng)觸屏事件監(jiān)聽的方法
這篇文章主要介紹了js實(shí)現(xiàn)滑動(dòng)觸屏事件監(jiān)聽的方法,適用于手機(jī)端觸屏滑動(dòng)事件的監(jiān)聽技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05