uniapp多组数组 搜索高亮效果demo(整理)

bash 复制代码
<template>
	<view class="mT100">
		<input type="text" v-model="keyword" @input="filterList" placeholder="请输入关键词">
		<ul>
			<li v-for="item in filteredList" :key="item.id">
				<span v-html="highlightKeyword(item.name)"></span>
			</li>
		</ul>

		<ul>
			<li v-for="item in filteredList1" :key="item.id">
				<span v-html="highlightKeyword(item.name)"></span>
			</li>
		</ul>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: '',
				list: [{
						name: '测试1',
						id: 1
					},
					{
						name: '测试2',
						id: 2
					},
					{
						name: '测试3',
						id: 3
					},
				],
				list1: [{
						name: '测试10',
						id: 1
					},
					{
						name: '测试20',
						id: 2
					},
					{
						name: '测试30',
						id: 3
					},
				],
			};
		},
		computed: {
			filteredList() {
				if (this.keyword === '') {
					return this.list;
				}
				return this.list.filter(item => item.name.includes(this.keyword));
			},
			filteredList1() {
				if (this.keyword === '') {
					return this.list1;
				}
				return this.list1.filter(item => item.name.includes(this.keyword));
			},
		},
		methods: {
			highlightKeyword(text) {
				if (!this.keyword) {
					return text;
				}
				const regex = new RegExp(`(${this.keyword})`, 'gi');
				return text.replace(regex, '<span style="color: red;">$1</span>');
			},
			filterList() {
				// 当关键词变化时,重新过滤列表
				// 可以根据需求调整触发的时机,如在用户输入结束后再触发过滤
				// 这里使用input事件实时响应用户输入
			},
		},
	};
</script>
相关推荐
蜡台4 天前
H5入住浙里办App详细步骤
vue·uniapp·h5·浙政钉
77美式6 天前
手机端键盘弹出导致页面抖动
前端·javascript·uniapp
咸虾米_8 天前
uni-id-pages解决自动管理token续期,以及token过期客户端自动跳转登录页面的功能
uniapp·unicloud·uni-id
阿凤2116 天前
后端返回文件二进制流
开发语言·前端·javascript·uniapp
阿凤2117 天前
后端返回数据流的格式
开发语言·前端·javascript·uniapp
绝世唐门三哥19 天前
uniapp系列-uniappp都有哪些生命周期?
vue.js·小程序·uniapp
宠友信息22 天前
社交软件源码哪个渠道好
java·微服务·架构·社交电子·springboot·uniapp
Fate_I_C23 天前
uniappx 鸿蒙运行包制作失败
华为·uni-app·uniapp·harmonyos
咸虾米_1 个月前
使用uniCloud阿里云服务空间的天塌了,云函数计费规则调整了
阿里云·云计算·uniapp·unicloud
蜡台1 个月前
SSE WebSocket Socket.IO 三者使用及区别
websocket·网络协议·uniapp·sse·socket.io·eventsource