vue實現搜索功能,匹配字段高亮顯示


文章參考:https://blog.csdn.net/qq_42268364/article/details/100655770?depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-9&utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-9

實現搜索代碼:
methods:{ search() {
if (this.keyword == '') { //如果沒有輸入內容,不讓往下執行 return } this.resultList = [] //每次搜索對結果數組做初始化 this.deviceList.forEach((item) => { //把初始數據進行遍歷 /** 下面是把初始數據中的每一條數據的四個字段分別去和輸入的內容進行匹配, 如果某一字段中包含輸入的內容,就往resultList中加 **/ if (item.name.indexOf(this.keyword) > -1 || item.date.indexOf(this.keyword) > -1 || item.adress.indexOf(this.keyword) > -1 || item.type.indexOf(this.keyword) > -1) { this.resultList.push(item) } }) if (this.resultList.length == 0) { //如果沒有匹配結果,就顯示提示信息 this.isShowTip = true } } }
實現高亮顯示:主要技術點(map映射)
this.resultList.map((item) => {
            item.name = this.brightKeyword(item.name)
            item.date = this.brightKeyword(item.date)
            item.adress = this.brightKeyword(item.adress)
            item.type = this.brightKeyword(item.type)
          })
        })
    },
    brightKeyword (val) {
      let keyword = this.keyword
      if (val.indexOf(keyword) !== -1) {
        return val.replace(keyword, `<font color='#42cccc'>${keyword}</font>`)
      } else {
        return val
      }
    }
  }
完整代碼:
<template>
  <div class="bright-index">
    <div class="search-box">
      <input type="text" v-model="keyword" class="input" placeholder="請輸入搜索內容, 提示:搜索設備">
      <button class="btn" @click="search">搜索</button>
    </div>
    <div class="content-box">
      <div class="content-card" v-for="(item ,index) in resultList" :key="index">
        設備名稱:<span v-html="item.name" style="color:#000;"></span>
        <span>日期:<span v-html="item.date"></span></span>
        <span>地址:<span v-html="item.adress"></span></span>
        <span>類型:<span v-html="item.type"></span></span>
      </div>
      <h2 v-if="isShowTip">沒有搜索到匹配結果</h2>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      keyword: '',
      deviceList: [],
      resultList: [],
      isShowTip: false
    }
  },
  methods: {
    search () {
      this.isShowTip = false
      if (this.keyword == '') {
        this.$message.warning('請輸入搜索內容')
        return
      }
      this.$axios.get('../../../static/mock/device.json')
        .then((res) => {
          this.deviceList = res.data.data.deviceList
        }).then(() => {
          this.resultList = []
          this.deviceList.forEach((item) => {
            if (item.name.indexOf(this.keyword) > -1 ||
              item.date.indexOf(this.keyword) > -1 ||
              item.adress.indexOf(this.keyword) > -1 ||
              item.type.indexOf(this.keyword) > -1) {
              this.resultList.push(item)
            }
          })
          if (this.resultList.length == 0) {
            this.isShowTip = true
          }
          this.resultList.map((item) => {
            item.name = this.brightKeyword(item.name)
            item.date = this.brightKeyword(item.date)
            item.adress = this.brightKeyword(item.adress)
            item.type = this.brightKeyword(item.type)
          })
        })
    },
    brightKeyword (val) {
      let keyword = this.keyword
      if (val.indexOf(keyword) !== -1) {
        return val.replace(keyword, `<font color='#42cccc'>${keyword}</font>`)
      } else {
        return val
      }
    }
  }
}
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM