Vue 實(shí)現(xiàn)輸入框新增搜索歷史記錄功能

vue實(shí)現(xiàn)搜索顯示歷史搜索記錄,采用插件-good-storage
安裝插件
npm install good-storage -S
在本地新建cache.js文件,該文件是關(guān)于本地存儲(chǔ)的邏輯處理(緩存到本地的數(shù)據(jù)最大緩存15條,并且新的插入在第一位,首先得到當(dāng)前的存儲(chǔ)數(shù)據(jù)情況,將關(guān)鍵字存到數(shù)組中,判斷如果數(shù)組中有相同的數(shù)據(jù),則把重復(fù)的數(shù)據(jù)刪除,將新的關(guān)鍵字存入到前面)
cache.js 文件中的代碼如下
/*把搜索的結(jié)果保存下來(lái)*/
/*用export把方法暴露出來(lái)*/
/*定義存儲(chǔ)搜索的key _search_定義內(nèi)部使用的key*/
const SEARCH_KEY='_search_'
const SEARCH_MAX_LENGTH=15
/*插入方法 arr存儲(chǔ)的數(shù)據(jù) val傳入存儲(chǔ)的值 compare前后比較的函數(shù) maxlen存入的最大值*/
function insertArray(arr,val,compare,maxlen){
//findIndex()函數(shù)也是查找目標(biāo)元素,找到就返回元素的位置,找不到就返回-1。
const index=arr.findIndex(compare)
if(index===0){ //數(shù)據(jù)為數(shù)組中的第一個(gè)數(shù)據(jù) 不做任何操作
return
}
if(index>0){ //數(shù)組中有這條數(shù)據(jù)并且不再第一個(gè)位置
arr.splice(index,1) //刪掉這個(gè)數(shù)
}
arr.unshift(val);//把這條數(shù)據(jù)存儲(chǔ)到數(shù)組中的第一個(gè)位置上
if(maxlen && arr.length>maxlen){
//如果有條數(shù)限制并且數(shù)組的個(gè)數(shù)大于限制數(shù)
arr.pop() //方法將刪除 arrayObject 的最后一個(gè)元素,把數(shù)組長(zhǎng)度減 1,并且返回它刪除的元素的值
}
}
//開(kāi)源storage的庫(kù),對(duì)localstorage和sessionstorage的封裝
import storage from 'good-storage'
export function saveSearch(query){
let searches=storage.get(SEARCH_KEY,[])
/*邏輯是最后一次搜索的結(jié)果放到搜索歷史的第一個(gè)*/
insertArray(searches,query,(item)=>{
return item===query //這是傳入的一個(gè)比較函數(shù) 說(shuō)明query在這個(gè)數(shù)組中
},SEARCH_MAX_LENGTH)
storage.set(SEARCH_KEY,searches)
return searches
}
在對(duì)應(yīng)的組件中應(yīng)用的方式:
<template>
<div class="search">
<!-- 頂部搜索欄 -->
<div class="header">
<div class="head-title title-style">輸入關(guān)鍵字</div>
<div class="head-input">
<input
type="text"
ref="inputchange"
v-model="valuetext"
@keyup.enter="onSearch(true)"
@keyup.tab="onSearch(true)"
@focus="initPage"
placeholder="請(qǐng)輸入關(guān)鍵字進(jìn)行搜索"
/>
<div type="text" @click="clear" class="input-btn title-style">清除</div>
</div>
<div class="history-panel" v-if="!isFocus">
<div v-if="historyxs">搜索歷史</div>
<div v-if="searches_list.length>0">
<ul class="his_ulcon" v-if="historyxs">
<li
v-for="(item,index) in searches_list"
:key="index"
@click="historysearch(item)"
>{{item}}</li>
</ul>
</div>
<div class="history-tips" v-else>暫無(wú)搜索記錄!</div>
<p v-if="historyxs" @click="clearhis">清空歷史記錄</p>
</div>
</div>
<!-- ... 此處省略無(wú)關(guān)代碼 -->
<!-- 底部按鈕 -->
<div class="footer title-style">
<van-row>
<van-col span="12">
<div class="left" @click="resetData">重置所選條件</div>
</van-col>
<van-col span="12">
<div class="right" @click="onSearch(true)">立即搜索</div>
</van-col>
</van-row>
</div>
</div>
</template>
<script type="text/Babel">
import { saveSearch } from "../../utils/cache"; //引用本地存儲(chǔ)js
import storage from "good-storage"; //引入good-storage包
export default {
data() {
return {
isFocus: true,
searches_list: [], //歷史搜索記錄列表
historyxs: false,
valuetext: ""
};
},
mounted() {},
methods: {
//輸入框獲取焦點(diǎn)
initPage() {
this.isFocus = false;
this.$emit("initSearchPage");
//為避免重復(fù)先清空再添加
this.searches_list = [];
let searches = storage.get("_search_");
this.searches_list = searches ? searches : [];
if (this.searches_list.length > 0) {
this.historyxs = true;
} else {
this.historyxs = false;
}
},
//清空歷史記錄
clearhis() {
storage.remove("_search_");
this.searches_list = [];
this.historyxs = false;
},
//點(diǎn)擊歷史搜索把搜索的記錄添加到good-storage中
historysearch(item) {
saveSearch(item);
this.valuetext = item;
this.historyxs = false;
},
resetData() {
// ...
// 此次省略重置數(shù)據(jù)的邏輯代碼
},
onSearch(isFirst) {
this.isFocus = true;
if (this.valuetext != "") {
//搜索框不為空
saveSearch(this.valuetext); // 本地存儲(chǔ)搜索的內(nèi)容
let params = {
majorInfo: this.valuetext //流程類(lèi)型或者流程名稱(chēng)
};
this.$emit("handleSearch", params);
this.isFocus = true;
}
// ...
// 此次省略調(diào)用搜索接口的代碼
},
clear() {
this.valuetext = "";
}
// ... 無(wú)關(guān)代碼已省略
}
};
</script>
<style lang="less" rel="stylesheet/less" type="text/less" scoped>
.search {
overflow-y: scroll;
overflow-x: hidden;
padding: 0.14rem 0.12rem 0.53rem;
.header {
border-bottom: 0.01rem solid #f2f2f2;
.head-title {
padding-bottom: 0.05rem;
color: #666666;
}
.head-input {
width: 100%;
padding-bottom: 0.1rem;
display: flex;
flex-direction: row;
justify-content: space-between;
> input {
height: 0.29rem;
width: 2.84rem;
border-radius: 0.03rem;
background-color: #f6f6f6;
font-family: PingFang-SC-Regular;
font-weight: Regular;
color: #999999;
font-size: 0.12rem;
padding-left: 0.12rem;
}
.input-btn {
color: #029ffb;
width: 0.5rem;
height: 0.29rem;
line-height: 0.29rem;
text-align: center;
}
}
.history-panel {
width: 100%;
overflow: hidden;
padding: 0.1rem 0;
border-top: 1px solid #f2f2f2;
.his_ulcon {
margin-top: 0.1rem;
box-sizing: border-box;
display: flex;
flex-direction: row;
justify-content: flex-start;
flex-wrap: wrap;
> li {
border: 1px solid #f2f2f2;
border-radius: 0.03rem;
display: inline-block;
font-size: 0.12rem;
padding: 0 0.15rem;
width: fit-content; //div寬度自適應(yīng)文字內(nèi)容
width: -webkit-fit-content;
width: -moz-fit-content;
height: 0.29rem;
line-height: 0.29rem;
text-align: center;
margin-right: 0.1rem;
background-color: #f2f2f2;
margin-bottom: 0.1rem;
}
}
div {
box-sizing: border-box;
font-size: 0.13rem;
color: #666666;
font-weight: Medium;
font-family: PingFang-SC-Medium;
}
> p {
text-align: center;
margin-top: 0.1rem;
font-size: 0.13rem;
}
}
.history-tips {
text-align: center;
}
}
.title-style {
font-size: 0.13rem;
font-weight: Medium;
font-family: PingFang-SC-Medium;
}
}
</style>
溫馨提示:引入cache.js時(shí)你的文件路徑要按照你自己的路徑來(lái) 一 一對(duì)應(yīng)
總結(jié)
以上所述是小編給大家介紹的Vue 實(shí)現(xiàn)輸入框新增搜索歷史記錄功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
利用Dectorator分模塊存儲(chǔ)Vuex狀態(tài)的實(shí)現(xiàn)
這篇文章主要介紹了利用Dectorator分模塊存儲(chǔ)Vuex狀態(tài)的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Vue3封裝hooks實(shí)現(xiàn)實(shí)時(shí)獲取麥克風(fēng)音量
這篇文章主要為大家詳細(xì)介紹了Vue3如何通過(guò)封裝一個(gè)hooks實(shí)現(xiàn)實(shí)時(shí)獲取麥克風(fēng)音量功能,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2024-03-03
vue?請(qǐng)求后端數(shù)據(jù)的示例代碼
在vue中,我們?nèi)绾瓮ㄟ^(guò)請(qǐng)求接口來(lái)訪問(wèn)后端的數(shù)據(jù)呢?在這里簡(jiǎn)單總結(jié)了一個(gè)小示例,對(duì)vue請(qǐng)求后端數(shù)據(jù)實(shí)例代碼感興趣的朋友一起看看吧2022-09-09
詳解如何在 vue 項(xiàng)目里正確地引用 jquery 和 jquery-ui的插件
本篇文章主要介紹了詳解如何在 vue 項(xiàng)目里正確地引用 jquery 和 jquery-ui的插件,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06
vue+axios實(shí)現(xiàn)圖片上傳識(shí)別人臉的示例代碼
本文主要介紹了vue+axios實(shí)現(xiàn)圖片上傳識(shí)別人臉,這里采用的是vant的文件上傳組件,通過(guò)上傳圖片后端識(shí)別圖片里的人臉,感興趣的可以了解一下2021-11-11
使用vue-cli3+typescript的項(xiàng)目模板創(chuàng)建工程的教程
這篇文章主要介紹了使用vue-cli3+typescript的項(xiàng)目模板創(chuàng)建工程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
使用Vue實(shí)現(xiàn)點(diǎn)擊按鈕小球加入購(gòu)物車(chē)動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)點(diǎn)擊按鈕小球加入購(gòu)物車(chē)動(dòng)畫(huà),文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
vue新vue-cli3環(huán)境配置和模擬json數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇vue新vue-cli3環(huán)境配置和模擬json數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09

