vue实现省市区三级联动,附文件代码

vue项目中使用到三级联动,现在自己实现一个三级联动,仅供大家参考一下,直接上代码。

<template>
        <el-form-item label="地址:">
          <div style="display:flex;margin-bottom: -18px;">
            <el-select v-model="ruleForm.address.country"
                       filterable
                       size="small"
                       style="width:160px;margin-right:10px"
                       @change="domesticClick">
              <el-option v-for="item in country_list"
                         :key="item.value"
                         :label="item.name"
                         :value="item.name" />
            </el-select>
            <mFormAddressCommon v-if="!showCountryEnLive"
                    v-model="ruleForm.address"
                    :show-county="true"
                    style="width:100%;" />
          </div>
        </el-form-item>
        </template>
<script>
import mFormAddressCommon from '@components/m-form/m-form-address-common.vue'
import country from '@assets/js/country'
import { mapActions, mapGetters } from 'vuex'

export default {
  components: {
    mFormAddressCommon,

  },
  data() {
    return {
      ruleForm: {
        address: {
          country: '中国大陆',
          province: '',
          city: '',
          county: '',
          details: '',
          postcode: '',
        },

      },
      country_list: country,
      showCountryEnLive: false,
    }
   
  },
  computed: {
    ...mapGetters({
      user: 'getUserInfo',
      group: 'getGroup',

    }),
  },
  methods: {
    domesticClick(row) {
      console.log(row)
      if (row != '中国大陆') {
        this.showCountryEnLive = true
      } else {
        this.showCountryEnLive = false
      }
    },
  },
}
</script>

mFormAddressCommon 组件代码

​
<template>
  <div>
    <el-row
      type="flex"
      justify="space-between"
    >
      <el-col>
        <el-form-item prop="province">
          <el-select
            v-model="content.province"
            size="small"
            placeholder="省份"
            @change="changeProvince"
          >
            <el-option
              v-for="item in provinceArr"
              :key="item.value"
              :value="item.label"
            >{{ item.label }}</el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col>
        <el-form-item prop="city" style="margin-left:10px">
          <el-select
            v-model="content.city"
            size="small"
            placeholder="城市"
            @change="changeCity"
          >
            <el-option
              v-for="item in citiesArr"
              :key="item.value"
              :value="item.label"
            >{{ item.label }}</el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-if="showCounty" style="margin-left:10px">
        <el-form-item prop="county">
          <el-select

            v-model="content.county"
            size="small"
            placeholder="区县"
            @change="changeCounty"
          >
            <el-option
              v-for="item in countyArr"
              :key="item.value"
              :value="item.label"
            >{{ item.label }}</el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <!-- <el-input type="text"
    size="small"
              v-model="content.details"
              placeholder="请输入详细地址" /> -->
  </div>
</template>

<script>
import { provinces } from '@/assets/js/provinces2'

export default {
  name: 'MFormAddressCommon', // 选择省份城市(通用)
  model: {
    prop: 'content',
    event: 'change'
  },
  props: {
    content: {
      type: Object,
      default: {
        province: '', // 省份
        city: '', // 城市
        county: '', // 区县
        postcode: '',
        details: '' // 详细
      }
    },
    disabled: {
      type: Boolean,
      default: false
    },
    showCounty: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      provinceArr: [],
      citiesArr: [],
      countyArr: []

    }
  },
  created() {
    this.provinceArr = provinces.data
  },
  mounted() {
    setTimeout(() => {
      if (this.content.province) {
        this.changeProvinceInit(this.content.province)
      }
      if (this.content.city) {
        this.changeCityInit(this.content.city)
      }
    }, 400)
  },
  methods: {
    async changeProvinceInit(val) {
      let item = null
      this.provinceArr.forEach((c) => {
        if (c.label === val) {
          item = c
        }
      })
      if (!item) return
      this.citiesArr = item.children
      this.countyArr = []
    },
    async changeCityInit(val) {
      let item = null
      this.citiesArr.forEach((c) => {
        if (c.label === val) {
          item = c
        }
      })
      if (!item) return
      this.countyArr = item.children || []
    },
    async changeProvince(val) {
      let item = null
      console.log('changeProvince', val)
      console.log('changeProvince', this.provinceArr)
      this.provinceArr.forEach((c) => {
        if (c.label === val) {
          item = c
        }
      })
      if (!item) return
      this.citiesArr = item.children
      this.countyArr = []
      this.content.city = ''
      this.content.county = ''
      this.content.postcode = ''
      this.$emit('change', this.content)
    },
    async changeCity(val) {
      
      let item = null
      this.citiesArr.forEach((c) => {
        if (c.label === val) {
          item = c
        }
      })
      if (!item) return
      this.countyArr = item.children || []
      this.content.county = ''
      this.content.postcode = ''
      this.$emit('change', this.content)
    },
    // async changeCounty() {
    //   this.$emit('change', this.content)
    // }
    async changeCounty(val) {
      this.countyArr.forEach((c) => {
        if (c.label === val) {
          this.content.postcode = c.postCode
        }
      })
      console.log(this.content, val)
      this.$emit('change', this.content)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

​

以上内容仅供参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值