最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序?qū)崿F(xiàn)搜索商品和歷史記錄的功能

 更新時(shí)間:2022年07月19日 08:34:32   作者:可愛(ài)碼農(nóng)  
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)搜索商品和歷史記錄的功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文主要基于微信小程序?qū)崿F(xiàn)和uni-app實(shí)現(xiàn)搜索商品和歷史記錄的功能。 不詳細(xì)介紹,主看代碼注釋即可??!

1、搜索組件頁(yè)面代碼塊

<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>

頁(yè)面實(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() { // 頁(yè)面開(kāi)始加載獲取本地搜索歷史存儲(chǔ)數(shù)據(jù)
? ? ?this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]') //頁(yè)面加載獲取搜索歷史
? ? },
? ? methods: {
? ? ? input(e) { // input 輸入事件的處理函數(shù)
? ? ? ? // console.log(e) //可以拿到最新的值
? ? ? ? clearTimeout(this.timer) // 清除定時(shí)器
? ? ? ? // 節(jié)流處理
? ? ? ? this.timer = setTimeout(() => { //開(kāi)啟定時(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ì)頁(yè)跳轉(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代表沒(méi)有
? ? ? ? // 判斷是否大于0 大于等于存在
? ? ? ? if (index >= 0) {
? ? ? ? // 刪除找到記錄
? ? ? ? ? this.historyList.splice(index, 1)
? ? ? ? }
? ? ? ? // 把input新值向數(shù)組開(kāi)頭添加
? ? ? ? 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)思路方法,沒(méi)有完全正確的。

希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

静安区| 白玉县| 淅川县| 沿河| 青铜峡市| 洪湖市| 金湖县| 香格里拉县| 霍山县| 南通市| 苍梧县| 永福县| 岑巩县| 平阴县| 汶上县| 怀化市| 紫金县| 雷波县| 祁东县| 交城县| 东兴市| 怀仁县| 监利县| 崇明县| 甘德县| 鸡泽县| 白城市| 灵丘县| 湛江市| 恭城| 广河县| 双流县| 孝感市| 平陆县| 华池县| 松潘县| 缙云县| 桐乡市| 旅游| 广东省| 泸州市|