VUE查询-历史记录功能

查询历史功能是一个很常见且实用的功能,一般搜索框都会默认加上这个功能。我这里使用element vue组件来实现,原理很简单:查询过的数据放到localStorage,代码简单易用,效果图如下:

代码如下:

html 复制代码
<el-input
  v-model="searchName"
  placeholder="请输入,按回车键过滤"
  style="width: 250px; margin-left: 10px; margin-right: 5px"
  :clearable="true"
  @change="searchSdmid()"
/>

<el-dropdown style="margin-right: 10px">
  <el-button type="text" style="color: #909399">
    <i class="el-icon-time"></i>
  </el-button>
  <el-dropdown-menu v-if="searchHistory.length === 0" slot="dropdown">
    <el-dropdown-item>
      <div class="item-add-padding">无历史记录</div>
    </el-dropdown-item>
  </el-dropdown-menu>

  <el-dropdown-menu v-else slot="dropdown">
    <el-dropdown-item v-for="(item, index) in searchHistory" :key="index" class="clear-padding">
      <div class="item-add-padding" @click="handleSelectSearch(item)">{{ item }}</div>
    </el-dropdown-item>
    <el-dropdown-item divided style="color: #e6a23c">
      <div class="item-add-padding" @click="clearSearches()">清空历史记录</div>
    </el-dropdown-item>
  </el-dropdown-menu>
</el-dropdown>


export default {
  data() {
    return {
      searchName: '',
      searchKey: '_search_sdmid_',
      searchHistory: [],
    }
  },
  created() {
    this.$nextTick(() => {
      this.searchHistory = this.getSearches()
    })
  },
  methods: {
    //查询
    searchData() {
      if (this.searchName !== '') {
        this.saveSearch(this.searchName)
        this.searchHistory = this.getSearches()

        //下面是过滤数据操作
        ...
      }else{
        return false
      }
    },

    //处理选择后的查询
    handleSelectSearch(item) {
      this.searchName = item
      this.searchData()
    },

    //插入数据处理
    insertArray(arr, val, compare, maxLen) {
      const index = arr.findIndex(compare)
      if (index === 0) return
      if (arr.indexOf(val) === 1) return

      if (index > 0) arr.splice(index, 1)
      arr.unshift(val)
      if (maxLen && arr.length > maxLen) arr.pop()
    },

    //保存查询记录到缓存
    saveSearch(query) {
      const MAX_LENGTH = 10
      var searches = JSON.parse(localStorage.getItem(this.searchKey)) || []
      this.insertArray(searches, query, (item) => item === query, MAX_LENGTH)
      localStorage.setItem(this.searchKey, JSON.stringify(searches))
      return searches
    },

    //获取查询记录
    getSearches() {
      return JSON.parse(localStorage.getItem(this.searchKey)) || []
    },

    //清除查询记录
    clearSearches() {
      localStorage.removeItem(this.searchKey)
      this.searchHistory = []
    },
  }
}
相关推荐
开开心心就好4 小时前
批量提取PDF中的图片,直接导出原图
前端·javascript·支持向量机·智能手机·pdf·html·启发式算法
Setsuna_F_Seiei6 小时前
前端转型 Agent 开发 05 之 Agent Hooks 与 Checkpointer(让 Agent 从全自动转变人为可掌控)
前端·agent·ai编程
百万蹄蹄向前冲8 小时前
风扇转了一晚上MVP专家团翻车事故
前端·人工智能
默_笙8 小时前
🏛 给 AI 配一间办公室:Harness Engineering 六大模块与它的实现
前端·javascript
linux_cfan9 小时前
videojs v10 源代码系列解读:14 · 谓词守卫:在运行时安全地调用能力
前端·javascript·音视频
kyriewen10 小时前
我扒了 10,221 条 JD:腾讯技术岗 75% 在要 AI
前端·人工智能·ai编程
郑州光合科技余经理10 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
+VX:Fegn089511 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
雪芽蓝域zzs11 小时前
第 7 章:双 Y 轴组合图(柱状 + 折线,看板高频场景)
vue.js·echars
IT_陈寒11 小时前
Vue的响应式让我熬到凌晨三点,原来漏了这个小细节
前端·人工智能·后端