微信小程序手動添加收貨地址省市區(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部分
這部分代碼其實是因為后端同學太懶了,數(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ù)。將省的名字放在對應的數(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
});
}
// 選擇成功后把對應的數(shù)組賦值給相應的變量
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ū)分別遍歷一下,把當前的id與數(shù)據(jù)中的id作對比。把相對應的數(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)在關注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JavaScript如何讓select選擇框可輸入和可下拉選擇
我們知道一般select下拉框是只能選擇的,而有時我們會遇到下拉框中沒有要選擇的信息項或者下拉選項特別多時,需要允許用戶輸入想要的內(nèi)容,這篇文章主要給大家介紹了關于JavaScript如何讓select選擇框可輸入和可下拉選擇的相關資料,需要的朋友可以參考下2023-10-10
js實現(xiàn)綠白相間豎向網(wǎng)頁百葉窗動畫切換效果
這篇文章主要介紹了js實現(xiàn)綠白相間豎向網(wǎng)頁百葉窗動畫切換效果,實例分析了javascript實現(xiàn)百葉窗動畫效果的技巧,需要的朋友可以參考下2015-03-03
JavaScript實現(xiàn)網(wǎng)頁圖片等比例縮放實現(xiàn)代碼及調(diào)用方式
為了保證圖片統(tǒng)一大小,直接設置圖片大小又會導致圖片拉伸,造成圖片模糊,接下來將介紹的代碼可以在圖片加載完成后自動按比例調(diào)整圖片大小,感興趣的你可以參考下2013-02-02
JavaScript實現(xiàn)電商平臺商品細節(jié)圖
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)電商平臺商品細節(jié)圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
javascript中innerText和innerHTML屬性用法實例分析
這篇文章主要介紹了javascript中innerText和innerHTML屬性用法,實例分析了javascript中innerText和innerHTML屬性的作用和相關的使用技巧,需要的朋友可以參考下2015-05-05
在JavaScript 中按字母排序之如何在 JS 中按名稱排序
有時你可能有一個單詞數(shù)組,你想按字母順序(從 a-z)對每個單詞進行排序,或者你可能有一個包含用戶信息(包括名字)的對象數(shù)組,例如,你想按照用戶的名字來排序,接下來通過本文給大家介紹在JavaScript 中按字母排序之如何在 JS 中按名稱排序,需要的朋友可以參考下2023-09-09
js實現(xiàn)按鈕控制圖片360度翻轉(zhuǎn)特效的方法
這篇文章主要介紹了js實現(xiàn)按鈕控制圖片360度翻轉(zhuǎn)特效的方法,涉及HTML5中canvas方法的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02

