vue inpt框搜索和保存历史记录

页面结果:
在这里插入图片描述
代码如下:
html:

<div class="search-container flex">
      <div class="euiFormControlLayout--fullWidth flex-1">
        <div class="shuruk" style="position: relative">
          <el-input
            v-model="searchss"
            style="flex: 1"
            clearable
            placeholder="请输入关键字"
            @focus="focus"
            @input="input"
            id="input"
            @keypress.native.enter="throttleChang"
          ></el-input>
          <div class="history" v-if="historyFlag" id="history">
            <div class="field">
              <div class="title">字段名称</div>
              <div class="record-item">
                <div
                  v-for="(item, index) in history.field"
                  :key="'field' + index"
                  class="record-list-item"
                  style="margin-bottom: 5px"
                >
                  <div
                    :id="'field' + index"
                    @click="handlePiteh(item)"
                    class="pointer search-history"
                  >
                    {{ item }}
                  </div>
                </div>
              </div>
            </div>
            <div class="record">
              <div class="title">历史记录</div>
              <div class="record-item">
                <div
                  v-for="(item, index) in history.record"
                  :key="index"
                  class="record-list-item"
                >
                  <div
                    :id="'record' + index"
                    @click="handlePiteh(item)"
                    class="pointer search-history"
                  >
                    {{ item }}
                  </div>
                </div>
              </div>
              <div
                class="blueColor pointer clearbtn"
                @click="clearData"
                v-show="history.record.length > 0"
              >
                清除历史记录
              </div>
            </div>
          </div>
        </div>
       
        <div class="search-s">
          <span @click="throttleSous">搜索</span>
        </div>
      </div>
      <div class="tips-calss">
        <span class="yufa" @click="dialogVisible = true">查询语法</span>
      </div>
    </div>

js:

data里面:
 history: {
        field: [],
        record: [],
      },
      historyFlag: false,
      historyRecord: [],
      historyField: [],
 qetSearchFields() {
      qetSearchFields().then(res => {
        this.history.field = res.data
        this.historyField = res.data
      })
    },
    //-------------------------------------------搜索记录提示---------------------------------
 
    removeRecordHeightLight() {
      if (this.historyFlag == true) {
        for (let i = 0; i < this.history.record.length; i++) {
          this.$nextTick(() => {
            if (document.querySelector("#record")) {
              document.querySelector("#record" + i).innerHTML =
                this.history.record[i];
            }
          });
        }
      }
    },
    removeFieldHeightLight() {
      if (this.historyFlag == true) {
        for (let i = 0; i < this.history.field.length; i++) {
          this.$nextTick(() => {
            if (document.querySelector("#field" + i)) {
              document.querySelector("#field" + i).innerHTML =
                this.history.field[i];
            }
          });
        }
      }
    },
    handleDeplicate(arr) {
      //去重
      let newArr = [];
      for (var i = 0; i < arr.length; i++) {
        if (newArr.indexOf(arr[i]) == -1) {
          newArr.push(arr[i]);
        }
      }
      return newArr;
    },
    focus() {
      this.historyFlag = true;
      // this.historyRecord = JSON.parse(localStorage.getItem("history2"));
      // this.history.record = this.historyRecord;
      // this.input(this.searchss);
      this.input();
    },
    clearData() {
      store.dispatch("setHistory2", JSON.stringify([]));
      this.history.record = [];
      this.historyRecord = [];
    },
    input() {
      this.historyFlag = true;
      this.historyRecord = JSON.parse(localStorage.getItem("history2"));
      this.history.record = this.historyRecord;
      let value = this.searchss;
      let arr = []; //历史记录
      let fieldArr = []; //字段名称
      for (let i = 0; i < this.historyRecord.length; i++) {
        if (this.historyRecord[i].indexOf(value) != -1) {
          arr.push(this.historyRecord[i]);
        }
      }
      for (let i = 0; i < this.historyField.length; i++) {
        if (this.historyField[i].indexOf(value) != -1) {
          fieldArr.push(this.historyField[i]);
        }
      }
      if (arr.length == 0) {
        this.history.record = this.historyRecord;
        this.removeRecordHeightLight();
      } else {
        this.history.record = arr;
        this.recordhighLight(value);
      }
      if (fieldArr.length == 0) {
        this.history.field = this.historyField;
        this.removeFieldHeightLight();
      } else {
        this.history.field = fieldArr;
        this.fieldhighLight(value);
      }
    },
    recordhighLight(value) {
      if (this.historyFlag == true) {
        for (let i = 0; i < this.history.record.length; i++) {
          let newTxt = this.history.record[i]
            .split(value)
            .join('<span style="color:#4388FC">' + value + "</span>");
          this.$nextTick(() => {
            if (document.querySelector("#record")) {
              document.querySelector("#record" + i).innerHTML = newTxt;
            }
          });
        }
      }
    },
    fieldhighLight(value) {
      if (this.historyFlag == true) {
        for (let i = 0; i < this.history.field.length; i++) {
          let newTxt = this.history.field[i]
            .split(value)
            .join('<span style="color:#4388FC">' + value + "</span>");
          this.$nextTick(() => {
            console.log(document.querySelector("#field" + i), "nets11");
            if (document.querySelector("#field" + i)) {
              document.querySelector("#field" + i).innerHTML = newTxt;
            }
          });
        }
      }
    },
    handlePiteh(value) {
      this.searchss = value;
      this.historyFlag = false;
      this.queryFn();
    },
    clickHistory(event) {
      const ev = window.event || event;
      const path = ev.composedPath && ev.composedPath();
      let that = this;
      let isHsa = path.find((_) => {
        return _.id === "input";
      });
      let isHsa2 = path.find((_) => {
        return _.id === "history";
      });
      if (isHsa || isHsa2) {
      } else {
        that.historyFlag = false;
      }
    },

js里面是注意点是:

created里 this.qetSearchFields(); 点击搜索请求成功时  if (this.listQuery.searchKeywords) {
          this.historyRecord.push(this.listQuery.searchKeywords)
        }                               this.listQuery.searchKeywords //这个变量是我定的搜索变量,你换成你的就行

 mounted() {
    window.addEventListener("click", this.clickHistory);
  },
  destroyed() {
    window.removeEventListener("click", this.clickHistory);
  },

css:

 .history {
    position: absolute;
    width: 740px;
    height: 300px;
    background-color: #ffffff;
    z-index: 999;
    border-radius: 4px;
    box-shadow: 0px 10px 20px 0px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: row;
    font-size: 13px;
    overflow: hidden;
    .field {
      width: 250px;
      height: 100%;
      display: flex;
      flex-direction: column;
      padding-left: 19.5px;
      padding-right: 10px;
      padding-bottom: 40px;
      .title {
        height: 40px;
        font-size: 13px;
        font-family: PingFang SC;
        font-weight: 600;
        line-height: 40px;
      }
      .record-item {
        flex: 1;
        overflow-y: auto;
      }
    }
    .record {
      flex: 1;
      box-shadow: 0px 1px 2px 0px rgba(0, 20, 61, 0.1);
      display: flex;
      flex-direction: column;
      padding-left: 19.5px;
      padding-right: 10px;
      .title {
        height: 40px;
        font-size: 13px;
        font-family: PingFang SC;
        font-weight: 600;
        line-height: 40px;
      }
      .record-item {
        flex: 1;
        overflow-y: auto;
      }
      .clearbtn {
        height: 40px;
        line-height: 40px;
      }
    }
  }
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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`方法进行搜索处理。 需要注意的是,以上代码只是一个简单的实现示例,实际应用中还需要进行一些优化,比如设置历史记录的最大数量、去重等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值