| 主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情! |
| 我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |
如何實現小程序的常用搜索功能?下面這篇文章就帶你一步步了解在小程序中實現搜索功能的方法,希望能幫到你!

在每個小程序開發的過程中,基本上都配備了搜索功能,那么相對智能的搜索功能是如何實現的呢?通過一段時間的學習,我已經學會了比較全面的搜索框功能。我們來看看吧!

開發準備微信小程序。展示vant組件庫的效果。我們先來看看效果。

提前準備好云數據庫導入一些數據來測試搜索框功能。

實現目錄下三個新頁面的創建。
索引用作搜索框的首頁。
搜索用于特定搜索的頁面。
Hotsearch是搜索內容的詳細頁面。
首先,我們來看看搜索框首頁的索引布局。這里主要介紹搜索框的內容,下面的內容這里不再贅述。

這是index.wxml的代碼
& ltview class = & quot搜索_ 1 & quotbindtap = & quotgotoSearch & quot& gt& ltvan-搜索占位符= & quot搜索項目" disabled/>;& lt/view & gt;& ltview class = & quot搜索_ 2 & quot& gt& ltview class = & quotpic & quotbindtap = & quot列表& quot& gt& ltimage src = & quot"& gt& lt/image & gt;& lt/view & gt;& lt/view & gt;& lt/view & gt;
這是搜索框的search.wxml代碼。
& ltview class = & quotdewu -搜索& quot& gt& ltview class = & quotreturn & quot& gt& ltview class = & quotreturn _ pic & quotbindtap = & quotgotoindex & quot& gt& ltimage src = & quot"& gt& lt/image & gt;& lt/view & gt;& ltview class = & quottxt & quot& gt搜索
const db = wx . cloud . database();const dewuCollection = db . collection(& amp;#39;吳德& amp#39;);要在輸入框變化時彈出相關內容,需要兩個頁面。當輸入框中有內容輸入時,顯示隱藏頁面。{{issuggest==false?隱藏“:”} } & quot判斷是否會出現相關的內容頁面,使用indexOf判斷云數據庫中是否存在e.detail(輸入框內容),如果存在,那么將這個數據存儲在一個數組中,連接上一次搜索后的數組,然后使用wx . setstoragesync();將輸入框中的數據保存到存儲器中,然后從wx.getStorageSync()中提取數據。
這是當輸入框中有數據時,會彈出詳情頁。點擊跳轉到產品的詳細信息頁面。

這是搜索框的邏輯
if(e.detail.length!= 0){ this . setdata({ issuggest:true,}) var arr =
Wx.navigateTo可用于跳轉到詳細頁面,添加字符串模板,判斷id的值,用數據驅動頁面,跳轉到不同數據的同一頁面。
wx . navigate to({ URL:` 0../hotsearch/hotsearch?Id=`+id})最后,更新數據。
wx . show loading({ title:& amp;#39;數據加載...&;#39;,})setTimeout(()= & gt;{ wx . hide loading()this . setdata({ goods nav:nav,goodsList:List,recommend:List,goods 2:this . data . history List })},1000)// console.log(goodsList,& amp#39;= = = = = = = = = = = & amp#39;);},注意不要忘記引入要在全局json或局部頁面json中使用的組件的地址。
"使用組件& quot:{ & quotvan-搜索& quot:& quot。/mini program _ NPM/@ vant/weapp/search/index & quot;},擴展自動跳轉到導航欄中間的功能也是相當常見的。

這是wxml代碼,最重要的是scroll -x = & quot;true & quot讓導航欄水平滑動scroll -with -animation = & quot;true & quot是動畫幻燈片,滾動-到-視圖= & quot{ { scrollTop } } & quot
& ltscroll -view scroll -x = & quot;true & quotscroll -with -animation = & quot;true & quotstyle = & quot寬度:100%;"class = & quotscroll -view _ H & quot;scroll -into -view = & quot;{ { scrollTop } } & quot& gt& lt查看wx:for = & quot;{ { goodsNav } } & quotwx:key = & quot;索引& quotid = & quot{ { item.id } } & quotdata -index = & quot;{ { index } } & quotdata -type = & quot;{ { item.type } } & quotbindtap = & quot改變商品& quotclass = & quotscroll -view -item _ H { { activenaviindex = = index?& amp#39;主動& amp#39;:& amp#39;& amp#39;} } & quot& gt& lttext & gt{ { item.text } } & lt/text & gt;& lt/view & gt;& lt/scroll -view & gt;& lt/view & gt;這是事件let {index,type} = e.currenttarget.dataset綁定到導航欄;提取索引和類型,然后設置一個計數作為前幾個不動,然后拼接到id,將id的值傳遞給scrollTop,讓導航欄跳轉到scrollTop的值,這個值在中間。
console . log(e);設{index,type } = e . current target . dataset;控制臺. log(& quot;index = & quot+index,& quottype = & quot+類型);this . setdata({ activenaviindex:index })if(type = = & amp;#39;推薦& amp#39;){ this . setdata({ goods list:this . data . recommend })} else { let goods = this . data . recommend . filter((好)= & gtgood . camptype = = type)this . setdata({ goods list:goods })//console . log(this . data . goods)} var index 1 = e . current target . dataset . index;var count = 2;var id = & quotitem & quot+(索引1-計數);//拼接id if(count = = 2 &;& ampindex1 <。計數= = 3 & amp& ampindex1 <。2 | | count = = 4 & amp& ampindex1 <。3){ id = & quot;item0 & quot;} console . log(& quot;下標”,索引1,& quot---分類id名稱”,ID)這個。setdata ({scroll top: ID})},可以達到添加wxss后的效果。但是這樣寫有一個問題,就是顯示的內容是偶數的時候,比如6,不能正確跳到中間,會跳到3的位置,所以有一點偏差。這個問題我還沒解決。不知道有沒有大佬知道怎么解決這個問題?
這里的源代碼是項目的完整源代碼,以上是部分代碼。有興趣的可以看看完整的源代碼。
綜上所述,這是我第一次寫一個稍微完整的項目。這里主要介紹一下我在開發過程中遇到的主要困難。雖然總體上沒有什么困難,但對我來說還是意義重大。有了這次經歷,我發現我還有很多東西要學,也感謝在我困難的時候幫助我指導我的老師和同學。如果你覺得這篇文章到了你的地方,不妨給我一個贊,對我來說就太棒了。
更多編程相關知識,請訪問:編程入門!!
這幾篇文章你可能也喜歡:
- dedecms 為您的網站添加搜索功能(dedecms 插件)
- 當時微信小程序模仿了網易云音樂的實時搜索功能(微信小程序歌曲播放列表)。
- 那些年微信小程序模仿網易云音樂關于實時搜索的功能(微信小程序網易云代碼)。
- Dedecms為網站增加搜索功能(dedecms使用教程)
- dedecms為網站增加搜索功能(dedecms建站操作)
本文由主機參考刊發,轉載請注明:小程序中實現搜索功能的方法深度講解(小程序中實現搜索功能的方法深度講解是) http://www.k2270.com/112724.html
主機參考

















評論前必須登錄!
注冊