vue 搜索框带历史记录

<template>
  <div>
    <div style="margin-top: 15px; width: 80%">
      <input
        placeholder="请输入内容"
        v-model="searchValue"
        class="input-with-select"
        clearable
        ref="input"
        @focus="onInputFocus"
        @blur="onInputBlur"
        @input="onInputFocus"
      />
      <select v-model="select" slot="prepend" placeholder="热舞广泛的给">
        <option label="餐厅名" value="1"></option>
        <option label="订单号" value="2"></option>
        <option label="用户电话" value="3"></option>
      </select>
      <button slot="append" icon="icon-search">搜索</button>

      <div class="selset_wrap" v-if="selsetWrapShow">
        <div class="selset_wrap_title">历史记录</div>
        <div
          class="selset_wrap_list"
          v-for="(item, index) in inputData"
          :key="index"
        >
          <div class="selset_wrap_list_list">
            <div
              class="selset_wrap_list_list_name"
              @mousedown.prevent="searchValueFn(item)"
            >
              {{ item.name }}
            </div>
            <div
              class="selset_wrap_list_list_remove"
              @mousedown.prevent="removeFn(index)"
            >
              <i class="icon-delete"></i>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchValue: "",
      select: "餐厅名",
      restaurants: [],
      state: "",
      timeout: null,
      selsetWrapShow: false,
      inputData: [
        { name: "在知识的山峰上登得越高,眼前的景色越壮阔", id: "1" },
        { name: "万事皆由人的意志创造。", id: "2" },
        { name: "万般皆是命,半点不由人", id: "3" },
        { name: "身外障碍事小,心中障碍事大。", id: "4" },
        {
          name: "实力的来源不是胜利。唯有奋斗才能增强实力。当你历经苦难而不气馁,那就是实力。唯有奋斗才能增强实力。当你历经苦难而不气馁,那就是实力唯有奋斗才能增强实力。当你历经苦难而不气馁,那就是实力",
          id: "5",
        },
        {
          name: "实力的来源不是胜利。唯有奋斗才能增强实力。当你历经苦难而不气馁,那就是实力。",
          id: "6",
        },
      ],
    };
  },
  methods: {
    onInputFocus() {
      this.selsetWrapShow = true;
      console.log("获取焦点");
    },
    onInputBlur() {
      this.selsetWrapShow = false;
      console.log("失去焦点");
    },
    searchValueFn(val) {
      this.selsetWrapShow = false;
      this.searchValue = val.name;
      this.$refs["input"].blur();
      console.log(this.searchValue);
    },
    removeFn(index) {
      this.selsetWrapShow = true;
      console.log("删除");
      this.inputData.splice(index, 1);
    },
  },
  mounted() {},
};
</script>

<style>
.selset_wrap {
  /* border: 1px solid; */
  margin-top: 5px;
  background: #ffffff;
  box-shadow: 3px 3px 3px 3px #999999;
}
.selset_wrap_title {
  color: #999999;
  height: 32px;
  width: 96%;
  line-height: 32px;
  margin: 0 auto;
  font-size: 12px;
}
.input-group__prepend {
  width: 60px;
}

.selset_wrap_list {
  /* height: 32px; */
  width: 100%;
  font-size: 12px;
  cursor: pointer;
}
.selset_wrap_list:hover {
  color: #fe774e;
  background: #f5f5f5;
}
.selset_wrap_list_list {
  width: 96%;
  line-height: 32px;
  margin: 0 auto;
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
}
.selset_wrap_list_list_name {
  width: 97%;
}
.selset_wrap_list_list_remove {
  width: 2%;
}
</style>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值