首頁  >  文章  >  微信小程式  >  小程式組件開發中的有關省市區三級連動的相關介紹

小程式組件開發中的有關省市區三級連動的相關介紹

巴扎黑
巴扎黑原創
2017-09-12 09:28:271837瀏覽

小程式組件開發中的有關省市區三級連動的相關介紹效果圖:

小程式組件開發中的有關省市區三級連動的相關介紹

小程式組件開發中的有關省市區三級連動的相關介紹源碼

  • 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(&#39;onLoad...&#39;);
            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: [&#39;-请选择-&#39;, &#39;福建省&#39;],
        city: {
            &#39;-请选择-&#39;: [&#39;-请选择-&#39;],
            &#39;福建省&#39;: [&#39;福州市&#39;, &#39;厦门市&#39;, &#39;泉州市&#39;]
        },
        district: {
            &#39;-请选择-&#39;: [&#39;-请选择-&#39;],
            &#39;福州市&#39;: [&#39;鼓楼区&#39;, &#39;台江区&#39;],
            &#39;厦门市&#39;: [&#39;湖里区&#39;, &#39;集美区&#39;],
            &#39;泉州市&#39;: [&#39;晋江市&#39;, &#39;安溪县&#39;]
        },
        provinceIndex: 0,
        cityIndex: 0,
        districtIndex: 0,
        selectedProvince: &#39;-请选择-&#39;,
        selectedCity: &#39;-请选择-&#39;,
        selectedDistrct: &#39;-请选择-&#39;
    };
    function init(that) {
        that.setData({
            &#39;city&#39;: city
        });
        var bindProvinceChange = function(e) {
            var city = that.data.city;
            that.setData({
                &#39;city.provinceIndex&#39;: e.detail.value,
                &#39;city.selectedProvince&#39;: city.province[e.detail.value],
                &#39;city.selectedCity&#39;: city.city[city.province[e.detail.value]][0],
                &#39;city.selectedDistrct&#39;: city.district[city.city[city.province[e.detail.value]][0]][0],
                &#39;city.cityIndex&#39;: 0,
                &#39;city.districtIndex&#39;: 0
            });
        };
        var bindCityChange = function(e) {
            var city = that.data.city;
            that.setData({
                &#39;city.cityIndex&#39;: e.detail.value,
                &#39;city.selectedCity&#39;: city.city[city.selectedProvince][e.detail.value],
                &#39;city.districtIndex&#39;: 0,
                &#39;city.selectedDistrct&#39;: city.district[city.city[city.selectedProvince][e.detail.value]][0]
            });
        };
        var bindDistrictChange = function(e) {
            var city = that.data.city;
            that.setData({
                &#39;city.districtIndex&#39;: e.detail.value,
                &#39;city.selectedDistrct&#39;: city.district[city.selectedCity][e.detail.value]
            });
        };
        that[&#39;bindProvinceChange&#39;] = bindProvinceChange;
        that[&#39;bindCityChange&#39;] = bindCityChange;
        that[&#39;bindDistrictChange&#39;] = bindDistrictChange;
    }
    module.exports = {
        init: init
    }

以上是小程式組件開發中的有關省市區三級連動的相關介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn