uniapp搜素内容,并且高亮搜索结果匹配的文字

做uinapp项目需要做这种搜索,然后结果需要高亮搜索的文字。如下图这种:
在这里插入图片描述
这边用的uview组件库的搜索, 键盘回车搜索

<u-search placeholder="搜索问题" v-model="keyword" actionText="取消" @search="search"></u-search>
<view style="margin-top: 50rpx;" v-if="seacrhList.length">
	<view class="seacrhList" v-for="(item, index) in seacrhList" :key="index"
				 v-html="brightenKeyword(item.title,editKeyword)">
				{{item.title}}
	</view>
</view>

data() {
			return {
				keyword: '', //搜索的文字
				editKeyword: '', //做匹配的文字(这个一定要,不能用‘keyword’变量,不然会有问题)
				seacrhList: [], //搜索的结果
				list: [{title: '123456'},{title: '56789'}] //这里假如是接口返回的数据
			}
		},
methods: {
    search() {
				if (!this.keyword.trim()) {
					//这里你要给个弹窗或者轻提示'请输入关键词'
					
					return
				}
                //这个地方要请求接口,我们现在直接用模拟的数据list
				//MINE_API.getHelpSearch({
				//	helpTitle: this.keyword
				//}).then(res => {
				//	this.editKeyword = this.keyword
				//	this.seacrhList = res.data.records
				//})

                this.editKeyword = this.keyword 
                this.seacrhList = this.list //这个是接口搜索到的数据赋值
			},
			brightenKeyword(val, editKeyword ) {
				const Reg = new RegExp(editKeyword);
				if (val) {
					const res = val.replace(Reg, `<span style="color: #D7311E;">${editKeyword }</span>`);
					return res;
				}
			},
}

<style>
		.seacrhList {
			margin-top: 30rpx;
			padding-bottom: 20rpx;
			border-bottom: 1rpx solid #ddd;
		}
</style>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值