vue纯前端留痕,搜索框历史记录,获取焦点,失去焦点历史记录消失

效果图

 

html代码

<div class="search">
        <div class="search-btn">
          <el-input
            v-model.trim="searchContent"
            size="small"
            style="width: 193px"
            class="mr10"
            placeholder="请输入搜索内容"
            @focus="showHistory"
            @blur="hiddenHistory"
          >
          </el-input>
          <el-button type="primary" size="small" @click="search">搜索</el-button>
        </div>
        <div :class="[isHistoryShow ? 'search-history' : 'show']">
          <div class="search-history-header">
            <span class="search-history-header__title">搜索记录</span>
            <span
              v-if="historyShowList&&historyShowList.length > 0"
              class="search-history-header__icon"
              @click="clearHistory"
            >
              <i class="el-icon-delete"></i></span>
          </div>
          <div
             v-if="historyShowList&&historyShowList.length > 0"
             class="search-history-content">
            <ul>
              <li
               v-for="item in historyShowList"
              :key="item"
               @click="historySearch(item)">{{ item }}</li>
            </ul>
          </div>
          <div v-else class="search-history-none">
            暂无数据
          </div>
        </div>
</div>

2.js部分代码

 data() {
    return {
      activeName: 'table',
      isHistoryShow: false,
      historyList: [],
      historyShowList: [],
      localList: {
        'table': 'dataMapTableHistory',
        'topic': 'dataMapTopicHistory',
        'api': 'dataMapApiHistory'
      }
    }
  }, 
methods: {
    selectProject() {
      this.$refs['table'][0].refresh()
    },
    search() {
      if (this.removeDedup(JSON.parse(localStorage.getItem(this.localList[this.activeName])))) {
        this.historyList.push(this.searchContent)
        localStorage.setItem(this.localList[this.activeName], JSON.stringify(this.historyList))
      }
      this.selectProject()
    },
    removeDedup(removeList) {
      let isSave = true
      removeList?.forEach(item => {
        if (this.searchContent === item) isSave = false
      })
      return isSave
    },
    historySearch(val) {
      this.searchContent = val
      this.selectProject()
    },
    showHistory() {
      this.historyShowList = JSON.parse(localStorage.getItem(this.localList[this.activeName]))
      this.isHistoryShow = true
    },
    clearHistory() {
      localStorage.removeItem(this.localList[this.activeName])
    },
    hiddenHistory() {
      setTimeout(() => {
        this.isHistoryShow = false
      }, 300)
    },
}

对于触发失去焦点事件,点击历史记录失效,采用的是setTimeout(() => {},300),延迟300ms,就可以触发点击事件了

1.先保存数据到本地,我这里使用的是localstorage。2.保存数据要去重,防止重复保存,这里使用JSON.stringfy保存数据是将数据JSON化,防止保存数据变为字符串

search() {
      const val = JSON.parse(localStorage.getItem(this.localList[this.activeName]))
      if (this.removeDedup(val)) {
        this.historyList.push(this.searchContent)
        localStorage.setItem(this.localList[this.activeName],
        JSON.stringify(this.historyList))
      }
      this.selectProject()
    },
removeDedup(removeList) {
      let isSave = true
      removeList?.forEach(item => {
        if (this.searchContent === item) isSave = false
      })
      return isSave
    },

3.触发focus(输入框获取焦点)事件,和blur(输入框失去焦点事件)

    showHistory() {
      this.historyShowList = 
      JSON.parse(localStorage.getItem(this.localList[this.activeName]))
      this.isHistoryShow = true
    },
    hiddenHistory() {
      setTimeout(() => {
        this.isHistoryShow = false
      }, 300)
    },

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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`方法进行搜索处理。 需要注意的是,以上代码只是一个简单的实现示例,实际应用中还需要进行一些优化,比如设置历史记录的最大数量、去重等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值