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>
相关推荐
秋田君4 分钟前
深入理解JavaScript设计模式之闭包与高阶函数
开发语言·javascript·设计模式
山有木兮木有枝_8 分钟前
JavaScript预编译机制深度解析:从V8引擎到执行上下文
前端
袁煦丞11 分钟前
电子书阅读器界的"万能工具"Koodo Reader :cpolar内网穿透实验室第593个成功挑战
前端·后端·远程工作
半醉看夕阳28 分钟前
HarmonyOS开发 ArkTS 之字符串的剖析
javascript·harmonyos·arkts
程序猿小D32 分钟前
第14节 Node.js 全局对象
linux·前端·npm·node.js·编辑器·vim
Mintopia37 分钟前
当代码遇见光影魔术师:用 JavaScript 揭秘环境光遮蔽的奇幻世界
前端·javascript·计算机图形学
不吃糖葫芦340 分钟前
App使用webview套壳引入h5(三)——解决打包为app后在安卓机可物理返回但是在苹果手机无法测滑返回的问题
uni-app·webview
Dignity_呱1 小时前
别在傻傻分不清any void never unknown的场景啦
前端·vue.js·typescript
站在风口的猪11081 小时前
《前端面试题:CSS3新特性》
前端·css·html·css3·html5
crary,记忆1 小时前
Angular报错:cann‘t bind to ngClass since it is‘t a known property of div
前端·javascript·angular·angular.js