搜索关键词标红组件

搜索关键词标红功能

今年做的一个需求,先看效果图。
在这里插入图片描述
先讲一下实现方式,前端输入关键词查询,后端返回html模板,前端通过v-html渲染
查到的数据是分页式,v-html的样式需要使用/deep/声明
下面是组件代码

<template>
  <div class="result-box">
    <div id="box-top"></div>
    <div class="search-list" v-for="(item, index) in searchWordsList">
      <div class="report-title">
        <img
          class="icon mini"
          v-show="item.reportSource === 1"
          src="../../assets/2023/icon_02.png"
          alt=""
        />
        <img
          class="icon"
          v-show="item.reportSource === 2"
          src="../../assets/2023/icon_01.png"
          alt=""
        />
        <div class="h5-icon" v-show="item.reportSource === 3">H5</div>
        <div class="reportName-html" @click="OpenDetails(item, index)" v-html="item.reportNameDescription">
        </div>
        &nbsp;
        <el-tag
          style="cursor: pointer;"
          v-show="item.hasAuth && item.isActive!=3"
          @click="GoSelect(item, index)"
          type="success"
          size="mini"
        >
          直接运行
        </el-tag>
        <el-tag
          style="cursor: pointer;"
          v-show="item.hasAuth && item.isActive==3"
          type="warning"
          size="mini"
        >
          禁用
        </el-tag>
        <el-tag v-show="!item.hasAuth" type="info" size="mini">
          未授权
        </el-tag>
      </div>
      <div class="html-template" v-html="item.description"></div>
      <div class="report-path">{{ item.catalogPath }}</div>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    searchWordsList: {
      type: Array,
      default: () => [],
    },
    id: {
      type: Number
    }
  },
  data() {
    return {
      templateCode: '<div class="desc">描述描述描述<span>标红</span></div>',
    }
  },
  created() {},
  mounted() {},
  watch: {
    searchWordsList(n, o) {
      console.log('搜索结果变化', n)
      this.GoListTop()
    },
  },
  methods: {
    // 打开详情
    OpenDetails(data, index) {
      if (data.hasDetail) {
        this.$emit('OpenDetails', data, data.reportId, index)
      } else {
        this.$message.warning('暂无可看详情')
      }
    },
    // 打开报表
    GoSelect(data, index) {
      if (data.hasAuth) {
        this.$emit('StartReport', data, index)
      } else {
        this.$message.warning('暂无权限')
      }
    },
    GoListTop() {
      document.querySelector('#box-top').scrollIntoView({
        behavior: 'smooth',
      })
    },
  },
}
</script>
<style lang="less" scoped>
.search-list {
  width: 100%;
  border-bottom: 1px solid rgba(34, 34, 34, 0.1);
  margin-bottom: 10px;
  .report-title {
    width: 100%;
    height: 20px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #3e86ff;
    line-height: 20px;
    display: flex;
    align-items: center;
    margin-bottom: 2px;
    .icon {
      width: 18px;
      height: 18px;
      display: block;
      margin-right: 8px;
      &.mini {
        width: 16px;
      }
    }
    .h5-icon {
      width: 18px;
      height: 18px;
      background: #eaf2ff;
      border-radius: 3px;
      font-size: 12px;
      font-family: SourceHanSansCN-Bold, SourceHanSansCN;
      font-weight: bold;
      color: #3f86ff;
      text-align: center;
      line-height: 18px;
      margin-right: 8px;
    }
    .reportName-html {
      // width: 100%;
      position: relative;
      /deep/.reportName {
        border-bottom: #3e86ff 1px solid;
        cursor: pointer;
        > span {
          color: red;
        }
      }
    }
  }
  .html-template {
    width: 100%;
    position: relative;
    /deep/.desc {
      width: 100%;
      font-size: 12px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: rgba(34, 34, 34, 0.7);
      line-height: 20px;
      margin-bottom: 5px;
      white-space: pre-line; // 合并空白符序列,但保留换行符
      > span {
        color: red;
      }
    }
  }
  .report-path {
    color: #929292;
    font-size: 12px;
    margin-bottom: 9px;
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用ElasticsearchRepository进行查询时,可以通过自定义ResultMapper来实现标红关键词的功能。可以参考以下步骤来实现: 1. 首先,创建一个自定义的ResultMapper类,该类需要实现org.springframework.data.elasticsearch.core.ResultsMapper接口。 2. 在自定义的ResultMapper类中,重写mapResults方法。在该方法中,可以通过获取到的SearchHit对象来获取帖子的相关信息,包括标题和内容。 3. 在获取到标题和内容后,可以使用HTML标签或其他方式将关键词进行标红处理。可以使用正则表达式或其他方法来匹配并替换关键词。 4. 最后,将标红后的帖子信息返回给调用方。 通过以上步骤,你可以实现在使用ElasticsearchRepository进行查询时,将关键词标红的功能。这样可以提高搜索结果的可读性和用户体验。 #### 引用[.reference_title] - *1* [ElasticSearch总结](https://blog.csdn.net/The_Later/article/details/127813294)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v4^insert_chatgpt"}} ] [.reference_item] - *2* [Springboot ElasticSearch6.X 关键词高亮查询](https://blog.csdn.net/qq_35715202/article/details/90518171)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v4^insert_chatgpt"}} ] [.reference_item] - *3* [仿牛客社区项目描述](https://blog.csdn.net/qq_35484331/article/details/125042339)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v4^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值