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

news2024/9/24 21:18:48

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

<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.coloradmin.cn/o/798270.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

DevOps-Git

DevOps-Git 版本控制软件提供完备的版本管理功能&#xff0c;用于存储&#xff0c;追踪目录&#xff08;文件夹&#xff09;和文件的修改历史。版本控制软件的最高目标是支持公司的配置管理活动&#xff0c;最终多个版本的开发和维护活动&#xff0c;即使发布软件。 git安装 h…

Python获取天气数据 并做可视化解读气象魅力

前言 大家早好、午好、晚好吖 ❤ ~欢迎光临本文章 前几天的长沙&#xff0c;白天大太阳&#xff0c;晚上下暴雨 一点也琢磨不透天气老人家它的想法 顺便哔哔一点生活小插曲&#xff1a; 前几天的时候&#xff0c;我出门&#xff0c;家里的几扇窗户开着在透气 等我十一点回…

了解Unity编辑器之组件篇Layout(八)

Layout&#xff1a;用于管理和控制UI元素的排列和自动调整一、Aspect Ratio Fitter&#xff1a;用于根据宽高比自动调整UI元素的大小 Aspect Mode&#xff1a;用于定义纵横比适配的行为方式。Aspect Mode属性有以下几种选项&#xff1a; &#xff08;1&#xff09;None&#xf…

开源预训练框架 MMPRETRAIN现有的推理模型(三)

推理器类型&#xff1a; &#xff08;1&#xff09;ImageClassificationInferencer&#xff1a;对给定图像进行图像分类。 &#xff08;2&#xff09;ImageRetrievalInferencer&#xff1a;从给定图像集上的给定图像执行图像到图像检索。 &#xff08;3&#xff09;ImageCapti…

FS32K144用官方Bootloader为底层用RAppIDL BL Tool工具下载升级程序

​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​ 一、工具问题 1、可以在NXP的官网上找到这个软件&#xff0c;也可以加载完成NXP的官方库后找到它&#xff08;自动安装的&#xff09;&#xff0c;也可我…

【接口测试】Postman--变量与集合

目录 变量与集合 一、变量 1、环境变量&#xff08;1&#xff09;创建环境变量&#xff08;2&#xff09;管理环境变量&#xff08;3&#xff09;选择与编辑环境变量2、全局变量&#xff08;1&#xff09;管理全局变量二、集合 1、创建集合2、保存请求到集合3、分享集合三、集…

链表是否有环、环长度、环起点

问题引入 如何检测一个链表是否有环&#xff0c;如果有&#xff0c;那么如何确定环的长度及起点。 引自博客&#xff1a;上述问题是一个经典问题&#xff0c;经常会在面试中被问到。我之前在杭州一家网络公司的电话面试中就很不巧的问到&#xff0c;当时是第一次遇到那个问题&…

【数据结构】实验一:绪论

实验一 绪论 一、实验目的与要求 1&#xff09;熟悉C/C语言&#xff08;或其他编程语言&#xff09;的集成开发环境&#xff1b; 2&#xff09;通过本实验加深对算法时间复杂度的理解&#xff1b; 3&#xff09;结合具体的问题分析算法时间复杂度。 二、实验内容 设计程…

C# PaddleDetection 目标检测 ( yolov3_darknet)

效果 项目 VS2022.net4.8OpenCvSharp4Sdcb.PaddleDetection 代码 using OpenCvSharp; using OpenCvSharp.Extensions; using Sdcb.PaddleDetection; using Sdcb.PaddleInference; using System; using System.Drawing; using System.Windows.Forms; using YamlDotNet;namespa…

vue实现循环数据动态添加标签以及单独控制显示隐藏

效果图&#xff1a; html: <table class"tag-table"><tbody><tr v-for"(item, index) in form.tagList" :key"index"><td>333</td><td><divclass"tag-box"v-for"(item1, index1) in it…

