uniapp简单实现搜索--历史记录功能

点击搜索时候,将搜索的值存入本地记录并展示,并且有清空历史记录功能。
代码直接贴上:

<template>
	<view>
		<!-- 搜索框 -->
		<view class="search">
			<view style="display: flex;align-items: center;">
				<text class="iconfont icon-sousuo position-absolute text-muted"></text>
				<input class="searchInput" v-model="inputValue" @confirm="search" placeholder="搜索" type="text" />
			</view>
			<view>取消</view>
		</view>
		<!-- 搜索框 -->

		<!-- 搜索历史 -->
		<view class="searchHistory">
			<view style="display: flex;align-items: center;justify-content: space-between;box-sizing: border-box;padding: 0px 5px;">
				<view>搜索历史:</view>

				<view style="color: red;font-size: 28px;" @click="empty">×</view>
			</view>
			<view class="searchHistoryItem">
				<view v-for="(item, index) in searchHistoryList" :key="index">
					<text>{{ item }}</text>
				</view>
			</view>
		</view>
		<!-- 搜索历史 -->
	</view>
</template>

<script>
export default {
	data() {
		return {
			inputValue: '',
			searchHistoryList: [] //搜索出来的内容
		};
	},
	methods: {
		search() {
			if (this.inputValue == '') {
				uni.showModal({
					title: '搜索内容不能为空'
				});
			} else {
				if (!this.searchHistoryList.includes(this.inputValue)) {
					this.searchHistoryList.unshift(this.inputValue);
					uni.setStorage({
						key: 'searchList',
						data: JSON.stringify(this.searchHistoryList)
					});
				} else {
					//有搜索记录,删除之前的旧记录,将新搜索值重新push到数组首位
					let i = this.searchHistoryList.indexOf(this.inputValue);
					this.searchHistoryList.splice(i, 1);
					this.searchHistoryList.unshift(this.inputValue);
					uni.showToast({
						title: '不能重复添加'
					});
					uni.setStorage({
						key: 'searchList',
						data: JSON.stringify(this.searchHistoryList)
					});
				}
			}
		},
		//清空历史记录
		empty() {
			uni.showToast({
				title: '已清空'
			});
			uni.removeStorage({
				key: 'searchList'
			});

			this.searchHistoryList = [];
		}
	},
	async onLoad() {
		let list = await uni.getStorage({
			key: 'searchList'
		});
		// console.log(list,'历史记录----');
		if(list[1]&&list[1].data.length>0){			
	    	this.searchHistoryList = JSON.parse(list[1].data);
		}

		
	}
};
</script>

<style>
.search {
	width: 100%;
	height: 30px;
	display: flex;
	align-items: center;
	justify-content: space-between;
	box-sizing: border-box;
	padding: 0px 15px;
}
.searchInput {
	background-color: #f8f9fa;
	width: 220px;
	margin-left: 5px;
}
.searchHistory {
	width: 100%;
	margin-top: 5px;
}
.searchHistoryItem {
	width: 100%;
	display: flex;
	flex-wrap: wrap;
}
.searchHistoryItem view {
	/* width: 50px; */
	height: 20px;
	border: 1px solid #eee;
	margin: 0px 5px;
}
</style>



实现搜索页面的历史功能,可以使用uniapp提供的本地存储功能来保存用户的搜索历史记录。下面是一个简单的代码实现示例: 1. 在搜索页面的vue文件中,可以使用input组件来实现搜索框,同时使用一个列表展示搜索历史记录。在data中定义一个变量来保存搜索历史数据,比如`searchHistory`。 ```html <template> <view> <input type="text" v-model="keyword" @confirm="search" placeholder="请输入搜索关键字" /> <view v-if="searchHistory.length > 0"> <view v-for="(item, index) in searchHistory" :key="index"> <text>{{ item }}</text> </view> </view> </view> </template> ``` 2. 在methods中实现搜索方法`search`,在该方法中将用户输入的关键字保存到`searchHistory`中,并使用uniapp提供的本地存储功能`uni.setStorageSync`将`searchHistory`保存到本地。 ```javascript <script> export default { data() { return { keyword: '', searchHistory: [] }; }, methods: { search() { if (this.keyword.trim() !== '') { this.searchHistory.push(this.keyword); uni.setStorageSync('searchHistory', this.searchHistory); } } }, created() { // 从本地存储中获取搜索历史记录 const history = uni.getStorageSync('searchHistory'); if (history) { this.searchHistory = history; } } }; </script> ``` 这样,用户每次搜索时,会将搜索关键字添加到`searchHistory`中,并保存到本地存储中。在页面加载时,会从本地存储中获取之前保存的搜索历史记录并展示出来。 需要注意的是,本地存储的数据是以字符串形式保存的,如果需要使用数组或对象形式,需要使用JSON.stringify和JSON.parse进行转换。另外,可以根据实际需求对搜索历史记录进行管理,比如清空历史记录等操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值