uniapp 选择城市定位 根据城市首字母分类排序

获取城市首字母排序,按字母顺序排序

<template>
	<view class="address-wrap" id="address">
		<!-- 搜索输入框-end -->
		<template v-if="!isSearch">
			<!-- 城市列表-start -->
			<view class="address-scroll">
				<!-- 当前城市 -->
				<view class="address-currentcity" id="start">
					<view class="address-currentcity-title">当前城市</view>
					<view class="choosecity-item-li-item" :class="isSelectAreaId== 'start'? 'is-active': ''">
						<image class="icon-dingwei" src="@/static/image/icon-dingwei.png" mode="widthFix"></image>
						<text style="margin-left: 8.5px;">上海</text>
					</view>
				</view>
				<!-- 选择城市 -->
				<view class="address-choosecity">
					<view class="address-choosecity-title">选择城市</view>
					<view class="address-choosecity-con">
						<template v-for="(item,index) in cityList">
							<view class="address-choosecity-item" :key="index" :id="index">
								<view class="choosecity-item-title">{{index}}</view>
								<view class="flex-fs-left choosecity-item-li">
									<template v-for="value in item">
									  <view class="choosecity-item-li-item line1" 
									  :class="isSelectAreaId==value.id?'is-active': ''" 
									  :key="value.name" @click="chooseCityHandle(value)">{{value.name}}</view>
									</template>
								</view>
							</view>
						</template>
					</view>
				</view>
			</view>
			<!-- 城市列表-end -->
			<!-- 对应字母 -->
			<view class="address-letter">
				<view class="address-letter-item" @click="scrollHandle('start')">
					<image class="letter-image" v-if="isLetterIndex== 'start'" src="@/static/image/tiny-spot.png" mode="widthFix"></image>
					<image class="letter-image" v-else src="@/static/image/tiny-spot1.png" mode="widthFix"></image>
				</view>
				<template v-for="(item,index) in cityList">
				   <view class="address-letter-item" :class="isLetterIndex==index? 'is-active': ''" :key="index" @click="scrollHandle(index)">{{index}}</view>
				</template>
			</view>
		</template>
	</view>
</template>

<script>
	import { pinyin } from 'pinyin-pro';
	import { mapGetters, mapMutations } from "vuex";
	import { getHotArea } from '@/api/commonApi.js'
	export default {
		data() {
			return {
				searchVal:'',
				cityList:{},//根据拼音排序的城市数据
				windowTop:0,
				isSearch:false,//是否显示搜索内容,默认:false
				isLetterIndex: 'start',
				isSelectAreaId: 'start',
			};
		},
		onLoad() {
			this.getHotAreaList()
			this.dealwithCityData();
		},
		onShow() {
			//获取手机系统信息
			const systemInfo=uni.getSystemInfoSync();
			console.log("[systemInfo]",systemInfo)
			// #ifdef H5 || APP-PLUS || MP-ALIPAY
			this.windowTop=systemInfo.windowTop
			// #endif
		},
		computed: {
			...mapGetters(["areaCacheList"]),
			cityData() {
				return this.areaCacheList || []
			}
		},
		methods:{
			// 获取热门城市
			getHotAreaList() {
				getHotArea().then(data=> {
					console.log(data)
					console.log('data-热门城市')
				})
			},
			//处理城市数据
			dealwithCityData(){
				let tempCityList={};//临时城市数据
				const cityData=this.cityData || [];
				let cityTempList = []
				cityData.forEach(element=> {
					if (element.children) {
						element.children.forEach(child=> {
							cityTempList.push({
								id: child._id,
								name: child.name
							})
						})
					}
				})
				// //把数据转换成拼音
				let tempPinYinList={};//临时拼音数据
				cityTempList.forEach(temp=>{
					let py=pinyin(temp.name.substring(0,1), { pattern: 'first', toneType: 'none' }).toUpperCase();
					if(tempPinYinList[py]==undefined){
						tempPinYinList[py]=[];
					}
					tempPinYinList[py].push(temp)
				})
				
				//对数据进行排序
				this.cityList= this.objKeySort(tempPinYinList);
			},
			objKeySort(obj) {
				//排序的函数
			    var newkey = Object.keys(obj).sort();
			  //先用Object内置类的keys方法获取要排序对象的属性名,再利用Array原型上的sort方法对获取的属性名进行排序,newkey是一个数组
			    var newObj = {};//创建一个新的对象,用于存放排好序的键值对
			    for (var i = 0; i < newkey.length; i++) {//遍历newkey数组
			        newObj[newkey[i]] = obj[newkey[i]];//向新创建的对象中按照排好的顺序依次增加键值对
			    }
			    return newObj;//返回排好序的新对象
			},
			
			//点击字母滚动事件
			scrollHandle(index){
				console.log(index)
				console.log('index-----------------------')
				this.isLetterIndex = index
				const query = uni.createSelectorQuery().in(this);
				uni.createSelectorQuery().select("#address").boundingClientRect(data=>{
				  uni.createSelectorQuery().select("#"+index).boundingClientRect((res)=>{
					
				    uni.pageScrollTo({
				      duration:100,
				      scrollTop:res.top - data.top - 12,//滚动到实际距离是元素距离顶部的距离减去最外层盒子的滚动距离
				    })
				  }).exec()
				}).exec();
			},
			//选择城市
			chooseCityHandle(params){
				console.log(params)
				console.log('params00000000000000000')
				this.isSelectAreaId = params.id
			}
			
		}
	}
