zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    吴裕雄 python深度学习与实践(1)
    吴裕雄 python 机器学习-Logistic(1)
    吴裕雄 python 熵权法确定特征权重
    【Uva 1252】Twenty Questions
    【玲珑杯 round#18 B】图论你先敲完模板
    【Uva 10817】Headmaster's Headache
    【玲珑杯 round#18 A】计算几何你瞎暴力
    【Uva 12128】Perfect Service
    【UVa 12186】Another Crisis
    【Uva 10003】Cutting Sticks
  • 原文地址:https://www.cnblogs.com/gzw-23/p/12768737.html
Copyright © 2011-2022 走看看