小程序组件开发中省市区三级联动的示例分析

发布时间:2021-09-13 09:42:46 作者:小新
来源:亿速云 阅读:131

小编给大家分享一下小程序组件开发中省市区三级联动的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

<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]}}"/>
  var city = require("../../utils/city.js");
Page({
    data: {},
    onLoad: function() {
        console.log('onLoad...');
        var that = this;
        city.init(that);
    }
});
<?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>
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
}

看完了这篇文章,相信你对“小程序组件开发中省市区三级联动的示例分析”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 小程序globalData的示例分析
  2. 小程序中云开发的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

小程序

上一篇:winform框架的winform怎样使用

下一篇:如何通过微信公众平台实现在线客服机器人功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》