推荐用于学习RN原生模块开发的开源库—react-native-ble-manager

如题RN的原生模块/Native Modules的开发是一项很重要的技能&#xff0c;但RN官网的示例又比较简单&#xff0c;然后最近我接触与使用、还有阅读了react-native-ble-manager的部份源码&#xff0c;发现里边完全包含了一个Native Modules所涉及的知识点/技术点&#xff0c;故特推…

【SAM MaskDecoder 图】SAM(segment anything) 中MaskDecoder过程图示

SAM(segment anything) 中MaskDecoder过程图示 本人根据代码画的&#xff0c;如有出入&#xff0c;欢迎留言反馈更正。

算法之归并排序算法

归并&#xff08;Merge&#xff09;排序法是将两个&#xff08;或两个以上&#xff09;有序表合并成一个新的有序表&#xff0c;即把待排序序列 分为若干个子序列&#xff0c;每个子序列是有序的。然后再把有序子序列合并为整体有序序列。 public class MergeSortTest {public …

简单上手FineBI

简介 安装下载 下载的是V6.0.11版本 设置管理员账号 账号admin 密码123456 新建分析主题 添加数据 选择本地数据上传 选择示例数据上传 打开效果如下&#xff0c;点击“确定”&#xff0c;这样就将示例数据上传到分析主题中 分析数据——编辑数据 如果数据质量好&#xf…

用OpenCV图像处理技巧之白平衡算法(二)

1. 引言 在上一节中我们介绍了白平衡算法的原理&#xff0c;并详细实现了基于白色补丁算法的白平衡实现&#xff0c;本文继续就白平衡的其他算法实现进行展开。 闲话少说&#xff0c;我们直接开始吧&#xff01; 2. Gray-world Algorithm 灰色世界算法&#xff08;Gray-wor…

protobuf安装教程

protobuf安装 一&#xff0c;Windows下安装下载protobuf配置环境变量检查是否安装成功 二&#xff0c;Linux下安装下载protobuf安装protobuf检查是否安装成功 一&#xff0c;Windows下安装 下载protobuf 下载地址 本次下载以v21.11为例&#xff0c;根据自己需求下载即可。 配…

VL163的基本信息

VL163是2:4差分通道多路复用/demux开关USB 3.1应用&#xff0c;为交换机信号性能支持高达USB 3.1&#xff0c;并使用QFN-28 3.5x4.5mm绿色封装。 VL163 QFN28 只能处理2Lane数据信号。自己没有CC识别沟通协议&#xff0c;如果要做USB-C Swtich&#xff0c;就要通过别的USB-C协…

Python计算统计分析MSE 、RMSE、MAE、R2

1、平均绝对误差 (MAE)Mean Absolute Error&#xff0c;是绝对误差的平均值&#xff0c;能更好地反映预测值误差的实际情况。范围[0,∞)&#xff0c;当预测值与真实值完全吻合时等于0&#xff0c;即完美模型&#xff1b;误差越大&#xff0c;该值越大。 2、均方误差 MSE(mean…

团簇大小分布计算方法,fix ave/histo命令详解

LAMMPS是一款广泛应用于分子动力学模拟的强大软件。在模拟过程中&#xff0c;我们经常需要对系统的物理性质进行分析和统计。 fix ave/histo命令则是LAMMPS中一个非常有用的命令&#xff0c;它可以帮助我们对系统进行直方图统计分析。 本文将深入介绍fix ave/histo命令的用法和…

CentOS 搭建 GitLab Git

本文目录 1. CentOS7 搭建 Gitlab1. 安装 sshd1. 安装 sshd 依赖2. 启动并设置开机自启3. 安装防火墙4. 开启防火墙5. 开放 ssh 以及 http 服务 2. 安装 postfix1. 安装 postfix2. 启动并设置开机自启3. 几个补充知识 3. 下载并安装 gitlab1. 在线下载安装包2. 安装 4. 修改 gi…