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 = []
    },
  }
}
相关推荐
神探小白牙11 分钟前
eCharts 多系列柱状图增加背景图
javascript·ecmascript·echarts
女生也可以敲代码16 分钟前
AI时代下的50道前端开发面试题:从基础到大模型应用
前端·面试
ZhengEnCi23 分钟前
M5-markconv自定义CSS样式指南 📝
前端·css·python
IT_陈寒41 分钟前
SpringBoot自动配置的坑差点让我加班到天亮
前端·人工智能·后端
xingpanvip42 分钟前
星盘接口开发文档:星相日历接口指南
android·开发语言·前端·css·php·lua
@PHARAOH1 小时前
WHAT - GitLens supercharged 插件
前端
TT模板1 小时前
苹果cms整合西瓜播放器XGplayer插件支持跳过片头尾
前端·html5
Wect2 小时前
React 性能优化精讲
前端·react.js·性能优化
追风筝的人er2 小时前
SpringBoot+Vue3 企业考勤如何处理法定假期?节假日方案、调休补班与工作日判断链路拆解
前端·vue.js·后端
无敌的黑星星3 小时前
Java8 CompletableFuture 实战指南
linux·前端·python