欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vue实现搜索关键字高亮

程序员文章站 2022-07-14 10:48:36
...

Recently i got a development assignment,it is interesting,below is a effect picture

vue实现搜索关键字高亮

wxml

<view class="resultBox" wx:if='{{keyword}}'>
      <view wx:for='{{searchList}}' wx:for-item='item' wx:key="{{index}}" class="resultItem">
        <text wx:for='{{item.title}}' wx:for-item='itm'  class="{{itm==keyword?'matchedText':''}}">{{itm}}</text>
      </view>
</view>

js

  data: {
    inputFocus: false,
    keyword: '',
    searchList: [{
      title: "入门指南入门的入门"
    }, {
      title: "高手的入门精髓"
    }, {
      title: "看看大佬入门网购经验"
    }]
  }

  keywordChange(e) {
    var that=this
    setTimeout(function(){
      that.showResult(e,that)
    },500)
  },
  showResult(e,that){
    var inputVal = e.detail.value
    that.setData({
      keyword: inputVal
    })
    var searchArray = that.data.searchList
    searchArray.forEach((item, index) => {
      item.title = item.title.split(inputVal).join('*' + inputVal + '*').split("*");
      console.log(item.title);
    })
    that.setData({
      searchList: searchArray
    })
  },
相关标签: vue