搜索,前端模糊过滤,高亮输入项

有两种类型看需求封装

<rich-text v-else :nodes="item.value"></rich-text>

类型一,每个字都高亮

onInput(){
    // 重置数组 
    this.filterList = JSON.parse(JSON.stringify(this.communityList))
    // 过滤数组 
    this.filterList = this.filterList.filter(item => item.value.includes(this.valueInp))
    // 数组项高亮 
    this.filterList.forEach(item => {
        let fontres = ''
        for(let fontItem of item.value) {
            if(this.valueInp.includes(fontItem)) 
                fontres += `<font style='color: #0378BC'>${fontItem}</font>`
            else 
                fontres += fontItem
        }
        item.value = fontres
    });
    console.log(this.filterList);
},

效果


image.png

类型二,按单词高亮第一项

onInput(){
    // 重置数组 
    this.filterList = JSON.parse(JSON.stringify(this.communityList))
    // 过滤数组 
    this.filterList = this.filterList.filter(item => item.value.includes(this.valueInp))
    // 数组项高亮 
    this.filterList.forEach(item => {
        item.value = item.value.replace(this.valueInp, `<font style='color: #0378BC'>${this.valueInp}</font>`)
    });
    console.log(this.filterList);
},

效果


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容