微信小程序手动添加收货地址省市区联动
更新时间:2020年5月19日 09:39 点击:1944
本文实例为大家分享了微信小程序手动添加收货地址省市区联动的具体代码,供大家参考,具体内容如下
先看效果图
html部分
用小程序的piceker-view 嵌入页面的滚动选择器
<picker-view indicator-style="height: 50px;" style="width:100%; height: 400rpx;" bindchange="bindChange"> <picker-view-column class="selectItem"> <view class="tooth" wx:for="{{province}}" wx:key="this">{{item.name}}</view> </picker-view-column> <picker-view-column class="selectItem"> <view class="tooth" wx:for="{{city}}" wx:key="this">{{item.name}}</view> </picker-view-column> <picker-view-column class="selectItem"> <view class="tooth" wx:for="{{area}}" wx:key="this">{{item.name}}</view> </picker-view-column> </picker-view>
js部分
这部分代码其实是因为后端同学太懒了,数据没有整理就直接返回过来了。我人微言轻的,只好自己默默地整理了。
// 把数据格式化成页面现实的形式 formatCityData: function () { var that = this, region = that.data.region, selectItems = [], province = [], city = [], area = [], area_index = that.data.area_index, city_index = that.data.city_index, province_index = that.data.province_index; // 第一遍格式化数据, for (var i = 0; i < region.length; i++) { if (region[i].parent_id == 1) { var provinceItem = region[i]; var selectItem1 = { label: provinceItem.zh_name, provinceId: provinceItem.id, children: [] }; for (var j = 0; j < region.length; j++) { if (region[j].parent_id == provinceItem.id) { var cityItem = region[j]; var selectItem2 = { label: cityItem.zh_name, cityId: cityItem.id, children: [] }; selectItem1.children.push(selectItem2); for (var k = 0; k < region.length; k++) { if (region[k].parent_id == cityItem.id) { var areaItem = region[k]; var selectItem3 = { label: areaItem.zh_name, areaId: areaItem.id, children: [] }; selectItem2.children.push(selectItem3); } } } } selectItems.push(selectItem1); } } // 遍历所有的数据。将省的名字放在对应的数组中 for (let i = 0; i < selectItems.length; i++) { province.push({ name: selectItems[i].label, id: selectItems[i].provinceId }); } if (selectItems[province_index].children && selectItems[province_index].children) {// 判断选中的省级里面有没有市 if (selectItems[province_index].children[city_index]) { for (let i = 0; i < selectItems[province_index].children.length; i++) { city.push({ name: selectItems[province_index].children[i].label, id: selectItems[province_index].children[i].cityId }); } if (selectItems[province_index].children[city_index].children) { if (selectItems[province_index].children[city_index].children[area_index]) { for (let i = 0; i < selectItems[province_index].children[city_index].children.length; i++) { area.push({ name: selectItems[province_index].children[city_index].children[i].label, id: selectItems[province_index].children[city_index].children[i].areaId }); } } else { that.setData({ area_index: 0 }); for (let i = 0; i < selectItems[province_index].children[city_index].childre.length; i++) { area.push({ name: selectItems[province_index].children[city_index].children[i].label, id: selectItems[province_index].children[city_index].children[i].areaId }); } } } else { area.push({ name: province[province_index].children[city_index].label, id: province[province_index].children[city_index].areaId }); } } else { that.setData({ city_index: 0 }); for (let i = 0; i < selectItems[province_index].childre.length; i++) { city.push({ name: selectItems[province_index].children[i].label, id: selectItems[province_index].children[i].cityId }); } } } else { // 如果该省没有市,那么就把省的名字作为市和区的名字 city.push({ name: province[province_index].label, id: province[province_index].cityId }); area.push({ name: province[province_index].label, id: province[province_index].areaId }); } // 选择成功后把对应的数组赋值给相应的变量 that.setData({ province: province, city: city, area: area }); provincialCity = { province: { name: province[that.data.province_index].name, id: province[that.data.province_index].id, }, city: { name: city[that.data.city_index].name, id: city[that.data.city_index].id }, area: { name: area[that.data.area_index].name, id: area[that.data.area_index].id } } },
后台返回的格式如下:
// 选择城市 bindChange: function (e) { const val = e.detail.value; this.setData({ province_index: val[0], city_index: val[1], area_index: val[2] }); this.formatCityData(); },
前面说的是新增收获地址,后面是编辑
html部分
value就是用来回显之前选择的城市。
把整理好的省市区分别遍历一下,把当前的id与数据中的id作对比。把相对应的数据的下标给取出来。
this.data.province.map(function(val, key) { if (val.id === that.data.result.province.id) { provinceId = key; } return provinceId; }) this.setData({ province_index: provinceId }); this.formatCityData(); this.data.city.map(function(val, key) { if (val.id === that.data.result.city.id) { cityId = key; } return cityId; }) this.setData({ city_index: cityId }); this.formatCityData(); this.data.area.map(function (val, key) { if (val.id === that.data.result.area.id) { areaId = key; } return areaId; }) var lists = [provinceId, cityId, areaId]; this.setData({ cityShow: lists });
为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
下一篇: js实现自定义右键菜单
相关文章
- ...2015-12-29
- 这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
- 这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
- 二级联动下拉菜单选择应用在在很多地方,比如说省市下拉联动,商品大小类下拉选择联动。本文将通过实例讲解使用jQuery+PHP+MySQL来实现大小分类二级下拉联动效果。 实现效果:当选择大类时,小类下拉框里的选项内容也随着改...2015-10-30
- 这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
- 这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
- c#获取本机的IP地址的代码,需要的朋友可以参考一下...2020-06-25
- 这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
- 这篇文章主要介绍了微信小程序(应用号)开发新闻客户端实例的相关资料,需要的朋友可以参考下...2016-10-25
- 这篇文章主要介绍了微信小程序 页面跳转传递值几种方法详解的相关资料,需要的朋友可以参考下...2017-01-16
- 这篇文章主要介绍了微信小程序手势操作之单触摸点与多触摸点的相关资料,需要的朋友可以参考下...2017-03-13
- 这篇文章主要为大家详细介绍了微信小程序实现canvas分享朋友圈海报,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-21
- 这篇文章主要为大家详细介绍了JavaScript实现网页下拉列表的省市联动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-07
- 这篇文章主要为大家详细介绍了微信小程序实现选择地址省市区三级联动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-21
- 这篇文章主要介绍了uniapp,微信小程序中使用 MQTT的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-07-11
- 这篇文章主要为大家详细介绍了微信小程序实现点击导航条切换页面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-11-19
- 这篇文章主要介绍了微信小程序自定义底部弹出框动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-11-16
jQuery EasyUI编辑DataGrid用combobox实现多级联动
本文给大家分享jQuery EasyUI编辑DataGrid用combobox实现多级联动效果的实例代码,代码简单易懂,非常不错,具有参考借鉴价值,感兴趣的朋友一起看看吧...2016-09-01- 这篇文章主要介绍了微信小程序 通过控制CSS实现view隐藏与显示的相关资料,需要的朋友可以参考下...2017-05-27
- 这篇文章主要为大家详细介绍了微信小程序入门之绘制时钟,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-10-23