微信小程序 实现三级联动-省市区
github项目地址 https://github.com/z1511676208/chooseAddr
序:项目中需要用到三级联动,自己试着写了下,也查了一些资料,现在把这个记录一下,里面地区数据,可根据个人需要做一些更改,我比较懒就不改了。
wxml
<!--index.wxml-->
<view class="infoText">{{province}} {{city}} {{county}}</view>
<view class="aaaa" >
<button class="animation-button" bindtap="translate">选择城市</button>
</view>
<view class="animation-element-wrapper" animation="{{animation}}" style="visibility:{{show ? 'visible':'hidden'}}" bindtap="hiddenFloatView" data-id="444">
<view class="animation-element" catchtap="nono">
<text class="left-bt" catchtap="hiddenFloatView" data-id="555">取消</text>
<text class="right-bt" catchtap="hiddenFloatView" data-id="666">确定</text>
<view class="line"></view>
<picker-view indicator-style = "height: 50rpx;" value="{{value}}" bindchange="bindChange" catchtap="nono">
<!--省-->
<picker-view-column>
<view wx:for="{{provinces}}" wx:for-item="sheng" wx:key="">
{{sheng.name}}
</view>
</picker-view-column>
<!--地级市-->
<picker-view-column>
<view wx:for="{{citys}}" wx:key="">
{{item.name}}
</view>
</picker-view-column>
<!--区县-->
<picker-view-column>
<view wx:for="{{countys}}" wx:key="">
{{item.name}}
</view>
</picker-view-column>
</picker-view>
</view>
</view>
js
//index.js
//获取应用实例
// var app = getApp()
// var util = require('../../utils/util.js')
var area = require('../../utils/area.js') var areaInfo = [];//所有省市区县数据 var provinces = [];//省 var citys = [];//城市 var countys = [];//区县 var index = [0, 0, 0]; var cellId; var t = 0;
var show = false;
var moveY = 200; Page({
data: {
show: show,
provinces: provinces,
citys: citys,
countys: countys,
value: [0, 0, 0]
},
//滑动事件
bindChange: function (e) {
var val = e.detail.value
// console.log(e)
//判断滑动的是第几个column
//若省份column做了滑动则定位到地级市和区县第一位
if (index[0] != val[0]) {
val[1] = 0;
val[2] = 0;
getCityArr(val[0], this);//获取地级市数据
getCountyInfo(val[0], val[1], this);//获取区县数据
} else { //若省份column未做滑动,地级市做了滑动则定位区县第一位
if (index[1] != val[1]) {
val[2] = 0;
getCountyInfo(val[0], val[1], this);//获取区县数据
}
}
index = val; console.log(index + " => " + val); //更新数据
this.setData({
value: [val[0], val[1], val[2]],
province: provinces[val[0]].name,
city: citys[val[1]].name,
county: countys[val[2]].name
}) },
onLoad: function (options) {
cellId = options.cellId;
var that = this;
var date = new Date()
console.log(date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日"); //获取省市区县数据
area.getAreaInfo(function (arr) {
areaInfo = arr;
//获取省份数据
getProvinceData(that);
}); },
// ------------------- 分割线 --------------------
onReady: function () {
this.animation = wx.createAnimation({
transformOrigin: "50% 50%",
duration: 0,
timingFunction: "ease",
delay: 0
}
)
this.animation.translateY(200 + 'vh').step();
this.setData({
animation: this.animation.export(),
show: show
})
},
//移动按钮点击事件
translate: function (e) {
if (t == 0) {
moveY = 0;
show = false;
t = 1;
} else {
moveY = 200;
show = true;
t = 0;
}
// this.animation.translate(arr[0], arr[1]).step();
animationEvents(this,moveY, show); },
//隐藏弹窗浮层
hiddenFloatView(e){
console.log(e);
moveY = 200;
show = true;
t = 0;
animationEvents(this,moveY, show); },
//页面滑至底部事件
onReachBottom: function () {
// Do something when page reach bottom.
},
tiaozhuan(){
wx.navigateTo({
url: '../../pages/modelTest/modelTest',
})
}
}) //动画事件
function animationEvents(that,moveY,show){
console.log("moveY:" + moveY + "\nshow:" + show);
that.animation = wx.createAnimation({
transformOrigin: "50% 50%",
duration: 400,
timingFunction: "ease",
delay: 0
}
)
that.animation.translateY(moveY + 'vh').step() that.setData({
animation: that.animation.export(),
show: show
}) } // ---------------- 分割线 ---------------- //获取省份数据
function getProvinceData(that) {
var s;
provinces = [];
var num = 0;
for (var i = 0; i < areaInfo.length; i++) {
s = areaInfo[i];
if (s.di == "00" && s.xian == "00") {
provinces[num] = s;
num++;
}
}
that.setData({
provinces: provinces
}) //初始化调一次可更改
getCityArr(0, that);
getCountyInfo(0, 0, that);
that.setData({
province: "北京市",
city: "市辖区",
county: "东城区",
}) } // 获取地级市数据
function getCityArr(count, that) {
var c;
citys = [];
var num = 0;
for (var i = 0; i < areaInfo.length; i++) {
c = areaInfo[i];
if (c.xian == "00" && c.sheng == provinces[count].sheng && c.di != "00") {
citys[num] = c;
num++;
}
}
if (citys.length == 0) {
citys[0] = { name: '' };
} that.setData({
city: "",
citys: citys,
value: [count, 0, 0]
})
} // 获取区县数据
function getCountyInfo(column0, column1, that) {
var c;
countys = [];
var num = 0;
for (var i = 0; i < areaInfo.length; i++) {
c = areaInfo[i];
if (c.xian != "00" && c.sheng == provinces[column0].sheng && c.di == citys[column1].di) {
countys[num] = c;
num++;
}
}
if(countys.length == 0){
countys[0] = {name:''};
}
that.setData({
county: "",
countys: countys,
value: [column0, column1, 0]
})
}
wxss
/**index.wxss**/
page{
background-color: rgba(255, 255, 255, 1);
} .infoText{
margin-top: 20rpx;
text-align: center;
width: 100%;
justify-content: center;
} picker-view{
background-color: white;
padding: 0;
width: 100%;
height: 380rpx;
bottom: 0;
position: fixed;
} picker-view-column view{
vertical-align:middle;
font-size: 28rpx;
line-height: 28rpx;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
} /* ----------------------------------------- */ .animation-element-wrapper {
display: flex;
position: fixed;
left: 0;
top:0;
height: 100%;
width: 100%;
background-color: rgba(0, 0, 0, 0.6);
}
.animation-element {
display: flex;
position: fixed;
width: 100%;
height: 470rpx;
bottom: 0;
background-color: rgba(255, 255, 255, 1);
} .animation-button {
margin-top: 20rpx;
top:20rpx;
width: 400rpx;
height: 100rpx;
line-height: 100rpx;
align-items:center;
} text{
color: #999999;
display: inline-flex;
position: fixed;
margin-top: 20rpx;
height: 50rpx;
text-align: center;
line-height: 50rpx;
font-size: 34rpx;
font-family: Arial, Helvetica, sans-serif;
} .left-bt{
left: 30rpx;
}
.right-bt {
right: 30rpx;
} .line{
display: block;
position: fixed;
height: 1rpx;
width: 100%;
margin-top: 89rpx;
background-color: #eeeeee;
}
这个是没有进行封装的处理
项目里已经封装好,可直接通过模板调用使用。
微信小程序 实现三级联动-省市区的更多相关文章
- 微信小程序---自定义三级联动
在开发的很多电商类型的项目中,免不了会遇到三级联动选择地址信息,如果单纯的使用文本框给用户选择,用户体检可能就会差很多.今天我给大家整理了关于小程序开发利用picker-view组件和animatio ...
- uni-app 微信小程序 picker 三级联动
之前做过一个picker的三级联动功能,这里分享代码给大家 具体代码: // An highlighted block <template> <view> <picker ...
- NO--14 微信小程序,左右联动二
上一篇讲解了左=>右联动,那个还比较简单,本篇写剩下比较核心的部分,也是本次开发过程中遇到最难的部分,右=>左联动,先简单看一下演示 右左联动.gif 一.关键技术: (1) 小程序 ...
- NO--13微信小程序,左右联动
写在前面: 从2016年张小龙发布微信小程序这种新的形态,到2017年小程序的不温不火,再到今年小程序的大爆发,从一度刷爆朋友圈的‘头脑王者’,再到春节聚会坐在一起的火爆小游戏“跳一跳",都 ...
- winform窗体 小程序【三级联动】
三级联动[省,市,区] 类似地区选择,当选的某个省份,后面的下拉框相对变成对应省份的区县 实现省市区联动关键是数据库的表,[每个省内区的AreaCode列是同样的] public Form2() { ...
- 微信小程序之地址联动
这就是我们要实现的效果 <view class="consignee"> <!-- consignee 收件人 --> <text>收件人: & ...
- 微信小程序省市区联动,自定义地区字典
最近在做一个项目的时候遇到了这么一个问题,就是省市区的联动呢,我们需要自定义字典来设置,那么微信小程序自带的省市区选择就不能用了,经过三根烟的催化,终于写出来了.下面献上代码示例. 首先是在utils ...
- 微信小程序导航:官方工具+精品教程+DEMO集合(1月7更新)
1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=14764346784612:简易教程:https://mp.weixin.qq.com/debug ...
- 微信小程序踩坑集合
1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教程:https://mp.weixin.qq.com/debu ...
随机推荐
- 基于 HTML5 的工业组态高炉炼铁 3D 大屏可视化
前言 在大数据盛行的现在,大屏数据可视化也已经成为了一个热门的话题.大屏可视化可以运用在众多领域中,比如工业互联网.医疗.交通.工业控制等等.将各项重要指标数据以图表.各种图形等形式表现在一个页面上, ...
- 201671010142 <java程序设计>初次学习心得与感悟
从开始对JDK的配置就遇到了问题,从这点就可以知道自己知识的薄弱.又知道了在控制台下一些常用命令的掌握.对知识的理解挺艰难,比如遇到一个新的问题就不知道该从哪里入手,有时候还不知道到底问题是啥.接受能 ...
- 5.使用std的迭代器访问并修改图像
void Test_ColorReduceByIterator() { Mat g_srcImage=imread("D:\\OpenCV Projects\\OpenCV_Test_Ima ...
- 初见《构建之法》orz……
作为一个大二的计科狗,还是对软件设计有些懵,尽管之前学习过软件工程,但并没有掌握,突如其来的软件设计实践,让我着实傻眼. 通过消息得知,要学习<构建之法>,就去搜了一下,它是以实践为基础的 ...
- 2019-04-16-day033-锁与队列
内容回顾 几个问题 概念多,练习少 不问问题 概念?代码? Process类 并发并行 并发 是同一时间段内多个任务交替使用同一个cpu 并行 是在同一个时刻多个任务在不同的cpu上同时执行 同步异步 ...
- Element UI样式无法修改解决方法。
最近在做的项目中要用到Element UI组件来写,非常方便,但毕竟Element UI是有它自己的默认样式的,并不是客户所要求的,但就在我想要修改样式时遇到了棘手的问题. 如何引入和使用 Eleme ...
- Quartz在Spring中动态设置cronExpression
什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定). 这样总不能修改配置文件每定制个定时任务就增加一个trigger吧,即便允许客户修改配置文 ...
- Vue2.0
Vue 1.0到2.0的变化: 1.在每个组件的模板中,不再支持片段代码 组件中的模板: 之前: <template> <h3我是组件<strong>我是加粗模板< ...
- C++ 跨语言调用 Java
C++ 跨语言调用 Java Java JDK 提供了 JNI 接口供 C/C++ 程序调用 Java 编译后的类与方法,主要依赖于头文件(jni.h) 和 动态库(jvm.so/jvm.dll),由 ...
- CART-GBRT-GBDT
CART:分类回归树 分类树和回归树的区别:分裂节点时使用的节点非纯度量(最小化准则.特征选择)不一样,修剪树的准则不一样 回归树: 节点非纯度量:平方误差和 区域估计值:均值(在给定的划分下,均值带 ...