H5项目输入框搜索相关业务功能

 输入搜索内容列表展示关键字高亮

<!-- 输入搜索 列表展示相关内容 --S -->
<span v-html="showKeyWord(info.scenicSpotName)"></span> //scenicSpotName为数组展示的列表内容
showKeyWord(text) {
    const highlighStr = `<span style="color:#5a9dda; font-weight: bold;">${this.value}</span>`;
    const reg = new RegExp(this.value, "gi");
    return text.replace(reg, highlighStr)
}

点击删除按钮删除全部历史记录

deleteHistory() {
    this.$dialog.confirm({
        message: '是否删除历史记录',
    })
        .then(() => {
            localStorage.removeItem('history')
            this.searchHistory = []
        })
        .catch(() => {
            // on cancel
        })
},

输入框内容变化时请求数据的防抖处理

inputChange(value) {
    // 定义延时器输入框的防抖处理
    this.showhistory = false
    this.type = 0;
    clearTimeout(this.timer)
    this.timer = setTimeout(() => {
        this.kw = value
        this.queryKeyword(this.kw);
    }, 700)
    if (this.showclose == true) return
    this.showclose = true;
},

点击关闭当前搜索历史列表的tag标签事件

// 点击关闭tag标签事件
close(index) {
   this.searchHistory.splice(index, 1)
},

输入框点击搜索按钮事件,对搜索历史进行本地缓存

searchInputButton() {
    this.showclose = true;
    this.showhistory = false;
    this.type = 1
    this.queryKeyword(this.value);
    if (this.value == "") {
        this.value = this.placeholder
        return
    }
    let val = this.value
    if (this.searchHistory.indexOf(val) == -1) {
        // searchHistory存储历史数据的数组
        this.searchHistory.unshift(this.value)
        // 页面只允许展示10条数据
        this.searchHistory = this.searchHistory.slice(0, 10)
        // 进行本地存储
        localStorage.setItem('history', JSON.stringify(this.searchHistory))
    }
},

mounted() {
    if (JSON.parse(localStorage.getItem('history'))) {
        this.searchHistory = JSON.parse(localStorage.getItem('history'))
    }
},
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值