五级地址选择器 微信小程序 四级五级各类地址数据文件

成品展示

由于实在找不到适合直接导入微信小程序数据库的文件,我就自己动手经过程序修改,生成了可直接导入云数据库的文件,顺手整理了一些地址文件

自定义组件形式(链接中没有)

<view class="area2-select-wrap {{showArea?'show':''}}">
  <!-- 遮罩层,点击到就关掉弹出层 -->
  <view class="area2-select-mask" bindtouchend="areaClose"></view>
  <!-- 地址选择器 由上往下分层 -->
  <view class="area2-select-inner">
            <!-- 第一层,关闭图标 和 标题:选择地点  -->
            <view class="area2-select-header">
                <view class="area2-close-wrap" bindtap="areaClose">
                    <image class="area2-close-icon" src="../../images/area_close.png"></image>
                </view>
                <view>选择地点</view>
            </view>
            <!-- 第二层,小标题,所选择的 省 市 区 镇 村 -->
            <view class="area2-sub-header" >
                <!--  省级选项头 出现条件:无 -->
                <view wx:if="{{provinceList.length && curr_pro}}" class="area2-sub-item {{currentTab==1?'on':''}}" data-current="1" bindtap="tiao"><text>{{curr_pro}}</text></view>
                <!-- 市级选项头 出现条件:市数组已确定 -->
                <view wx:if="{{cityList.length && curr_cit}}" class="area2-sub-item {{currentTab==2?'on':''}}" data-current="2" bindtap="tiao"><text>{{curr_cit}}</text></view>
                <!-- 区级选项头 出现条件:区数组已确定 -->
                <view wx:if="{{countryList.length && curr_cou}}" class="area2-sub-item {{currentTab==3?'on':''}}" data-current="3" bindtap="tiao"><text>{{curr_cou}}</text></view>
                <!-- 镇级选项头 出现条件:镇数组已确定 -->
                <view wx:if="{{streetList.length && curr_str}}" class="area2-sub-item {{currentTab==4?'on':''}}" data-current="4" bindtap="tiao"><text>{{curr_str}}</text></view>
                <!-- 村级选项头 出现条件:村数组已确定 -->
                <view wx:if="{{villageList.length && curr_vil}}" class="area2-sub-item {{currentTab==5?'on':''}}" data-current="5" bindtap="tiao"><text>{{curr_vil}}</text></view> 
            </view>
            <!-- 第三层 -->
            <view class="area2-item-wrap">
                <!-- 滑块容器 -->
                <swiper current="{{currentTab-1}}" class="area2-swiper-box" duration="300" bindchange="bindChange">
                    <!-- 省滑块 存在条件:省数组确定 -->
                    <block wx:if="{{provinceList.length && curr_pro}}">
                        <swiper-item wx:if="{{provinceList.length}}">
                            <scroll-view scroll-y="true" style="height:100%">
                                <view class="area2-swiper-inner">
                                    <!-- 遍历渲染省数组 绑定了点击事件selectResideprovince -->
                                    <view bindtap="selectResideprovince"  data-code="{{item.code}}" data-name="{{item.name}}" class="area2-swiper-item {{item.name==curr_pro?'on':''}}" wx:for="{{provinceList}}" wx:key="code">
                                        <view class="area2-swiper-areaname">{{item.name}}</view>
                                    </view>
                                </view>
                            </scroll-view>
                        </swiper-item>
                    </block>
                    <!-- 市滑块 存在条件:市数组确定 -->
                    <block wx:if="{{cityList.length && curr_cit}}">
                        <swiper-item wx:if="{{cityList.length}}">
                            <scroll-view scroll-y="true" style="height:100%">
                                <view class="area2-swiper-inner">
                                    <!-- 遍历渲染市数组 绑定了点击事件selectResidecity -->
                                    <view bindtap="selectResidecity" data-name="{{item.name}}" data-code="{{item.code}}"class="area2-swiper-item {{item.name==curr_cit?'on':''}}" 
                                    wx:for="{{cityList}}" wx:key="name">
                                        <view class="area2-swiper-areaname">{{item.name}}</view>
                                    </view>
                                </view>
                            </scroll-view>
                        </swiper-item>
                    </block> 
                    <!-- 区滑块 存在条件:区数组确定  -->
                    <block wx:if="{{countryList.length && curr_cou}}">
                        <swiper-item wx:if="{{countryList.length}}">
                            <scroll-view scroll-y="true" style="height:100%">
                                <view class="area2-swiper-inner">
                                    <!-- 遍历渲染区数组 绑定点击事件selectCountry -->
                                    <view bindtap="selectCountry" data-name="{{item.name}}" data-code="{{item.code}}"class="area2-swiper-item {{item.name==curr_cou?'on':''}}" 
                                    wx:for="{{countryList}}" wx:key="name">
                                        <view class="area2-swiper-areaname">{{item.name}}</view>
                                    </view>
                                </view>
                            </scroll-view>
                        </swiper-item>
                    </block>
                    <!-- 镇滑块 存在条件:镇数组确定 -->
                    <block wx:if="{{streetList.length && curr_str}}">
                        <swiper-item wx:if="{{streetList.length}}">
                            <scroll-view scroll-y="true" style="height:100%">
                                <view class="area2-swiper-inner">
                                    <!-- 遍历渲染市数组 绑定了点击事件selectStreet -->
                                    <view bindtap="selectStreet" data-name="{{item.name}}" data-code="{{item.code}}"class="area2-swiper-item {{item.name==curr_str?'on':''}}" 
                                    wx:for="{{streetList}}" wx:key="name">
                                        <view class="area2-swiper-areaname">{{item.name}}</view>
                                    </view>
                                </view>
                            </scroll-view>
                        </swiper-item>
                    </block> 
                    <!-- 村滑块 存在条件:村数组确定  -->
                    <block wx:if="{{villageList.length && curr_vil}}">
                        <swiper-item wx:if="{{villageList.length}}">
                            <scroll-view scroll-y="true" style="height:100%">
                                <view class="area2-swiper-inner">
                                    <!-- 遍历渲染村数组 绑定点击事件selectVillage -->
                                    <view bindtap="selectVillage" data-name="{{item.name}}" class="area2-swiper-item {{item.name==curr_vil?'on':''}}" 
                                    wx:for="{{villageList}}" wx:key="name">
                                        <view class="area2-swiper-areaname">{{item.name}}</view>
                                    </view>
                                </view>
                            </scroll-view>
                        </swiper-item>
                    </block>
                </swiper>
            </view>

  </view>
</view>
const db = wx.cloud.database();
const _ = db.command;
const App = getApp();
Component({
  data: {
    provinceList:[],
    cityList:[],
    countryList:[],
    streetList:[],
    villageList:[],
    curr_pro:'',
    curr_cit:'',
    curr_cou:'',
    curr_str:'',
    curr_vil:'',
    currentTab: 1,
  },
  properties: {
    address:{
      type: Object
    },
    showArea:{
      type:Boolean,
      value: false
    }
  },
  lifetimes: {
    attached: function() {
    // 在组件实例进入页面节点树时执行
    this.choosearea();
    },
  },
  methods: {
  //打开地点选择器
  choosearea:async function () {
    this.getProvinceList();
    this.setData({
      currentTab:1,//赋值当前地址栏
      //赋值地址栏头
      curr_pro: '请选择',
      curr_cit: '请选择',
      curr_cou:'请选择',
      curr_str:'请选择',
      curr_vil:'请选择',
    });
  },
  //从数据库集合address获取省数据
  getProvinceList(){
    console.log('地址选择器初始化');
    db.collection('address')
    .where({_id:"省级"})
    .get()
    .then((res)=>{
      this.setData({provinceList : res.data[0].provinceList })
    })
  },
  //点击选择省
  selectResideprovince: async function (e) {
    let code = e.currentTarget.dataset.code;
    let name = e.currentTarget.dataset.name;
    if(name === this.data.curr_pro) return;
    let cityList = [];
    let country = [];
    let streetList = [];
    let villageList = [];
    //生成市数组 
    db.collection('address')
    .where({_id:"市级"})
    .get()
    .then((res)=>{
      cityList = (res.data[0])[code];
      this.setData({
        currentTab:2,
        cityList: cityList,
        countryList:country,
        streetList:streetList,
        villageList:villageList,
        curr_pro:name,
        curr_cit:'请选择',
        curr_cou:'请选择',
        curr_str:'请选择',
        curr_vil:'请选择'
      })
    });
  },
  //点击选择市
  selectResidecity:function (e) {
    let name = e.currentTarget.dataset.name;
    let code = e.currentTarget.dataset.code;
    if( name === this.data.curr_cit )return;
    //生成区数组
    db.collection("address")
    .where({
      _id:"区级"
    })
    .get()
    .then((res)=>{
      var countryList = (res.data[0])[code];
      this.setData({
        currentTab:3,
        countryList: countryList,
        streetList:[],
        villageList:[],
        curr_cit:name,
        curr_cou:'请选择',
        curr_str:'请选择',
        curr_vil:'请选择'
      });
    })
  },
  //点击选择区
  selectCountry:function (e) {
    let name = e.currentTarget.dataset.name;
    let code = e.currentTarget.dataset.code;
    if(name===this.data.curr_cou)return;
    //生成镇数组
    db.collection("address")
    .where({
      _id:code
    })
    .get()
    .then((res)=>{
      var streetList = res.data[0].streetList;
      this.setData({
        currentTab:4,
        streetList: streetList,
        villageList: [],
        curr_cou: name,
        curr_str:'请选择',
        curr_vil:'请选择'
      });
    })
  },
  //点击选择镇
  selectStreet(e){
    let name = e.currentTarget.dataset.name;
    let code = e.currentTarget.dataset.code;
    if(name===this.data.curr_str)return;
    //生成街数组
    db.collection("address")
    .where({
      _id:code
    })
    .get()
    .then((res)=>{
      var villageList = res.data[0].vlliageList;
      this.setData({
        currentTab:5,
        villageList: villageList,
        curr_str: name,
        curr_vil:'请选择'
      });
    })
  },
  //点击选择村
  async selectVillage(e){
    let name = e.currentTarget.dataset.name;
    if(name===this.data.curr_vil)return;
    var address = {
      province: this.data.curr_pro,
      city: this.data.curr_cit,
      country: this.data.curr_cou,
      street: this.data.curr_str,
      village: name
    }
    this.setData({
      currentTab:5,
      curr_vil: name,
      //showArea:false
    })
    //this.triggerEvent('changeAddress',{address:address})
  },
  // 滑动切换tab
  bindChange: function( e ) {
    var that = this;
    var t = this.data.currentTab;
    that.setData({
      currentTab: e.detail.current + 1
    },()=>{
      if(this.data.currentTab!=t) console.log('左右滑动导致滑块改变')
    });
  },
  //隐藏弹出层
  areaClose:async function () {
    var obj={};
    if(this.data.curr_pro !=="请选择")obj.province=this.data.curr_pro;
    if(this.data.curr_cit !=="请选择")obj.city=this.data.curr_cit;
    if(this.data.curr_cou !=="请选择")obj.country=this.data.curr_cou;
    if(this.data.curr_str !=="请选择")obj.street=this.data.curr_str;
    if(this.data.curr_vil !=="请选择")obj.village=this.data.curr_vil;
    if( Object.keys(obj).length < 5  ){
     //console.log('Address1');
      return this.setData({showArea:false});
    }else if(JSON.stringify(obj)!=JSON.stringify(this.data.address)){
      //console.log('Address2');
        this.setData({
          showArea:false,
          address:obj,
      });
      this.triggerEvent('changeAddress',{address:obj}) 
    }else{
      return this.setData({showArea:false});
    }
  },
  //点击选项卡
  tiao(e){
   var c = parseInt(e.currentTarget.dataset.current);
   this.setData({currentTab : c})
  }
  },
  

});

