vue实现搜索框历史记录功能,本地储存,最新点击排在最前面

记录一下,方便以后学习
需求:
输入框搜索历史记录,去重,最新点击的排在最前面,可点击删除

效果:
在这里插入图片描述在这里插入图片描述

话不多说,上代码…

HTML部分

 <div class="head_top">
      <div class="left">
        <img src="../fangdajing.png" />   
        <input
          placeholder="请输入搜索关键字"
          ref="getValue"
          v-model="inputText"
          maxlength="50"
          @click="searchName"
        />
      </div>
      <div class="search" @click="search">搜索</div>
    </div>
    <div class="history" v-if="pastHistory">     //历史记录默认不显示,有搜索记录才显示
      <div class="hisText">历史记录</div>
      <ul>
        <li v-for="(item,index) in historyList" :key="index">
          <span @click="clickHistory(item,index)">{{item}}</span>
          <img src="../del.png" alt @click="deleteHis(index)" />
        </li>
      </ul>
    </div>

JS部分

//搜索产品列表
    search() {    //点击搜索按钮
      this.filterData.productTitle = this.$refs.getValue.value;   //获取输入框的值
      this.pastHistory = false;   //每次点击搜索后历史记录就隐藏
      
      if (this.$refs.getValue.value !== "") {  //判断输入框的值
        // 每次搜索的值push到新数组里
        this.newArr.push(this.$refs.getValue.value);
		
        this.newArr = this.unique(this.newArr);  //调用unique方法去重
        
        this.list = [];
        for (let i = this.newArr.length; i > 0; i--) {  //数组倒序  最新输入的排在最上面
          this.list.push(this.newArr[i - 1]);
        }
        
        if (this.list.length > 10) {  //最多保存10条
          this.list = this.list.slice(0, 10);
        }
        localStorage.setItem("historyList", JSON.stringify(this.list));   //存localStorage
      }
      
      this.getProductList();  //最后调用接口
    },
    //去重方法封装 
    unique(arr) {
      return arr.filter(function(item, index, arr) {
        return arr.indexOf(item, 0) === index;
      });
    },
    // 点击文本框输入
    searchName() {
      if (localStorage.getItem("historyList")) {   //historyList有值才执行接下来的操作
        let arrlist = JSON.parse(localStorage.getItem("historyList"));
        this.historyList = arrlist;
        if (this.historyList.length !== 0) {  
          this.pastHistory = true;  //有值显示历史记录
        }
      } else {
        this.pastHistory = false;
      }
    },
    // 点击历史记录直接搜索
    clickHistory(item, index) {
      this.pastHistory = false;   
      this.inputText = item;
      //接口前处理
      this.filterData.productTitle = this.inputText;
      this.productList = [];
      this.getProductList();   //调用搜索接口
      let listIndex = index;
      
      this.historyList.splice(0, 0, this.historyList[listIndex]);  //每次点击记录被点击的展示在最前面
      this.historyList = this.unique(this.historyList);  // 去重
      
      localStorage.setItem("historyList", JSON.stringify(this.historyList));  //新数组储存
    },
    //点击删除记录
    deleteHis(index) {
      if (isNaN(index) || index >= this.historyList.length) {
        return false;
      }
      this.historyList.splice(index, 1);
      
      localStorage.setItem("historyList", JSON.stringify(this.historyList));  //保存删除后的新数组
      if (this.historyList.length === 0) {
        this.pastHistory = false;
      }
    },

css代码就不上啦

  • 5
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
Vue2框架中,实现搜索框历史记录可以使用localStorage存储搜索记录,并在页面中展示出来。 下面是一个简单的实现代码: ```html <!-- 搜索框 --> <input type="text" v-model="keyword" @keyup.enter="search" /> <!-- 历史记录 --> <div v-if="history.length > 0"> <h3>搜索历史</h3> <ul> <li v-for="(item, index) in history" :key="index" @click="selectHistory(item)"> {{ item }} </li> </ul> </div> ``` ```javascript export default { data() { return { keyword: '', history: [] } }, mounted() { // 从localStorage中读取历史记录 this.history = JSON.parse(localStorage.getItem('searchHistory')) || [] }, methods: { search() { // 搜索处理 // ... // 存储搜索记录到localStorage中 if (this.keyword.trim() !== '') { this.history.unshift(this.keyword) localStorage.setItem('searchHistory', JSON.stringify(this.history)) } }, selectHistory(item) { // 点击历史记录处理 this.keyword = item this.search() } } } ``` 以上代码中,使用了v-model指令将搜索框的值与Vue实例的`keyword`属性进行绑定,使用了`@keyup.enter`事件监听回车键,触发`search`方法进行搜索处理。在搜索处理中,将搜索关键字存储到历史记录中,并使用`localStorage`进行本地存储。在页面中展示历史记录时,使用`v-for`指令遍历历史记录数组,使用`@click`事件监听点击事件,触发`selectHistory`方法进行搜索处理。 需要注意的是,以上代码只是一个简单的实现示例,实际应用中还需要进行一些优化,比如设置历史记录的最大数量、去重等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值