Vue实现省市区三级联动el-select组件的示例代码

 更新时间:2023年02月20日 09:59:42   作者:chengqiuming  
这篇文章主要为大家详细介绍了Vue实现省市区三级联动el-select组件的方法,文中的示例代码讲解详细,具有一定的借鉴价值,需要的的可以参考一下

一 自定义组件

1 位置

2 代码

<template>
  <div class="areaSelect flex">
    <!-- 省选择框 -->
    <el-select
      filterable
      :disabled="disabled"
      v-model="province"
      :size="size"
      placeholder="省"
      @change="changeCode($event,0)">
      <el-option
        v-for="item in provinceList"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <!-- 市选择框 -->
    <el-select
      filterable
      :disabled="disabled"
      class="center_select"
      v-model="city"
      placeholder="市"
      @change="changeCode($event,1)">
      <el-option
        v-for="item in cityList"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <!-- 区选择框 -->
    <el-select
      filterable
      :disabled="disabled"
      v-model="area"
      placeholder="区"
      @change="changeCode($event,2)">
      <el-option
        v-for="item in areaList"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
  </div>
</template>
<script>
  export default {
    name: 'regionSelect',
    props: {
      size: '',
      disabled: {
        size: String,
        type: Boolean,
        default: false
      },
      code: {
        type: Object,
        default: () => {
          return {
            areaCode: '',
            areaName: '',
            cityCode: '',
            cityName: '',
            provinceCode: '',
            provinceName: ''
          }
        }
      }
    },
    data () {
      return {
        province: '',
        city: '',
        area: '',
        provinceList: [],
        cityList: [],
        areaList: []
      }
    },
    watch: {
      code (val) {
        if (val.areaName && val.areaName !== '') {
          this.province = val.provinceCode
          this.city = val.cityCode
          this.area = val.areaCode
          this.provinceCity(val.provinceCode)
          this.cityArea(val.cityCode)
        } else {
          this.cityList = []
          this.areaList = []
        }
      }
    },
    mounted () {
      if (this.code.areaName && this.code.areaName !== '') {
        this.province = this.code.provinceCode
        this.city = this.code.cityCode
        this.area = this.code.areaCode
        this.provinceCity(this.code.provinceCode)
        this.cityArea(this.code.cityCode)
      }
      this.getProvince()
    },
    methods: {
      resetArea () {
        this.province = ''
        this.city = ''
        this.area = ''
      },
      // 当 type == 0 ,data 表示省编码
      // 当 type == 1 ,data 表示市编码
      changeCode (data, type) {
        if (type === 0) {
          this.city = ''
          this.area = ''
          this.provinceCity(data)
        }
        if (type === 1) {
          this.area = ''
          this.cityArea(data)
        }
        if (this.province !== '' && this.city !== '' && this.area !== '') {
          this.$emit(
            'code', [{
              code: this.province,
              name: this.provinceList.find(
                (val) => val.value === this.province
              ).label
            }, {
              code: this.city,
              name: this.cityList.find(
                (val) => val.value === this.city
              ).label
            }, {
              code: this.area,
              name: this.areaList.find(
                (val) => val.value === this.area
              ).label
            }]
          )
        }
      },
      // 从后台获得省数据列表
      async getProvince () {
        let result = []
        let url = '/base/division/provinceList'
        let data = await this.$http.get(url)
        data.data.data.map((item) => {
          result.push({
            label: item.name,
            value: item.code
          })
        })
        this.provinceList = result
      },
      // 依据省编码获得市数据列表
      async provinceCity (code) {
        let result = []
        let url = '/base/division/cityList'
        let data = await this.$http({
          url: url,
          method: 'get',
          params: {
            provinceCode: code
          }
        })
 
 
        data.data.data.map((item) => {
          result.push({
            label: item.name,
            value: item.code
          })
        })
        this.cityList = result
      },
      // 依据市编码获得区数据列表
      async cityArea (code) {
        let url = '/base/division/districtList'
        let data = await this.$http({
          url: url,
          method: 'get',
          params: {
            cityCode: code
          }
        })
        let result = []
        data.data.data.map((item) => {
          result.push({
            label: item.name,
            value: item.code
          })
        })
        this.areaList = result
      }
    }
  }
