欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Vue實(shí)現(xiàn)關(guān)鍵詞實(shí)時搜索高亮顯示關(guān)鍵詞

 更新時間:2018年07月21日 10:49:32   作者:IT0315  
這篇文章主要介紹了基于Vue實(shí)現(xiàn)關(guān)鍵詞實(shí)時搜索高亮顯示關(guān)鍵詞,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近在做移動real-time-search于實(shí)時搜索和關(guān)鍵詞高亮顯示的功能,通過博客的方式總結(jié)一下,同時希望能夠幫助到別人~~~

如果不喜歡看文字的朋友我寫了一個demo方便已經(jīng)上傳到了github上,可以clone下來直接看代碼 https://github.com/IT0315/real-time-search.git

下面是demo運(yùn)行的效果圖

好了閑話不多說直接上代碼

實(shí)時搜索

實(shí)時搜索通過觸發(fā)input事件和定時器來實(shí)現(xiàn)

<input v-model="keyWords" type="text" placeholder="請輸入關(guān)鍵詞" @input="handleQuery">

在每次輸入框的值變化的時候都會執(zhí)行handleQuery方法

 clearTimer () {
   if (this.timer) {
    clearTimeout(this.timer)
   }
  },
  handleQuery (event) {
   this.clearTimer()
   console.log(event.timeStamp)
   this.timer = setTimeout(() => {
    console.log(event.timeStamp)
    // console.log(this.lastTime)
    // if (this.lastTime - event.timeStamp === 0) {
    this.$http.post('/api/vehicle').then(res => {
     console.log(res.data.data)
     this.changeColor(res.data.data)
    })
    // }
   }, 2000)
  },

在handleQuery方法中有一個定時器,通過設(shè)置時間來控制搜索的執(zhí)行,由于輸入時input的框中的值總是變化的,所以每次變化都會執(zhí)行一次handleQuery,我們通過clearTimer方法清除timer定時器,如果兩次輸入的間隔時間小于你設(shè)置的時間間隔(2s)的話第一個定期器將會被清除,同時執(zhí)行第二個定時器。這樣就實(shí)現(xiàn)了實(shí)施搜多的控制,而不是每次輸入的時候就去請求數(shù)據(jù)。

注意:如果時間設(shè)置過短或者說我們服務(wù)器請求較慢的話,可能第一次查詢還沒有返回便進(jìn)行了第二次查詢,那么返回的數(shù)據(jù)將是兩次查詢的結(jié)果,造成查詢結(jié)果的混亂,如果使用的是axios可以利用axios.CancelToken來終止上一次的異步請求,防止舊關(guān)鍵字查詢覆蓋新輸入的關(guān)鍵字查詢結(jié)果。

高亮顯示

通過RegExp實(shí)現(xiàn)對關(guān)鍵詞的替換,通過添加class實(shí)現(xiàn)關(guān)鍵詞高亮顯示

 changeColor (resultsList) {
   resultsList.map((item, index) => {
    // console.log('item', item)
    if (this.keyWords && this.keyWords.length > 0) {
     // 匹配關(guān)鍵字正則
     let replaceReg = new RegExp(this.keyWords, 'g')
     // 高亮替換v-html值
     let replaceString =
      '<span class="search-text">' + this.keyWords + '</span>'
     resultsList[index].name = item.name.replace(
      replaceReg,
      replaceString
     )
    }
   })
   this.results = []
   this.results = resultsList
}

在查詢到結(jié)果后執(zhí)行changeColor方法將查詢出來的數(shù)據(jù)傳遞過來通過RegExp來將關(guān)鍵詞替換成huml標(biāo)簽,同時用vue中的v-html進(jìn)行綁定。最后將demo完整的代碼展示出來

<template>
 <div class="Home">
  <input v-model="keyWords" type="text" placeholder="請輸入關(guān)鍵詞" @input="handleQuery">
  <ul>
    <li v-for="(item,index) in results" :key='index' v-html='item.name'></li>
  </ul>
 </div>
</template>

<script>
export default {
 name: 'Home',
 data () {
  return {
   keyWords: '',
   results: []
  }
 },
 methods: {
  clearTimer () {
   if (this.timer) {
    clearTimeout(this.timer)
   }
  },
  handleQuery (event) {
   this.clearTimer()
   console.log(event.timeStamp)
   this.timer = setTimeout(() => {
    console.log(event.timeStamp)
    // console.log(this.lastTime)
    // if (this.lastTime - event.timeStamp === 0) {
    this.$http.post('/api/vehicle').then(res => {
     console.log(res.data.data)
     this.changeColor(res.data.data)
    })
    // }
   }, 2000)
  },

  changeColor (resultsList) {
   resultsList.map((item, index) => {
    // console.log('item', item)
    if (this.keyWords && this.keyWords.length > 0) {
     // 匹配關(guān)鍵字正則
     let replaceReg = new RegExp(this.keyWords, 'g')
     // 高亮替換v-html值
     let replaceString =
      '<span class="search-text">' + this.keyWords + '</span>'
     resultsList[index].name = item.name.replace(
      replaceReg,
      replaceString
     )
    }
   })
   this.results = []
   this.results = resultsList
  }
 }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
.search-text{
color: red;
}
</style>

最后,如果本文對你的學(xué)習(xí)或者工作有幫助的話,麻煩給個star鼓勵下啦~~~

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論