实现历史搜索记录和搜索功能

uniapp实现搜索功能和搜索历史记录
<template>
	<view class="content">
		<!-- 搜索引擎 这里是模糊查询,后续会加上搜索提示,当您输入关键字下面会来一些提示搜索词,每个设备的搜索记录或者每个账号的搜索记录是不一样的-->
		<view class="search" @click="go">
			<uni-easyinput class="uni-mt-5" v-model="inputValue" :styles="styles" :placeholderStyle="placeholderStyle"
				placeholder="请输入要搜索的关键字" @input="input" prefixIcon="search" type="text" maxlength="16" minlength="6"
				@confirm="search"></uni-easyinput>
		</view>
		<!-- 搜索历史 -->
		<view v-if="isHistory==true" class="searchHistory">
			<view style="display: flex;align-items: center;justify-content: space-between;box-sizing: border-box;padding: 0px 5px;">
				<view>搜索历史:</view>
				<view>
					<image style="width: 50rpx;height: 50rpx;" src="/static/删除.png" @click="empty"></image>
				</view>
			</view>

			<view class="searchHistoryItem">
				<view v-for="(item, index) in searchHistoryList" :key="index">
					<text>{{ item }}</text>
				</view>
			</view> <!-- 通过搜索记录下标数组来计算有数据还是无数据     如果有数据就会显示,如果没数据就不会显示则显示您还没历史记录-->
		</view>
		<view style="text-align: center;color:#A88565;font-size: 26rpx;padding-top:20rpx;" v-if="isHistory==false">
			您还没有历史记录</view>

		<!-- 这里显示搜索索引 -->
		<view v-show="hidden">

		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				isHistory: true,
				hidden: false,
				//默认显示历史搜索记录,当点击input框时为false
				data: [], //一个空数组
				inputValue: '',
				searchHistoryList: [], //搜索出来的内容
				// inputValue: '',
				password: '',
				placeholderStyle: "color:#A88565;font-size:24rpx",
				styles: {
					color: '#A88565',
					borderColor: '#dedede',
					fontsize: '28rpx'
				}
			};
		},
		methods: {
			go() {
				console.log('点击了搜索框')
			
				
			},
			input(e) {
				console.log('输入内容:', e);
			},
			search() {
				if (this.inputValue == '') {
					uni.showToast({
						title: '搜索内容不能为空'
					})
				} else {
					if (!this.searchHistoryList.includes(this.inputValue)) {
						this.searchHistoryList.unshift(this.inputValue);
						uni.setStorage({
							key: 'searchList',
							data: JSON.stringify(this.searchHistoryList)
						});
						uni.switchTab({
							url: '/pages/shopping/shopping'
						})
					} else {
						//有搜索记录,删除之前的旧记录,将新搜索值重新push到数组首位
						let i = this.searchHistoryList.indexOf(this.inputValue);
						this.searchHistoryList.splice(i, 1);
						this.searchHistoryList.unshift(this.inputValue);
						// uni.showToast({
						// 	title: '不能重复添加'
						// });
						uni.switchTab({
							url: '/pages/shopping/shopping'
						})
						//这里应该是如果下面有历史记录或者啥的直接跳转到下个页面

						uni.setStorage({
							key: 'searchList',
							data: JSON.stringify(this.searchHistoryList)
						});
					}

				}
			},
			//清空历史记录
			empty() {
				uni.showModal({
					title: '提示',
					content: '是否清理全部搜索历史?该操作不可逆。',
					success: res => {
						if (res.confirm) {
							uni.removeStorage({
								key: 'searchList'
							});
							this.searchHistoryList = [];
							console.log(this.searchHistoryList)
							if (this.searchHistoryList.length > 0) {
								this.isHistory = true

							} else {

								this.isHistory = false

							}
						}
					}
				})


			}
		},
		async onLoad() {
			let list = await uni.getStorage({
				key: 'searchList'
			});
			console.log(list[1].data);
			if (list[1].data) {
				this.searchHistoryList = JSON.parse(list[1].data);
			}
	var a = this.searchHistoryList.length
			console.log(a)
			if (this.searchHistoryList.length > 0) {
				this.isHistory = true

			} else {

				this.isHistory = false

			}

		},
		onShow() {
			var a = this.searchHistoryList.length
			console.log(a)
			if (this.searchHistoryList.length > 0) {
				this.isHistory = true

			} else {

				this.isHistory = false

			}
		},
		onReady() {
		
		},



	}
</script>
<style lang="scss">
	.content {
		width: 750rpx;
		height: 100%;

		// background-color: #47474A;
		/deep/ .is-input-border {
			margin: 25rpx;
			width: 700rpx !important;
			border-radius: 10rpx !important;
			height: 70rpx;
		}

		.searchInput {
			background-color: #f8f9fa;
			width:440rpx;
			margin-left: 10rpx;
		}

		.searchHistory {
			width: 100%;
			margin-top: 10rpx;
		}

		.searchHistoryItem {
			width: 100%;
			display: flex;
			flex-wrap: wrap;
		}

		.searchHistoryItem view {
			text-align: center;
			width:120rpx;
			border-radius: 30rpx;
			height: 40rpx;
			border: 2px solid #f4f4f4;
			margin: 0rpx 10rpx;
			background-color: lightgray;
		}
	}
</style>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Android应用中实现搜索功能并显示历史记录,你可以按照以下步骤进行: 1. 创建一个搜索界面,包括一个搜索框和一个搜索按钮。 2. 使用Shared Preferences来存储搜索历史记录。当用户点击搜索按钮时,将搜索关键字添加到SharedPreferences中。 3. 在搜索界面中添加一个“搜索历史记录”按钮,当用户点击该按钮时,从SharedPreferences中读取搜索历史记录并显示在一个列表中。 4. 当用户点击历史记录列表中的一个记录时,将该记录填充到搜索框中,并执行搜索操作。 以下是一个简单的示例代码的实现: ```java public class SearchActivity extends AppCompatActivity { private EditText mSearchEditText; private Button mSearchButton; private Button mHistoryButton; private SharedPreferences mSharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); mSharedPreferences = getPreferences(MODE_PRIVATE); mSearchEditText = findViewById(R.id.search_edit_text); mSearchButton = findViewById(R.id.search_button); mHistoryButton = findViewById(R.id.history_button); mSearchButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String keyword = mSearchEditText.getText().toString(); addKeywordToHistory(keyword); // 执行搜索操作 } }); mHistoryButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showHistoryList(); } }); } private void addKeywordToHistory(String keyword) { SharedPreferences.Editor editor = mSharedPreferences.edit(); Set<String> history = mSharedPreferences.getStringSet("history", new HashSet<String>()); history.add(keyword); editor.putStringSet("history", history); editor.apply(); } private void showHistoryList() { Set<String> history = mSharedPreferences.getStringSet("history", new HashSet<String>()); final String[] historyArray = history.toArray(new String[history.size()]); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("历史记录"); builder.setItems(historyArray, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { mSearchEditText.setText(historyArray[i]); // 执行搜索操作 } }); builder.show(); } } ``` 在这个示例代码中,我们使用SharedPreferences来存储搜索历史记录。当用户点击搜索按钮时,将搜索关键字添加到SharedPreferences中。当用户点击历史记录按钮时,从SharedPreferences中读取搜索历史记录并显示在一个列表中。当用户点击历史记录列表中的一个记录时,将该记录填充到搜索框中,并执行搜索操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱池鱼的酱酱仔

您的鼓励是我前进的动力哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值