</script>
<style>
  .center_select {
    margin: 0 10px;
  }
 
 
  .global_form .areaSelect {
    width: 70%;
  }
 
 
  .global_form .areaSelect .el-select {
    width: 33.33%;
  }
</style>

二 main.js 配置

// 行政区划三级选择
import RegionSelect from './components/regionSelect'
// 行政区划三级选择
Vue.use(RegionSelect)
// 行政区划三级选择
Vue.component('regionSelect', RegionSelect)

三 使用方法

1 结构部分

<regionSelect
  :code="item.value"
  :disabled="item.disabled"
  :size="layout.size"
  @code="changeCode($event,item.prop)"
  v-if="item.type==='region'"
  ref="selectArea"
></regionSelect>

2 代码部分

searchForm: {
  province: '', // 省
  city: '', // 市
  district: '' // 区
},
item: { // 省市区 select 自定义组件传参部分
  value: '',
  type: 'region',
  disabled: false
},
layout: { // 选择框样式,用于传参
  size: ''
},

3 样式部分

// 省市区选择框改变时,传递出来已选择的值
changeCode (data, prop) {
  this.searchForm.province = data[0].name
  this.searchForm.city = data[1].name
  this.searchForm.district = data[2].name
},
// 重置选择框
resetForm () {
  this.$refs.selectArea[0].resetArea()  // 清除省市区
}

四 测试

1 级联选择

2 观察数据

到此这篇关于Vue实现省市区三级联动el-select组件的示例代码的文章就介绍到这了,更多相关Vue省市区三级联动组件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 手把手教你如何在vue项目中使用rem布局

    手把手教你如何在vue项目中使用rem布局

    公司内部一直有大屏的需求,也一直再做,中途也踩了一些坑,但是没有认真的来总结下,下面这篇文章主要给大家介绍了关于如何在vue项目中使用rem布局的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-02-02
  • Vue3实现下拉选择框多选功能的方法详解

    Vue3实现下拉选择框多选功能的方法详解

    在vue的实际开发过程中,我们如何将以选中的值直接渲染到页面中,下面这篇文章主要给大家介绍了关于Vue3实现下拉选择框多选功能的相关资料,需要的朋友可以参考下
    2023-09-09
  • Vue2 响应式系统之数组

    Vue2 响应式系统之数组

    这篇文章主要介绍了Vue2 响应式系统之数组,本文接着上几篇文章Vue2响应式系统 、Vue2 响应式系统之分支切换,响应式系统之嵌套、响应式系统之深度响应 展开相关内容,需要的朋友可以参考一下
    2022-04-04
  • vue项目初始化过程中错误总结

    vue项目初始化过程中错误总结

    在Vue.js项目初始化和构建过程中,可能会遇到多种问题,首先,npm install过程中报错,如提示“No such file or directory”,建议删除package-lock.json文件后重新安装,在build或run时,若出现core-js相关错误
    2024-09-09
  • 使用Vue实现移动端左滑删除效果附源码

    使用Vue实现移动端左滑删除效果附源码

    这篇文章主要介绍了使用Vue实现移动端左滑删除效果,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • 使用vue-cli4.0快速搭建一个项目的方法步骤

    使用vue-cli4.0快速搭建一个项目的方法步骤

    这篇文章主要介绍了使用vue-cli4.0快速搭建一个项目的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • 详解vue中v-model和v-bind绑定数据的异同

    详解vue中v-model和v-bind绑定数据的异同

    这篇文章主要介绍了vue中v-model和v-bind绑定数据的异同,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • 浅析vue-router原理

    浅析vue-router原理

    这篇文章主要围绕Vue的SPA单页面设计展开。SPA(single page application):单一页面应用程序,有且只有一个完整的页面,对vue router原理感兴趣的朋友跟随小编一起看看吧
    2018-10-10
  • vue router 跳转时打开新页面的示例方法

    vue router 跳转时打开新页面的示例方法

    这篇文章主要介绍了vue router 跳转时打开新页面的示例方法,本文通过示例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-07-07
  • 浅析vue中的组件传值

    浅析vue中的组件传值

    这篇文章主要介绍了浅析vue中的组件传值,文章基于vue的相关资料展开对主题的详细介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-04-04

最新评论