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>
相关推荐
HumoChen9918 小时前
GZip+Base64压缩字符串在ios上解压报错问题解决(安卓、PC模拟器正常)
android·小程序·uniapp·base64·gzip
Anthony_sun7 天前
UniAppx 跳转Android 系统通讯录
android·uniapp
MaCa .BaKa8 天前
36-校园反诈系统(小程序)
java·spring boot·mysql·小程序·vue·maven·uniapp
假客套8 天前
2025 后端自学UNIAPP【项目实战:旅游项目】2、安装下载引用前端UI框架:uview-plus
uniapp·项目实战
米奇妙妙wuu21 天前
uniapp中使用<cover-view>标签
前端·uniapp·html5
专注前端不学习一天浑身难受1 个月前
基于uniapp 实现画板签字
前端·uniapp
大大蚊子1 个月前
‌UniApp 安卓打包完整步骤(小白向)
uniapp
小程序照片合成1 个月前
uniapp微信小程序开发工具本地获取指定页面二维码
微信小程序·小程序·uniapp·二维码
奔跑吧邓邓子1 个月前
【商城实战(92)】高并发下的商城缓存进阶:从原理到实战
redis·缓存·springboot·uniapp·element plus·商城实战
奔跑吧邓邓子2 个月前
【商城实战(74)】数据采集与整理,夯实电商运营基石
springboot·uniapp·element plus·商城实战·商城数据采集与整理