小程序?qū)崿F(xiàn)搜索框
小程序中搜索框的簡單實現(xiàn),供大家參考,具體內(nèi)容如下
搜索框
搜索框無論是在電商網(wǎng)站還是小程序中是很常見的,那么在小程序中是如何實現(xiàn)的呢,我們一起來看看吧(過程遇到很多問題)。
思路
在搜索框中輸入關(guān)鍵詞時,應(yīng)該會向服務(wù)器發(fā)送請求,因為沒有相關(guān)接口,所以我就模擬數(shù)據(jù)啦,用文檔中API中的setStorage和getStorage在本地存儲數(shù)據(jù)和讀取數(shù)據(jù),在搜索框中輸入時若能匹配到則顯示,若匹配不到,則顯示“沒有數(shù)據(jù)”。
模糊搜索
search.wxml
<!--pages/search/search.wxml-->
<view class='search'>
<input type='text'
placeholder='請輸入您要搜索的內(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:[]
},
//鍵盤輸入時實時調(diào)用搜索方法
input(e){
// console.log(e)
this.search(e.detail.value)
},
//點擊完成按鈕時觸發(fā)
confirm(e){
this.search(e.detail.value)
},
search(key){
var that=this;
//從本地緩存中異步獲取指定 key 的內(nèi)容
var list=wx.getStorage({
key: 'list',
//從Storage中取出存儲的數(shù)據(jù)
success: function(res) {
// console.log(res)
if(key==''){ //用戶沒有輸入時全部顯示
that.setData({
list:res.data
})
return;
}
var arr=[]; //臨時數(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: []
},
//鍵盤輸入時實時調(diào)用搜索方法
input(e) {
this.search(e.detail.value)
},
search(key) {
var that = this;
//從本地緩存中異步獲取指定 key 的內(nèi)容
var list = wx.getStorage({
key: 'list',
//從Storage中取出存儲的數(shù)據(jù)
success: function (res) {
// console.log(res)
if (key == '') { //用戶沒有輸入時全部顯示
that.setData({
list: res.data
})
return;
}
var arr = []; //臨時數(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后面的值也加了引號,結(jié)果會出錯,數(shù)據(jù)拿不到,因此,要注意此處的坑吆! \color{red}{在此過程中,我在data后面的值也加了引號,結(jié)果會出錯,數(shù)據(jù)拿不到,因此,要注意此處的坑吆!}在此過程中,我在data后面的值也加了引號,結(jié)果會出錯,數(shù)據(jù)拿不到,因此,要注意此處的坑吆!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實戰(zhàn)例子之實現(xiàn)自動打字機(jī)動效
什么是打字機(jī)效果呢?打字機(jī)效果即為文字逐個輸出,實際上就是一種Web動畫,下面這篇文章主要給大家介紹了關(guān)于JS實戰(zhàn)例子之實現(xiàn)自動打字機(jī)動效的相關(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-11
javascript 二維數(shù)組的實現(xiàn)與應(yīng)用
javascript沒有二維數(shù)組.所有自定義了一個數(shù)組類,下面是實例代碼,需要的朋友可以參考下。2010-03-03
基于javascript實現(xiàn)動態(tài)時鐘效果
這篇文章主要為大家詳細(xì)介紹了基于javascript實現(xiàn)動態(tài)時鐘效果的相關(guān)資料,動態(tài)顯示系統(tǒng)當(dāng)前時間,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-02-02
JavaScript學(xué)習(xí)筆記之函數(shù)記憶
這篇文章主要介紹了JavaScript學(xué)習(xí)筆記之函數(shù)記憶,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

