首页 > 其他分享 >直播平台搭建,uni-app 实现搜索关键词高亮效果

直播平台搭建,uni-app 实现搜索关键词高亮效果

时间:2023-01-12 14:24:03浏览次数:43  
标签:city 高亮 keyword app 关键词 filterList item 搜索 uni

直播平台搭建,uni-app 实现搜索关键词高亮效果

1. 封装搜索关键词方法

 

 // 搜索关键词
export function searchKeyword (keyword, city) {
  return new Promise((resove, reject) => {
    const qqmapsdk = new QQMapWX({
        key: 'VNBBZ-SKMRW-U5TR5-OIYQZ-VEOMF-IABLP',  //之前在腾讯位置服务申请的key
  })
    qqmapsdk.getSuggestion({
      keyword: keyword,  //搜索关键词
      region: city,  //城市名
      region_fix: 1, //固定在当前城市
      success: (res) => {
        resove(res)
      },
      fail: (e) => {
        reject(e)
      },
    })
  })
}

 

2. 实现代码

 

<template>
   <view>
   <!-- 地址搜索 -->
    <view>
      <view @click="getCityList">
        <text class="city u-line-1">{{ city }}</text>
        <u-icon name="arrow-down" color="#474A54" size="10"></u-icon>
      </view>
      <view>
        <u-search
          placeholder="请输入地址"
          v-model="keyword"
          shape="square"
          :clearabled="true"
          height="65rpx"
          bgColor="#F5F6FA"
          borderColor="#ECECEC"
          :animation="true"
          :showAction="false"
          @change="getsuggestList"
        ></u-search>
      </view>
    </view>
    <!-- 搜索地址结果 -->
       <view v-if="filterList.length > 0">
        <scroll-view
          scroll-y
          scroll-with-animation
          :scroll-top="scrollTop"
          :style="{ height: `calc(100vh - 53px)` }"
        >
          <block v-for="(item, index) in filterList" :key="index">
            <view @click="selectFilterLocation(item)">
              <rich-text :nodes="item.titleStyle"></rich-text>
              <view>{{ item.address }}</view>
            </view>
          </block>
        </scroll-view>
      </view>
       <!-- 无搜索结果 -->
      <view v-if="filterList.length === 0 && showEmpty">
        <u-empty
          mode="search"
          text="没发现这个地址,换个关键词试试吧~"
          marginTop="100"
        />
      </view>
  </view>
