vue+element-ui实现搜索框

在这里插入图片描述

功能实现
1.点击搜索框,显示列表,点击其它地方搜索列表消失.

在input组件上监听focus与blur事件,设置isFoucus标志量,通过v-if控制搜索列表是否显示,在computed中进行计算。

<el-input   @focus="focus" @blur="blur" />
<el-card v-if="isSearch" >...</el-card>
computed: {
   isSearch() {
      return this.isFocus;
    }
  }
2.按下enter键,也可以进行搜索

在input组件上监听enter事件(注意在组件上使用原生的方法需要使用native关键字)

 <el-input @keyup.enter.native="searchHandler"/>
3.点击搜索框不输入搜索内容,显示热门搜索和历史搜索。点击搜索框输入搜索内容,直接显示搜索列表

显示列表时通过v-if进行控制时候显示,在computed中进行判断是应该显示热门搜索和历史搜索还是直接显示搜索列表

<dl v-if="isHistorySearch">...</dl>
<dl v-if="isSearchList">...</dl>
computed: {
    isHistorySearch() {
      return this.isFocus && !this.search;   //获取了焦点并且搜索框内容为空显示热门搜索和历史搜索
    },
    isSearchList() {
      return this.isFocus && this.search;   //获取了焦点并且搜索框内容不为空直接显示搜索列表
    },
  }
4.将历史搜索内容存储到LocalStorage.

在这里插入图片描述
因为LocalStorage只能以String的形式存在,所以要存的是否要转为String类型,取得是否再转为json,为了方便,可以建立一个通用的store.js

const LOCAL_STORAGE_KEY = "searchHistory";

class Store { }

//通过Store.xxx写法可以把xxx实现静态方法的效果,无需创建Store的实例即可使用

//将[]存入localStorage
Store.saveHistory = (arr) => {
    localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(arr));
}

//从localSotrage取出
Store.loadHistory = () =>JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY))

//清空全部localStorage历史查询
Store.removeAllHistory=()=>{localStorage.removeItem(LOCAL_STORAGE_KEY)}

module.exports = Store

同时我们通过element-ui 的<el-tag>组件,为搜索历史记录提供了好看的样式。而生成的搜索历史样式是随机生成的,可以看见我们之前存入LocalStorage的搜索记录中还存入了对应的tag式样。

types: ["", "success", "info", "warning", "danger"] //搜索历史tag式样

生成[min,max)区间随机数

class RandomUtil {
}
//静态方法,获得随机数
RandomUtil.getRandomNumber = (min, max) => parseInt(Math.random() * (max - min)) + min
module.exports = RandomUtil

同时在存入LocalStorage之前要判断该搜索记录已经存在了,如果存在,则不存入.

如果没有任何搜索记录,将history字段设为false,只显示热门搜索不显示历史搜索

  searchHandler() {
      //随机生成搜索历史tag式样
      let n = RandomUtil.getRandomNumber(0, 5);

      let exist =
        this.historySearchList.filter(value => {
          return value.name == this.search;
        }).length == 0
          ? false
          : true; //判断搜索是否已经存在了
      if (!exist) {
        this.historySearchList.push({ name: this.search, type: this.types[n] });
        Store.saveHistory(this.historySearchList);
      }
      //如果没有搜索记录,history字段为false,只显示热门搜索不显示历史搜索
      this.history = this.historySearchList.length == 0 ? false : true;
    },
5.删除历史记录

处理element-ui tag组件的@close事件,删除掉historySearchList中对应的value,重新存入LocalStorage.同时如果历史记录数为0了,将history字段置为false,使得不显示历史记录。

 <el-tag
      v-for="search in historySearchList"
      :key="search.id"
      closable
      :type="search.type"
       @close="closeHandler(search)">
  </el-tag>
closeHandler(search) {
      this.historySearchList.splice(this.historySearchList.indexOf(search), 1);
      Store.saveHistory(this.historySearchList);
      if (this.historySearchList.length == 0) {//历史记录数为0了,将history字段置为false,使得不显示历史记录。
        this.history = false;
      }
    }

👿但是你会发现这样的话,虽然一条历史记录被删除了,但是每次删除后搜索记录列表就不显示了。
聪明的同学已经想到原因了:点击搜索记录列表内的内容,触发了搜索input输入框的blur事件

解决的方法也很简单:在blur事件中,重置focus焦点时设置timeout,而删除历史记录后,取消掉这个timeout,不重置focus焦点.

blur() {
      var self = this;
      this.searchBoxTimeout = setTimeout(function() {
        self.isFocus = false;
      }, 300);
    },
closeHandler(search) {
      this.historySearchList.splice(this.historySearchList.indexOf(search), 1);
      Store.saveHistory(this.historySearchList);
      //取消timeout
      clearTimeout(this.searchBoxTimeout);
      if (this.historySearchList.length == 0) {
        this.history = false;
      }
    },
搜索框组件源码

搜索框组件源码

  • 26
    点赞
  • 131
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
可以使用element-ui的el-table组件和el-input组件来实现表格搜索框。具体实现步骤如下: 1. 在el-table中添加一个slot="header"的template,用于放置搜索框。 2. 在template中添加一个el-input组件,用于输入搜索关键字。 3. 在el-table的data属性中添加一个filterText属性,用于存储搜索关键字。 4. 在el-table的computed属性中添加一个filteredData属性,用于根据搜索关键字过滤数据。 5. 在el-input的v-model属性中绑定filterText,实现搜索框与数据的双向绑定。 示例代码如下: ``` <template> <div> <el-table :data="tableData" style="width: 100%"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> <template slot="header"> <el-input v-model="filterText" placeholder="请输入搜索关键字"></el-input> </template> </el-table> </div> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18, address: '北京市' }, { name: '李四', age: 20, address: '上海市' }, { name: '王五', age: 22, address: '广州市' }, { name: '赵六', age: 24, address: '深圳市' } ], filterText: '' } }, computed: { filteredData() { return this.tableData.filter(item => { return item.name.toLowerCase().includes(this.filterText.toLowerCase()) || item.address.toLowerCase().includes(this.filterText.toLowerCase()) }) } } } </script> <style> </style> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Selenium399

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值