您好,登錄后才能下訂單哦!
小編給大家分享一下小程序組件開發中省市區三級聯動的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
index.wxml
<import src="../../templates/address-temp"/> <template is="addressPicker" data="{{provinceIndex:city.provinceIndex,cityIndex:city.cityIndex,districtIndex:city.districtIndex,province:city.province, city:city.city[city.selectedProvince],district:city.district[city.selectedCity]}}"/>
index.js
var city = require("../../utils/city.js"); Page({ data: {}, onLoad: function() { console.log('onLoad...'); var that = this; city.init(that); } });
address-temp.wxml
<?xml version="1.0" encoding="utf-8"?> <template name="addressPicker"> <view style="display:flex;margin:0;height:100%;align-items: center;justify-content: center"> <view style="width:100%;"> <picker bindchange="bindProvinceChange" value="{{provinceIndex}}" range="{{province}}"> <view style="text-align:center;padding:10rpx;font-size:0.8rem">{{province[provinceIndex]}}</view> </picker> </view> <view style="width:100%;"> <picker bindchange="bindCityChange" value="{{cityIndex}}" range="{{city}}"> <view style="text-align:center;padding:10rpx;font-size:0.8rem">{{city[cityIndex]}}</view> </picker> </view> <view style="width:100%;"> <picker bindchange="bindDistrictChange" value="{{districtIndex}}" range="{{district}}"> <view style="text-align:center;padding:10rpx;font-size:0.8rem">{{district[districtIndex]}}</view> </picker> </view> </view> </template>
city.js
var city = { province: ['-請選擇-', '福建省'], city: { '-請選擇-': ['-請選擇-'], '福建省': ['福州市', '廈門市', '泉州市'] }, district: { '-請選擇-': ['-請選擇-'], '福州市': ['鼓樓區', '臺江區'], '廈門市': ['湖里區', '集美區'], '泉州市': ['晉江市', '安溪縣'] }, provinceIndex: 0, cityIndex: 0, districtIndex: 0, selectedProvince: '-請選擇-', selectedCity: '-請選擇-', selectedDistrct: '-請選擇-' }; function init(that) { that.setData({ 'city': city }); var bindProvinceChange = function(e) { var city = that.data.city; that.setData({ 'city.provinceIndex': e.detail.value, 'city.selectedProvince': city.province[e.detail.value], 'city.selectedCity': city.city[city.province[e.detail.value]][0], 'city.selectedDistrct': city.district[city.city[city.province[e.detail.value]][0]][0], 'city.cityIndex': 0, 'city.districtIndex': 0 }); }; var bindCityChange = function(e) { var city = that.data.city; that.setData({ 'city.cityIndex': e.detail.value, 'city.selectedCity': city.city[city.selectedProvince][e.detail.value], 'city.districtIndex': 0, 'city.selectedDistrct': city.district[city.city[city.selectedProvince][e.detail.value]][0] }); }; var bindDistrictChange = function(e) { var city = that.data.city; that.setData({ 'city.districtIndex': e.detail.value, 'city.selectedDistrct': city.district[city.selectedCity][e.detail.value] }); }; that['bindProvinceChange'] = bindProvinceChange; that['bindCityChange'] = bindCityChange; that['bindDistrictChange'] = bindDistrictChange; } module.exports = { init: init }
看完了這篇文章,相信你對“小程序組件開發中省市區三級聯動的示例分析”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。