下面是包含了五级地址器DEMO和各类地址文件的链接

https://gitee.com/YYCCbigbig/five-level-address-selector.git

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Uniapp是一款跨平台的开发框架,可以用于开发多种类型的应用程序,包括小程序、H5、App等。对于自定义省市三级联动组件,我们可以参照以下步骤来实现。 首先,我们需要获取省市的数据源,可以从后台接口获取或者使用静态的json文件。将数据源存储在data的变量中。 接下来,我们可以创建一个Picker组件来实现三级联动选择。通过使用uni-app的Picker组件,我们可以选择省份,然后根据选择的省份,展示对应的城市和区的选项。同时,需在Picker组件上设置改变省份选项时的change事件,以及城市和区选项的列数、初始值等属性。 在change事件中,我们根据选择的省份,从数据源中获取对应的城市和区选项,并更新到data的变量中。然后,将更新后的data传递给Picker组件,刷新选项。 最后,我们可以在页面上引入并使用自定义的省市三级联动组件。通过v-model指令,将选择的省市的值与页面的数据进行双向绑定,并可以在提交时获取选中的省市的值,进行后续的业务处理。 通过以上步骤,我们可以实现自定义的省市三级联动组件。根据不同的需求,我们还可以对组件进行优化和扩展,例如添加搜索功能、增加默认值等。总之,Uniapp提供了强大的开发能力,可以帮助我们轻松地实现自定义的省市三级联动组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值