微信小程序手動添加收貨地址省市區(qū)聯(lián)動
本文實例為大家分享了微信小程序手動添加收貨地址省市區(qū)聯(lián)動的具體代碼,供大家參考,具體內(nèi)容如下
先看效果圖


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部分
這部分代碼其實是因為后端同學(xué)太懶了,數(shù)據(jù)沒有整理就直接返回過來了。我人微言輕的,只好自己默默地整理了。
// 把數(shù)據(jù)格式化成頁面現(xiàn)實的形式
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;
// 第一遍格式化數(shù)據(jù),
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);
}
}
// 遍歷所有的數(shù)據(jù)。將省的名字放在對應(yīng)的數(shù)組中
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 {
// 如果該省沒有市,那么就把省的名字作為市和區(qū)的名字
city.push({
name: province[province_index].label,
id: province[province_index].cityId
});
area.push({
name: province[province_index].label,
id: province[province_index].areaId
});
}
// 選擇成功后把對應(yīng)的數(shù)組賦值給相應(yīng)的變量
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就是用來回顯之前選擇的城市。

把整理好的省市區(qū)分別遍歷一下,把當(dāng)前的id與數(shù)據(jù)中的id作對比。把相對應(yīng)的數(shù)據(jù)的下標給取出來。
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 });
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript數(shù)據(jù)結(jié)構(gòu)常見面試問題整理
在JavaScript中,數(shù)據(jù)結(jié)構(gòu)是指相互之間存在一種或多種特定關(guān)系的數(shù)據(jù)元素的集合,是帶有結(jié)構(gòu)特性的數(shù)據(jù)元素的集合。常用的數(shù)據(jù)結(jié)構(gòu)有:數(shù)組、列表、棧、隊列、鏈表、字典、集合等等2022-08-08
使用bootstrap莫名其妙出現(xiàn)橫向滾動條的問題及解決
這篇文章主要介紹了使用bootstrap莫名其妙出現(xiàn)橫向滾動條的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
小程序getLocation需要在app.json中聲明permission字段
這篇文章主要介紹了小程序getLocation需要在app.json中聲明permission字段,個別需要獲取用戶地理位置的在開發(fā)者工具調(diào)試時會出現(xiàn)getLocation需要在app.json中聲明permission字段 ,下面我們就一起來解決一下2019-04-04
MUI 實現(xiàn)側(cè)滑菜單及其主體部分上下滑動的方法
下面小編就為大家分享一篇MUI 實現(xiàn)側(cè)滑菜單及其主體部分上下滑動的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
輕松學(xué)習(xí)Javascript閉包函數(shù)
這篇文章主要幫助大家輕松學(xué)習(xí)掌握Javascript閉包函數(shù),從閉包的含義出發(fā),由淺入深學(xué)習(xí)Javascript閉包函數(shù),感興趣的小伙伴們可以參考一下2015-12-12
關(guān)于二級域名下使用一級域名下的COOKIE的問題
我們通常在使用cookie的時候一般都只是局限在本站內(nèi)使用,也就是只在一個域名下使用2011-11-11

