uniApp搜索页面

本文详细介绍了如何在uniApp中构建一个搜索页面,涵盖了前端技术栈,包括JavaScript和uniApp框架的应用,通过实例展示了搜索功能的实现步骤和关键代码,帮助开发者了解并掌握uniApp的搜索组件和数据处理技巧。
摘要由CSDN通过智能技术生成

 

 

 

<template>
	<view class="introScroll">
		<u-navbar title="" :is-back="false" :border-bottom="false" :title-bold="true">
			<!-- 城市搜索输入框 -->
			<view class="serchView">
				<image @tap="goBack" style="width: 48rpx; height: 48rpx; margin-left: 30rpx" src="@/static/images/busTicket/go_back.png" mode=""></image>
				<view class="serch">
					<image class="serchIconImage" src="/static/app-plus/image/center/centertool/shopRecruit/img_search_logo.png"></image>
					<input @input="cityInputListener" v-model="value" adjust-position="false" class="serchInput" placeholder="输入城市名称进行搜索" type="text" />
				</view>
			</view>
		</u-navbar>
		<view class="topView">
			<!-- 定位城市-标题 -->
			<!-- <view class="nowCityTextTipsView">
				<text class="nowLocationCity">当前定位城市</text>
			</view> -->
			<!-- 定位城市-内容 -->
			<!-- <view class="againLocationView">
				<text class="loactionText">{
  { nowCity }}</text>
				<text class="loactionStatusText">{
  { locationStatus }}</text>
				<image :animation="animation" @click="locationImageClick" class="refreshImage" src="/static/app-plus/image/index/refreshIcon.png"></image>
			</view> -->
		</view>
		<!-- 右侧字母 -->
		<view class="city_right">
			<text class="letter_item" v-for="(item, index) in letter" :key="index" @tap.stop="letterTap(index)">{
  { item }}</text>
		</view>
		<scroll-view scroll-with-animation="true" class="cityListView" scroll-y="true" :scroll-into-view="scrollId">
			<view style="display: flex; justify-content: space-between; align-items: center">
				<!-- 历史城市 -->
				<text class="hotCityTips">历史</text>
				<button @click="empty" style="height: 50rpx; display: flex; margin-right: 20rpx; align-items: center">
					<!-- <van-icon class="icon" name="delete-o" /> -->
					<text style="font-size: 24rpx">清除记录</text>
				</button>
			</view>

			<view class="new_city_box">
				<text class="new_city_text" v-for="(item, index) in historyCityList" :key="index" @tap.stop="cityHistory(index)">
					{
  { item }}
				</text>
			</view>
			<image
				class="historyMore"
				v-if="historyArrowShow.length > 4"
				@tap="getMoreHistory"
				:class="{ arrowTransform: !flag, arrowTransformReturn: flag }"
				src="@/static/images/busTicket/bottom_icon.png"
			></image>
			<view v-if="historyCityList.length == 0" style="margin-left: 100rpx; margin-bottom: 30rpx">
				<text style="color: #999999">暂无历史搜索记录</text>
			</view>
			<!-- <p v-else style="text-align: center; margin: 0 auto; font-size: 16px; margin-top: 20px">暂无搜索记录</p> -->
			<!-- 热门城市 -->
			<text class="hotCityTips" :id="'re'">热门</text>
			<view class="new_city_box">
				<text class="new_city_text" v-for="(item, index) in hotCityList" :key="index" @tap.stop="cityTap(index)">{
  { item.city }}</text>
			</view>
			<!-- 城市列表 -->
			<view class="forCityView">
				<view class="forCityItem" v-for="(item, index) in allCityList" :key="index">
					<view v-if="item.show" :id="item.letter" class="letterItemFirst">{
  { item.letter }}</view>
					<text @click="cityItemClick(index)" class="itemCityContent">{
  { item.city }}</text>
					<view v-if="!item.last" class="line"></view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			nowCity: '',
			value: '',
			locationStatus: '定位成功',
			letter: ['热', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
			//热门城市
			hotCityList: [],
			//全部城市
			allCityList: [],
			//滑动到指定位置
			scrollId: '',
			//动画
			animation: {},
			//接口请求的数据
			cityList: [],
			receiveId: null,
			historyCityList: [],
			flag: false,
			historyArrowShow: []
		};
	},

	onLoad(options) {
		//设置title标题
		this.nowCity = '北京';
		this.hotCityList = this.returnHotCity();
		uni.setNavigationBarTitle({
			title: '当前城市-' + this.nowCity
		});
		//获取全国城市
		this.getCityList();
		this.receiveId = options.id;
	},
	created() {},
	mounted() {
		// 历史列表
		if (JSON.parse(localStorage.getItem('historyCityList'))) {
			this.historyArrowShow = JSON.parse(localStorage.getItem('historyCityList'));
			this.historyCityList = JSON.parse(localStorage.getItem('historyCityList')).splice(0, 4);
		}
	},
	methods: {
		//拼音点击事件
		letterTap(index) {
			console.log(index);
			if (this.letter[index] == '热') {
				this.scrollId = 're';
			} else {
				this.scrollId = this.letter[index];
			}
		},
		// 清空历史搜索记录
		empty() {
			// this.$notify({ type: 'success', message: '清空历史搜索成功' })
			localStorage.removeItem('historyCityList');
			this.historyCityList = [];
			this.historyArrowShow = [];
		},
		//搜索
		cityInputListener(e) {
			let value = e.detail.value;
			// console.log('22', value);
			let cityAll = this.allCityList;
			let newArray = [];
			for (let i = 0; i < cityAll.length; i++) {
				if (cityAll[i].city == value || cityAll[i].letter == value.toUpperCase()) {
					newArray.push(cityAll[i]);
				}
			}
			if (value == '') {
				this.formatCityData(this.cityList);
			} else {
				this.formatCityData(newArray);
			}
			// if (value == '') {
			// 	// this.$toast("请输入搜索内容");
			// 	// this.$notify({ type: 'primary', message: '请输入搜索内容' });
			// 	return false;
			// } else {
			// 	if (!this.historyCityList.includes(value)) {
			// 		// this.historyCityList.unshift({ city: value });
			// 		this.historyCityList.unshift(value);
			// 		localStorage.setItem('historyCityList', JSON.stringify(this.historyCityList));
			// 	} else {
			// 		const i = this.historyCityList.indexOf(value);
			// 		this.historyCityList.splice(i, 1);
			// 		this.historyCityList.unshift({ city: value });
			// 		localStorage.setItem('historyCityList', JSON.stringify(this.historyCityList));
			// 	}
			/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值