uniapp搜索功能实现

2 阅读1分钟

首先搜索分为,搜索高亮显示,搜索结果,搜索历史记录,热点数据(推广数据),我们着重对前三种进行描述

<template>
	<view class="content" style="padding-left: 12rpx;padding-right: 12rpx;">
		
		<view class="SearchInput">
			<view class="SearchInput_Left">
				<input type="text"
				placeholder="请输入文字" 
				v-model="InputText"
				@focus="SearchFocusStyle" @blur="SearchBlurStyle" @click="SearchSelect" />
			</view>
			
			<view class="SearchInput_Right">
				<!-- 拍照,扫一扫等等 -->
				<view class="SearchInput_Scan" v-if="HightList">
					<image src="/static/scanImage.png" mode="aspectFit"></image>
				</view>
				<!-- 推出高亮列表 -->
				<view class="SearchInput_Scan" v-else="HightList">
					<image src="/static/exitImage.png" mode="aspectFit"></image>
				</view>
				<view class="SearchInput_Btn">
					<text>搜索</text>
				</view>
			</view>
			
		</view>
		<!-- 最近搜索,推广数据 -->
		<view class="" v-if="HightList">
			<view class="" v-for="(iteam,index) in GetHistoryData" :key="index">
				<text>{{iteam}}</text>
			</view>
		</view>
		<!-- 高亮显示列表 -->
		<view class="" v-else="HightList">
			<view class="" v-for="(iteam,index) in HightTextList" :key="index" @click="navigator">
				<text v-html="iteam" ></text>
				
<!-- 	小程序	<rich-text :nodes="iteam"></rich-text> -->
			</view>
		</view>
	</view>
</template>

<script setup>
	import { onMounted, ref,watch } from 'vue'
	
	const HightList=ref(true)
	const HightTextList=ref()
	const InputText=ref()
	
	//将数据转为数组
	const SetHistoryData=ref([])
	
	//从本地存储拿出来
	const GetHistoryData=ref()
	
	onMounted(()=>{
		GetHistoryData.value=uni.getStorageSync("SearchHistory")
	})
	
	const SearchFocusStyle=()=>{
		HightList.value=false
	}
	
	const SearchBlurStyle=()=>{
		HightList.value=true
	}
	
	const SearchSelect=(e)=>{
		InputText.value=e.detail.value
		
	}
	
	// 跳转页面
	const navigator=()=>{
		uni.navigateTo({
			url:"目标页面",
			success: () => {
				uni.setStorageSync("SearchHistory",HistoryData)
			}
		})
	}
	
	watch(InputText,(newValue,oldValue)=>{
		// 用newvalue请求后台
		const res=SearchList(newValue)
		// 高亮列表
		HightTextList.value=res.data.map((iteam)=>{
			HistoryData.value.push(newValue)
			return iteam.content.split(newValue).join(`<span style='color:red>${newValue}</span>`)
		})
		
	})
	
	
	
</script>

<style lang="scss" scoped>
	.SearchInput{
		height: 75rpx;
		border: 1px solid #ccc;
		border-radius: 12rpx;
		
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-left: 24rpx;
		padding-right: 12rpx;
		.SearchInput_Right{
			display: flex;
			align-items: center;
			width: 200rpx;
		}
		.SearchInput_Left{
			width: 90%;
			.SearchInput_Scan image{
				width: 35rpx;
				height: 35rpx;
			}
			.SearchInput_Btn{
				width: 110rpx;
				text-align: center;
				line-height: 35rpx;
			}
		}
	}
	
</style>

搜索高亮显示

map和forEach区别是forEach不具备返回值,map具备返回值

	HightTextList.value=res.data.map((iteam)=>{
			HistoryData.value.push(newValue)
			return iteam.content.split(newValue).join(`<span style='color:red>${newValue}</span>`)
		})

历史记录

历史记录一定是搜索完成之后在进行存储,也就是完成一次前后端联调。然后每次完成联调的搜索消息都要push到数组里面,然后存取不一样。取就是渲染,通过v-for循环到页面

//将数据转为数组
	const SetHistoryData=ref([])

搜索结果

主要重新建立页面,完成一次前后端联调,保证页面生命周期,本身请求数据不难。