微信小程序?qū)崿F(xiàn)搜索商品和歷史記錄的功能
本文主要基于微信小程序?qū)崿F(xiàn)和uni-app實(shí)現(xiàn)搜索商品和歷史記錄的功能。 不詳細(xì)介紹,主看代碼注釋即可!!
1、搜索組件頁面代碼塊
<template> ? <view> ? ? <!-- uni的搜索框 --> ? ? <view class="search-box"> ? ? ? <uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar> ? ? </view> ? ? <!-- 搜索建議列表 --> ? ? <view class="sugg-list" v-if="searchResults.length !== 0"> ? ? ? <view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="gotoDatail(item)"> ? ? ? ? <view class="goos-name"> {{item.goods_name}} </view> ? ? ? ? <uni-icons type="arrowright" size="17" ></uni-icons> ? ? ? </view> ? ? </view> ? ? <!-- 搜索歷史盒子 --> ? ? <view class="history-box" v-else> ? ? ? <!-- 歷史標(biāo)題區(qū)域 --> ? ? ? <view class="history-title"> ? ? ? ? <text>搜索歷史</text> ? ? ? ? <uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons> ? ? ? </view> ? ? ? <!-- 歷史記錄列表區(qū)域 --> ? ? ? <view class="history-list"> ? ? ? ? <uni-tag :text="item" v-for="(item,i) in historyList" :key="i"></uni-tag> ? ? ? </view> ? ? </view> ? </view> </template>
頁面實(shí)現(xiàn)效果圖如下圖:
2、樣式代碼塊
<style lang="scss"> ? .search-box { ? ? position: sticky; //搜索框黏性定位 ? ? top: 0; ? ? z-index: 999; ? ?? ? ? .uni-searchbar { ? ? ? background-color: #C00000 !important; ? ? } ? } //搜索列表樣式 ? .sugg-list { ? ? padding: 0 5px; ? ? .sugg-item { ? ? ? display: flex; ? ? ? align-items: center; ? ? ? justify-content: space-between; //兩端對(duì)其 ? ? ? font-size: 12px; ? ? ? padding: 13px 0; ? ? ? border-bottom: 1px solid #EEEEEE; ? ? ? .goos-name { ? ? ? ? white-space: nowrap; // 文字不允許換行 ? ? ? ? overflow: hidden; ? ? ? ? text-overflow: ellipsis; //文本超出內(nèi)容用。。。隱藏 ? ? ? } ? ? } ? } //搜索歷史樣式 ? .history-box { ? ? padding: 0 5px; ? ? .history-title { ? ? ? display: flex; ? ? ? justify-content: space-between; //兩側(cè)對(duì)齊 ? ? ? height: 40px; ? ? ? align-items: center; ? ? ? font-size: 13px; ? ? ? border: 1px solid #efefef; ? ? ? .history-list { ? ? ? ? display: flex; ? ? ? ? flex-wrap: wrap; ? ? ? ? .uni-tag { ? ? ? ? ? margin: 0 2px; ? ? ? ? } ? ? ? } ? ? } ? } </style>
3、邏輯代碼塊
<script> ? export default { ? ? data() { ? ? ? return { ? ? ? ? timer: null, //接收定時(shí)器 ? ? ? ? kw: '', //input的最新值 ? ? ? ? searchResults: [], // 搜索的結(jié)果列表 ? ? ? ? historyList: [], // 搜索歷史的數(shù)組 ? ? ? }; ? ? }, ? ? onLoad() { // 頁面開始加載獲取本地搜索歷史存儲(chǔ)數(shù)據(jù) ? ? ?this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]') //頁面加載獲取搜索歷史 ? ? }, ? ? methods: { ? ? ? input(e) { // input 輸入事件的處理函數(shù) ? ? ? ? // console.log(e) //可以拿到最新的值 ? ? ? ? clearTimeout(this.timer) // 清除定時(shí)器 ? ? ? ? // 節(jié)流處理 ? ? ? ? this.timer = setTimeout(() => { //開啟定時(shí)器 ? ? ? ? ? // console.log(e) ? ? ? ? ? this.kw = e // 輸入框最新值 賦值給kw ? ? ? ? ? this.getSearchList() // 調(diào)用獲取搜索 ? ? ? ? }, 500) ? ? ? }, ? ? ? // 獲取搜索聯(lián)想建議方法 ? ? ? async getSearchList() { ? ? ? // 判斷input的值是否為空 ? ? ? ? if (this.kw.length === 0) { ? ? ? ? ? this.searchResults = [] // 清空搜索結(jié)果 ? ? ? ? ? return // 停止執(zhí)行 ? ? ? ? } ? ? ? ? // 獲取搜索結(jié)果 ? ? ? ? const { ? ? ? ? ? data: res ? ? ? ? } = await uni.$http.get('/api/.....', { ? ? ? ? ? query: this.kw ? ? ? ? }) ? ? ? ? // 判斷是否成功獲取數(shù)據(jù) ? ? ? ? if (res.meta.status !== 200) return uni.$showMsg() ? ? ? ? // 獲取成功把結(jié)果賦值 ? ? ? ? this.searchResults = res.message ? ? ? ? this.saveSearchHistory() // 調(diào)用保存搜索歷史記錄 ? ? ? }, ? ? ? // 搜索聯(lián)想列表詳細(xì)頁跳轉(zhuǎn)方法 ? ? ? gotoDatail(item) { ? ? ? ? uni.navigateTo({ ? ? ? ? ? url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id ? ? ? ? }) ? ? ? }, ? ? ? // 保存搜索歷史記錄并持久化歷史搜索方法 ? ? ? saveSearchHistory() {? ? ? ? // 查找搜索歷史結(jié)果數(shù)組中,重復(fù)的搜索 ? ? ? ? const index = this.historyList.findIndex(x => x === this.kw) // 返回結(jié)果 ?-1代表沒有 ? ? ? ? // 判斷是否大于0 大于等于存在 ? ? ? ? if (index >= 0) { ? ? ? ? // 刪除找到記錄 ? ? ? ? ? this.historyList.splice(index, 1) ? ? ? ? } ? ? ? ? // 把input新值向數(shù)組開頭添加 ? ? ? ? this.historyList.unshift(this.kw) ? ? ? ? //持久化搜索歷史 ? ? ? ? uni.setStorageSync('kw', this.historyList) ? ? ? }, ? ? ? // 清空搜索歷史記錄方法 ? ? ? cleanHistory() {? ? ? ? ? // 清空 data 中保存的搜索歷史 ? ? ? ? this.historyList = [] ? ? ? ? // 清空本地存儲(chǔ)中記錄的搜索歷史 ? ? ? ? uni.setStorageSync('kw', '[]') ? ? ? } ? ? } ? } </script>
以上就是實(shí)現(xiàn)小程序搜索功能,歷史記錄功能的實(shí)現(xiàn),當(dāng)然這也是一種實(shí)現(xiàn)思路方法,沒有完全正確的。
希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
推薦幾個(gè)不錯(cuò)的console調(diào)試技巧實(shí)現(xiàn)
這篇文章主要介紹了推薦幾個(gè)不錯(cuò)的console調(diào)試技巧實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12JavaScript省市聯(lián)動(dòng)實(shí)現(xiàn)代碼
這篇文章主要介紹了JavaScript省市聯(lián)動(dòng)實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-02-02JavaScript資源預(yù)加載組件和滑屏組件的使用推薦
這篇文章主要介紹了JavaScript資源預(yù)加載組件和滑屏組件的使用推薦,分別為preload和slide的用法講解,使用起來非常簡(jiǎn)單,需要的朋友可以參考下2016-03-03小程序圖片長(zhǎng)按識(shí)別功能的實(shí)現(xiàn)方法
這篇文章主要介紹了小程序圖片長(zhǎng)按識(shí)別功能的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08利用Bootstrap實(shí)現(xiàn)表格復(fù)選框checkbox全選
Bootstrap相信應(yīng)該不用多介紹,來自 Twitter,是目前最受歡迎的前端框架。這篇文章主要給大家介紹了如何利用Bootstrap實(shí)現(xiàn)表格中的checkbox復(fù)選框全選效果,文中給出詳細(xì)的介紹及完整的實(shí)例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,下面來一起看看吧。2016-12-12JavaScript實(shí)現(xiàn)類似拉勾網(wǎng)的鼠標(biāo)移入移出效果
其實(shí)也是個(gè)偶然的機(jī)會(huì)讓我想去研究一下這個(gè)效果。主要是由于有個(gè)群里的人發(fā)了個(gè)講解這個(gè)效果的鏈接,當(dāng)時(shí)也沒怎么在意,然后過兩天,突然就想起這件事,便去拉勾網(wǎng)一看,效果不錯(cuò)啊,所以就自己研究起來,現(xiàn)在將過程分享給大家,有需要的可以參考借鑒。2016-10-10JS實(shí)現(xiàn)鼠標(biāo)經(jīng)過好友列表中的好友頭像時(shí)顯示資料卡的效果
需求為當(dāng)用戶鼠標(biāo)經(jīng)過好友列表中好友頭像時(shí),顯示該好友的基本資料,其實(shí)也就是類似QQ客戶端的那種功能,下面是具體的實(shí)現(xiàn)思路及過程2014-07-07