</template>
<script>
import { searchKeyword } from '@/utils/tool.js'
export default {
  data() {
    return {
      city:'青岛' 
      keyword: '', //搜索关键词
      filterList: [], //搜索结果
      showEmpty: false, // 搜索空状态
    }
  },
  methods:{
  // 点击搜索的任意一条
   selectFilterLocation(e) {
      //做一些存储操作或者根据项目需求自己改
      uni.setStorageSync("xxx", e)
      this.filterList = []
      this.keyword = '' 
   },
   // input输入框发生改变
    getsuggestList(keyword) {
      if (keyword === '') {
        this.filterList = []
        this.showEmpty = false
        return
      }
      //过滤符合条件的值
      searchKeyword(keyword, this.relocation.city)
        .then((res) => {
          console.log(res, '筛选数组');
          let sug = []
          res.data.forEach((item) => {
            sug.push({
              id: item.id,
              location: item.title,
              titleStyle: this.join(item.title, keyword),
              address: item.address,
              city: item.city,
              lat: item.location.lat,
              lng: item.location.lng,
            })
          })
          this.filterList = sug
          if (this.filterList.length === 0) {
            this.showEmpty = true
          } else {
            this.showEmpty = false
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 拼接  关键词高亮
    join(str, key) {
      var reg = new RegExp(`(${key})`, 'gm')
      var replace = '<span style="color:#F3671A;font-weight:bold;">$1</span>'
      return str.replace(reg, replace)
    },
</script>
<style scoped>
 .searchCon {
    display: flex;
    align-items: center;
    padding: 0 22upx;
    box-sizing: border-box;
    background: #fff;
    border-bottom: 1upx solid #eee;
    .cityCon {
      margin-right: 36upx;
      display: flex;
      align-items: baseline;
      .city {
        max-width: 155upx;
        font-size: 27upx;
        margin-right: 8upx;
      }
    }
    .searchInput {
      flex: 1;
      margin: 18upx auto;
    }
  }
 .filterList {
    position: absolute;
    top: 106upx;
    bottom: 0;
    left: 0;
    right: 0;
    background: #fff;
    padding: 0 22upx;
    .item {
      padding: 22upx 0;
      border-bottom: 1upx solid #eee;
      .filterTitle {
        font-size: 30upx;
        color: #333;
      }
      .address {
        font-size: 26upx;
        color: #999;
      }
    }
  }
</style>

 

以上就是直播平台搭建,uni-app 实现搜索关键词高亮效果, 更多内容欢迎关注之后的文章

 

标签:city,高亮,keyword,app,关键词,filterList,item,搜索,uni
From: https://www.cnblogs.com/yunbaomengnan/p/17046545.html

相关文章

  • 视频直播APP源码,通过css控制div内容展开更多/收起效果
    视频直播APP源码,通过css控制div内容展开更多/收起效果一.实现思路1.需要设置一个变量控制展开/收起效果2.提前写好最高高度的class样式,超出这个高度多余内容会隐藏......
  • uni-app Pages.json配置
    https://uniapp.dcloud.net.cn/collocation/pages.htmlpages.json 文件用来对uni-app进行全局配置,决定页面文件的路径、窗口样式、原生的导航栏、底部的原生tabbar等......
  • Oracle使用append对表insert会阻塞表的其他会话DML操作
     Oracle使用append对表insert会阻塞其他会话DML操作 快春节了,抽点时间把NNNNNNN久之前的东西整理记录。insert/*+append*/into会对表持有LOCKED_MODE=6的TM锁,导......
  • Power Apps设置Gallery的参数过滤
      在下拉列表的OnSelect属性中设置:ClearCollect(statusCol,{Name:Blank()});Collect(statusCol,Choices([@'ScrapPartsApplicationOrder'].'OrderStatus状态'));......
  • uni-app 背景图片
    背景图片uni-app 支持使用在css里设置背景图片,使用方式与普通 web 项目大体相同,但需要注意以下几点:支持base64格式图片。支持网络路径图片。小程序不支持在cs......
  • uni-app Flex布局
    样式导入使用@import语句可以导入外联样式表,@import后跟需要导入的外联样式表的相对路径,用;表示语句结束。示例代码:<style>@import"../../common/uni.css";......
  • uni-app尺寸单位
     px屏幕像素rpx响应式px屏幕基准:750px公式换算:750*元素在设计稿中的宽度/设计稿基准宽度例如:设计稿宽度375px,元素在设计稿上的宽度为200px,那么元素在uniapp中的宽度应......
  • 把 Eagle App 打造成本地后台管理系统,快速构建 WEB 图片网站。
    自己搭建了图片网站,发现不仅需要前端,还需要后台管理系统,就很烦。偶然之间发现了eagleapp这个软件(专门给设计师管理素材的),经过一番测试,发现了这个好玩的方案。如果你也......
  • uni-app生命周期
    应用生命周期onLaunch:当整个项目启动,渲染完成时触发(全局只触发一次)onShow:小程序显示出来(从后台进入前台显示,可以触发多次)onHide:小程序隐藏起来时触发(从前台进入后台,可......
  • uni-app打包发现
    1、云端发行-原生App-云打包    2、离线运行-原生App本地打包-生成本地打包资源,如果提示安装依赖包,安装即可注意:项目的AppID不能为空,请在该项目下的manifest.......