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>
相关推荐
郑州光合科技余经理6 天前
基于PHP:海外版同城O2O系统多语言源码解决方案
java·开发语言·git·spring cloud·uni-app·php·uniapp
脾气有点小暴8 天前
uniapp通用递进式步骤组件
前端·javascript·vue.js·uni-app·uniapp
脾气有点小暴8 天前
详解 HTML Image 的 mode 属性:图像显示模式的灵活控制
前端·html·uniapp
脾气有点小暴8 天前
uniapp通用单张图片上传组件
前端·javascript·vue.js·uni-app·uniapp
Light608 天前
用一个 Vue 中间件统一 UniApp 与 Taro:契约驱动的双栈方案
vue.js·uni-app·uniapp·taro·vue中间件·跨端适配·契约驱动
万岳软件开发小城8 天前
教育APP/小程序开发标准版图:课程、题库、直播、学习一站式梳理
大数据·php·uniapp·在线教育系统源码·教育app开发·教育软件开发
济南壹软网络科技有限公司8 天前
挑战全球高并发:壹软科技2025新版盲盒源码的“国际合规与弹性架构”解析
数据库·php·uniapp·盲盒源码
郑州光合科技余经理9 天前
PHP技术栈:上门系统海外版开发与源码解析
java·开发语言·javascript·git·uni-app·php·uniapp
微:xsooop11 天前
iOS 上架4.3a 审核4.3a 被拒4.3a 【灾难来袭】
flutter·unity·ios·uniapp
星光一影11 天前
同城派送系统源码,支持小程序,h5,app
mysql·小程序·php·uniapp