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>
相关推荐
centaury325 天前
使用FastAdmin框架开发二
uniapp·thinkphp·fastadmin
吃葡萄不吐葡萄皮嘻嘻6 天前
uniapp获取状态栏高度,胶囊按钮的高度,底部安全区域的高度,自定义导航栏
uniapp
雪碧聊技术10 天前
v-for的用法及案例
vue3·uniapp·v-for·购物车案例
G皮T13 天前
【Elasticsearch】检索高亮
大数据·elasticsearch·搜索引擎·全文检索·kibana·检索·高亮
nbsaas-boot15 天前
[特殊字符] 分享裂变新姿势:用 UniApp + Vue3 玩转小程序页面分享跳转!
小程序·uniapp·notepad++
weixin_ab1 个月前
实战记录:minapp框架下跨机型接口调用顺序引发的兼容性问题
uniapp·机型兼容性问题
菌菇汤1 个月前
微信小程序传参过来了,但是数据没有获取到
微信小程序·小程序·uniapp
等风也在等着你1 个月前
uniapp评价组件
uniapp
weixin_ab1 个月前
Uniapp 中 onShow 与 onLoad 的执行时机解析
uniapp
满分观测网友z1 个月前
CSS实现元素撑满剩余空间的5种方法
uniapp