vue 世界城市下拉模拟组件

 1.引入AddresJson.js城市数据文件

<template>
  <div>
  <div style="text-align:center;margin-bottom:10px;">
      <el-link type="primary" v-show="obj.GlobalRegionName">{{obj.GlobalRegionName}}</el-link>
      <el-link type="success" v-show="obj.CountryName">{{obj.CountryName}}</el-link>
      <el-link type="warning" v-show="obj.ProvinceName">{{obj.ProvinceName}}</el-link>
      <el-link type="danger" v-show="obj.UrbanCountyName">{{obj.UrbanCountyName}}</el-link>
      <el-link type="info" v-show="obj.CountyTownName">{{obj.CountyTownName}}</el-link>

  </div>
    <el-select v-model="GlobalRegionId" placeholder="请选择区域" @change="SelectGlobalRegionFn()" data-id="1"> 
        <el-option
          v-for="item in AddresData"
          :key="item.id"
          :label="item.name"
          :value="item.id"
          :path="item.path"
          :level="item.level">
           <span style="float: left">{{ item.name }}</span>
           <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name_en }}</span>
        </el-option>
  </el-select>
  <el-select v-model="CountryId" :disabled="CountryState" placeholder="请选择国家" @change="SelectCountryFn()" data-id="2">
        <el-option
          v-for="item in Country"
          :key="item.id"
          :label="item.name"
          :value="item.id">
           <span style="float: left">{{ item.name }}</span>
           <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name_en }}</span>
        </el-option>
  </el-select>
  <el-select v-model="ProvinceId" :disabled="ProvinceState" placeholder="请选择省份" @change="SelectProvinceFn()" data-id="3">
        <el-option
          v-for="item in Province"
          :key="item.id"
          :label="item.name"
          :value="item.id">
           <span style="float: left">{{ item.name }}</span>
           <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name_en }}</span>
        </el-option>
  </el-select>
  <el-select v-model="UrbanCountyId" :disabled="UrbanCountyState" placeholder="请选择市区" @change="SelectUrbanCountyFn()" v-show="UrbanCountyShow" data-id="4">
        <el-option
          v-for="item in UrbanCounty"
          :key="item.id"
          :label="item.name"
          :value="item.id">
           <span style="float: left">{{ item.name }}</span>
           <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name_en }}</span>
        </el-option>
  </el-select>
  <el-select v-model="CountyTownId" :disabled="CountyTownState" placeholder="请选择县" v-show="CountyTownShow" @change="SelectCountyTownFn()" data-id="5">
        <el-option
          v-for="item in CountyTown"
          :key="item.id"
          :label="item.name"
          :value="item.id">
           <span style="float: left">{{ item.name }}</span>
           <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name_en }}</span>
        </el-option>
  </el-select>

  </div>
</template>

<script>
import AddresData from '../lib/AddresJson';
import ObjectAddressn from '../lib/common';
export default {
  name: 'Demo',
  data () {
    return {
       AddresData,
       GlobalRegionId:"",
       CountryId:"",
       ProvinceId:"",
       UrbanCountyId:"",
       CountyTownId:"",
       Province:[],
       Country:[],
       UrbanCounty:[],
       CountyTown:[],
       CountryState:true,
       ProvinceState:true,
       UrbanCountyState:true,
       CountyTownState:true,
       UrbanCountyShow:true,
       CountyTownShow:true,
       obj:{}
    }
  },
  methods:{
    SelectGlobalRegionFn(){
       let _this=this;
       _this.CountryId="";
       _this.ProvinceId="";
       _this.UrbanCountyId="";
       _this.CountyTownId="";
       _this.Province=[];
       _this.Country=[];
       _this.CountyTown=[];
       _this.UrbanCounty=[];
       _this.obj={};
       const AddresData=new ObjectAddressn(_this.GlobalRegionId,_this.AddresData);
       _this.Country=AddresData.Arr;
       _this.obj.GlobalRegionName=AddresData.Name;
       _this.CountryState=AddresData.State;

    },
    SelectCountryFn(){
        let _this=this;
        _this.ProvinceId="";
        _this.Province=[];
        _this.UrbanCountyId="";
        _this.UrbanCounty=[];
       const AddresData=new ObjectAddressn(_this.CountryId,_this.Country);
       _this.Province=AddresData.Arr;
       _this.obj.CountryName=AddresData.Name;
       _this.ProvinceState=AddresData.State;
       
    },
    SelectProvinceFn(){
        let _this=this;
        _this.UrbanCountyId="";
        _this.UrbanCounty=[];
        _this.CountyTownId="";
        _this.CountyTown=[];
        const AddresData=new ObjectAddressn(_this.ProvinceId,_this.Province);
       _this.UrbanCounty=AddresData.Arr;
       _this.obj.ProvinceName=AddresData.Name;
       _this.UrbanCountyState=AddresData.State;
       _this.UrbanCountyShow=AddresData.Show;
       _this.CountyTownShow=AddresData.Show;
    },
    SelectUrbanCountyFn(){
        let _this=this;
        _this.CountyTownId="";
        _this.CountyTown=[];
        const AddresData=new ObjectAddressn(_this.UrbanCountyId,_this.UrbanCounty);
        _this.CountyTown=AddresData.Arr;
        _this.obj.UrbanCountyName=AddresData.Name;
        _this.CountyTownState=AddresData.State;
        _this.CountyTownShow=AddresData.Show;
    }, 
    SelectCountyTownFn(){
         let _this=this;
         debugger
        const AddresData=new ObjectAddressn(_this.CountyTownId,_this.CountyTown);
        _this.obj.CountyTownName=AddresData.Name;
    }
  },

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

 

 2.简单封装获取数据 

import AddresData from '../lib/AddresJson'
class ObjectAddress {
    constructor(id, arrOBJ) {
        this.id = id;
        this.arrOBJ = arrOBJ;
        this.Arr = [];
        this.State = true;
        this.Show = true;
        this.Name = "";
        this.init();
    }
    init() {
        if (this.id != "") {
            let _this = this;
            debugger
            _this.arrOBJ.forEach(a => {
                if (a.id == _this.id) {
                    _this.Name = a.name;
                    a.childrens.forEach(b => {
                        _this.Arr.push(b)
                    });
                }
            });

            if (_this.Arr.length == 0) {
                this.State = true;
                this.Show = false;
            } else {
                this.State = false;
                this.Show = true;
            }
        }
    }

}
export default ObjectAddress

3.展示效果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑色咖啡 Ken

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值