</script>


<style lang="scss" scoped>
.address-wrap{
	padding: 24rpx;
	display: flex;
	flex-direction: column;
	background: #fff;
	//城市筛选区
	.address-scroll{
		display: flex;
		flex-direction: column;
		.address-currentcity{
			display: flex;
			flex-direction: column;
			.address-currentcity-title{
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #4F4B4E;
				padding-bottom: 20rpx;
			}
			
			.icon-dingwei {
				width: 24rpx;
				height: 24rpx;
			}
		}
		//选择城市
		.address-choosecity{
			display: flex;
			flex-direction: column;
			.address-choosecity-title{
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #999999;
			}
			
		}
		
	}
	.address-choosecity-con{
		display: flex;
		flex-direction: column;
		.address-choosecity-item{
			display: flex;
			flex-direction: column;
			.choosecity-item-title{
				font-size: 15px;
				font-family: PingFang SC;
				font-weight: 400;
				color: #4F4B4E;
				padding: 20rpx;
			}
			.choosecity-item-li{
				flex-wrap: wrap;
			}
			
		}
	}
	.choosecity-item-li-item {
		width: 200rpx;
		height: 70rpx;
		background: #fff;
		border-radius: 4rpx;
		border: 1rpx solid #E5E5E5;
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #858585;
		line-height: 68rpx;
		text-align: center;
		margin-right: 40rpx;
		margin-bottom: 30rpx;
		padding: 0 8rpx;
		&:nth-child(3n) {
			margin-right: 0;
		}
		&.is-active {
			background: rgba(240,133,0,0.1);
			border: 1rpx solid #F08500;
			color: #4F4B4E;
		}
	}
	//字母
	.address-letter{
		position: fixed;
		top: 100rpx;
		right: 2rpx;
		display: flex;
		flex-direction: column;
		z-index: 10;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #4F4B4E;
		align-items: center;
		.address-letter-item{
			margin-bottom: 16rpx;
			&.is-active {
				color: #F08500;
			}
		}
		.letter-image {
			width: 24rpx;
			height: 24rpx;
		}
	}
	.search-content{
		display: flex;
		flex-direction: column;
		margin-top: 12px;
		margin-bottom: 12px;
		.search-con-item{
			border-bottom: 1px solid rgba(254, 254, 254, .2);
			height: 35px;
			line-height: 35px;
			font-size: 14px;
			font-family: PingFang SC;
			font-weight: 400;
			color: #4F4B4E;
		}
		.search-total{
			height: 100px;
			display: flex;
			font-size: 14px;
			font-family: PingFang SC;
			font-weight: 400;
			color: #4F4B4E;
			justify-content: center;
			align-items: center;
		}
	}
}
</style>


在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/49065.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

基于SSM实现个人随笔分享平台:创作心灵,分享自我

项目简介 本文将对项目的功能及部分细节的实现进行介绍。个人随笔分享平台基于 SpringBoot SpringMVC MyBatis 实现。实现了用户的注册与登录、随笔主页、文章查询、个人随笔展示、个人随笔查询、写随笔、草稿箱、随笔修改、随笔删除、访问量及阅读量统计等功能。该项目登录模…

【C语言day08】

int n5; int a[n][n2] 数组定义下角标不能为变量 注&#xff1a;C99标准中支持了使用变量本题考查的是二维数组的元素访问&#xff0c;A选项是 正确的&#xff0c;X[i]就是第i行的数组名&#xff0c;数组名表示首元素的地址&#xff0c;X[i]表示第i行的第一个元素的地址&#…

Qgis二次开发-QgsMapLayer(加载矢量、栅格图层)

1.简介 QgsMapLayer是所有地图层类型的基类&#xff0c;这是所有地图层类型(矢量&#xff0c;栅格)的基类&#xff0c;首先定义一个QgsMapCanvas地图画布&#xff0c;然后画布上添加图层&#xff0c;使用以下方法设置图层集合。 //设置当前图层集合 void setLayers (const QL…

【c语言进阶】字符函数和字符串函数知识总结

字符函数和字符串函数 前期背景求字符串长度函数strlen函数strlen函数三种模拟实现 长度不受限制的字符串函数strcpy函数strcpy函数模拟实现strcat函数strcat函数模拟实现strcmp函数strcmp函数模拟实现 长度受限制的字符串函数strncpy函数strncpy函数模拟实现strncat函数strnca…

【Qt】QML-02:QQuickView用法

1、先看demo QtCreator自动生成的工程是使用QQmlApplicationEngine来加载qml文件&#xff0c;下面的demo将使用QQuickView来加载qml文件 #include <QGuiApplication> #include <QtQuick/QQuickView>int main(int argc, char *argv[]) {QGuiApplication app(argc,…

electron dialog.showMessageBox使用案例

electron 版本&#xff1a;25.3.1 index.html <!DOCTYPE html> <html> <head><meta charset"UTF-8"><title>Hello World!</title><meta http-equiv"Content-Security-Policy" content"script-src self unsa…

MySQL绿色安装和配置

1、 从地址http://dev.mysql.com/downloads/mysql/中选择windows的版本下载。 2、 mysql各个版本的简介 &#xff08;1&#xff09; MySQL Community Server 社区版本&#xff0c;开源免费&#xff0c;但不提供官方技术支持。 &#xff08;2&#xff09; MySQL Enterprise Ed…

失去SSL证书,会对网站安全造成什么影响?

作为网络世界中的“身份证”&#xff0c;SSL证书可以在网络世界中证明你是一个真实可信的企业或个人网站&#xff0c;而不是一个钓鱼网站。且在网站的服务器上部署SSL证书后&#xff0c;可以使网站与访问者之间通过SSL协议建立安全的加密连接&#xff0c;确保在Web服务器和浏览…

【Unity细节】关于拉进镜头场景后场景资源消失的问题的解决

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! 本文由 秩沅 原创 收录于专栏&#xff1a;unity细节和bug ⭐关于拉进镜头场景资源消失的问题的解决⭐ 文章目录 ⭐关于拉进镜头场景资源消失…

No100.精选前端面试题,享受每天的挑战和学习(事件循环)

文章目录 1. 请解释一下JavaScript中的事件循环&#xff08;Event Loop&#xff09;是什么&#xff0c;并描述其工作原理。2. 请解释一下JavaScript中的宏任务&#xff08;macro-task&#xff09;和微任务&#xff08;micro-task&#xff09;的区别3. 在事件循环中&#xff0c;…

移动IP的原理

目的 使得移动主机在各网络之间漫游时&#xff0c;仍然能保持其原来的IP地址不变 工作步骤 代理发现与注册 主机A&#xff1a;主机A移动到外地网络后&#xff0c;通过“代理发现协议”&#xff0c;与外地代理建立联系&#xff0c;并从外地代理获得一个转交地址&#xff0c;…

非线性质量弹簧阻尼器的神经网络仿真研究(Matlab代码Simulink仿真实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

浅谈性能测试中的基准测试

在性能测试中有一种测试类型叫做基准测试。这篇文章&#xff0c;就聊聊关于基准测试的一些事儿。 1、定义 通过设计合理的测试方法&#xff0c;选用合适的测试工具和被测系统&#xff0c;实现对某个特定目标场景的某项性能指标进行定量的和可对比的测试。 2、特质 ①、可重…

FPGA——verilog实现格雷码与二进制的转换

文章目录 一、格雷码简介二、二进制转格雷码三、格雷码转二进制四、仿真 一、格雷码简介 格雷码是一种循环二进制码或者叫作反射二进制码。跨时钟域会产生亚稳态问题&#xff08;CDC问题&#xff09;&#xff1a;从时钟域A过来的信号难以满足时钟域B中触发器的建立时间和保持时…

【ROS第一讲】一、创建工作空间

【ROS第一讲】一、创建工作空间 一、工作空间1.src&#xff1a;2.build&#xff1a;3.devel&#xff1a;4.install: 二、创建工作空间1.工作空间的编译2.配置环境变量&#xff1a; 三、创建功能包 一、工作空间 1.src&#xff1a; 放置所有功能包源码的空间 2.build&#xf…

vue中tab隐藏display:none(v-show无效,v-if有效)

目录 背景 原因&#xff1a;display: table-cell>display:none 解决&#xff1a; 方法A.获取元素设置display&#xff08;适用于 简单场景&#xff09; 方法B.自定义tabs​​​​​​​ &#xff08;适用于 复杂场景&#xff09; 背景 内联样式(style“ ”) /this.$…

redis群集(主从复制)

---------------------- Redis 主从复制 ---------------------------------------- 主从复制&#xff0c;是指将一台Redis服务器的数据&#xff0c;复制到其他的Redis服务器。前者称为主节点(Master)&#xff0c;后者称为从节点(Slave)&#xff1b;数据的复制是单向的&#xf…

Docker 如何助您成为数据科学家

一、说明 在过去的 5 年里&#xff0c;我听到了很多关于 docker 容器的嗡嗡声。似乎我所有的软件工程朋友都在使用它们来开发应用程序。我想弄清楚这项技术如何使我更有效率&#xff0c;但我发现网上的教程要么太详细&#xff1a;阐明我作为数据科学家永远不会使用的功能&#…

5.定时器-间歇函数

网页中经常会需要一种功能&#xff1a;每隔一段时间需要自动执行一段代码&#xff0c;不需要我们手动去触发 例如&#xff1a;网页中的倒计时 ●要实现这种需求&#xff0c;需要定时器函数 5.1开启定时器 语法 setInterval(函数,间隔时间)作用&#xff1a;每隔一段时间调用这…

excel要如何自动累加某个单元格上方的所有单元格?

输入公式 SUM(INDIRECT("A1:A"&ROW()-1)) 运行实例如下图 注意图中b4&#xff0c;和b5单元格都输入相同的公式。 此方法可以避免写vba&#xff0c;以前此类问题的解决都是通过vba代码进行处理 对函数进行解析 主要使用了 INDIRECT() 2、公式说明&#xff1a;…
最新文章