码迷,mamicode.com
首页 > 其他好文 > 详细

Vue中h5三个下拉框实现省级联动

时间:2020-04-28 16:46:03      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:断点   disable   省市区   图片   ext   yar   duration   下拉框   label   

使用element-UI插件:(我这里是后台返回的省市区数据,不包含海外)

首先看页面效果:

技术图片

<template>
  <div class="allBox">
   <div ref="main">
    <p class="title">请选择您所在的区域</p>
       <el-select v-model="province_id" placeholder="请选择省" class="ipt" @change="getCityList(province_id)">
                <el-option
                  v-for="item in provincOptions"
                  :key="item.id"
                  :label="item.area_name"
                  :value="item.id">
                </el-option>
        </el-select>
        <el-select v-model="city_id" placeholder="请选择市" class="ipt" @change="getCountyList(city_id)">
                <el-option
                  v-for="item in cityOptions"
                  :key="item.id"
                  :label="item.area_name"
                  :value="item.id">
                </el-option>
        </el-select>
        <el-select v-model="county_id" placeholder="请选择区" class="ipt">
                <el-option
                  v-for="item in countyOptions"
                  :key="item.id"
                  :label="item.area_name"
                  :value="item.id">
                </el-option>
        </el-select>
   </div>
    <van-button class="nextStepBtn" :disabled="isDisabledSubmitBtn" @click="submitArea">下一步</van-button>
  </div>
</template>

<script>
  import {
    getCity,
    queryArea,
    editArea
  } from "../industry"; // 引入接口(注意文件路径)
  export default {
    data() {
      return {
        isDisabledSubmitBtn:false,
        province_id: ‘‘,
        province_name: ‘‘,
        city_id: ‘‘,
        city_name: ‘‘,
        county_id: ‘‘,
        county_name: ‘‘,
        provincOptions:[],
        cityOptions:[],
        countyOptions:[],
        selectArea:null
      };
    },
    created() {
      this.getArea()
    },
    methods: {
    // 获取省市区(数据回显)
    getArea() {
      let params={
        unionid:this.unionid
      }
      queryArea(params).then(res => {
        this.selectArea = res.data;
        this.getProvinceList(0)
        this.province_id = this.selectArea.provinceId
        this.province_name = this.selectArea.provinceName
        this.getCityList(this.province_id)
        this.city_id=this.selectArea.cityId
        this.city_name=this.selectArea.cityName
        this.getCountyList(this.city_id)
        this.county_id=this.selectArea.countyId
        this.county_name=this.selectArea.countyName
      })
    },
    //提交前的数据校验
    valid() {
      let that=this
      if (!that.province_id) {
        that.$toast({
          message: 请选择省,
          duration: 2000
        });
        return false;
      }
       if (!that.city_id) {
        that.$toast({
          message: 请选择市,
          duration: 2000
        });
        return false;
      }
       if (!that.county_id) {
        that.$toast({
          message: 请选择区,
          duration: 2000
        });
        return false;
      }
      return true;
    },
    // 获取省列表
    getProvinceList() {
      let params={
        parentId:0
      }
      getCity(params).then(res => {
        this.provincOptions = res.data
      })
    },
    // 获取市列表
    getCityList(province_id) {
      this.city_id=‘‘
      this.county_id=‘‘
      this.countyOptions=[]
      let params={
        parentId:this.province_id
      }
      if (this.province_id) {
        getCity(params).then(res => {
          this.cityOptions = res.data
        })
      } else {
        this.cityOptions = [] // 每一次选择新的省之后清空,防止错乱
      }
    },
    // 获取区列表
    getCountyList(city_id) {
      let params={
        parentId:this.city_id
      }
       this.county_id = ‘‘
        if (this.city_id) {
          getCity(params).then(res => {
            this.countyOptions = res.data
          })
        } else {
          this.countyOptions = [] // 每一次选择新的省之后清空,防止错乱
        }
    },
  // 提交数据
    submitArea(){
      if(this.valid()){
        this.isDisabledSubmitBtn=true // 按钮置灰,防止用户不断点击
     // 循环得到省市区名字
        for(var i = 0;i<this.provincOptions.length;i++){
          if(this.province_id == this.provincOptions[i].id){
            this.province_name = this.provincOptions[i].area_name
          }
        }
        for(var i = 0;i<this.cityOptions.length;i++){
          if(this.city_id == this.cityOptions[i].id){
            this.city_name = this.cityOptions[i].area_name
          }
        }
        for(var i = 0;i<this.countyOptions.length;i++){
          if(this.county_id == this.countyOptions[i].id){
            this.county_name = this.countyOptions[i].area_name
          }
        }
        let params={
          provinceId:this.province_id,
          provinceName:this.province_name,
          cityId:this.city_id,
          cityName:this.city_name,
          countyId:this.county_id,
          countyName:this.county_name // 传入后台所需要的参数
        }
        editArea(params).then(res=>{
          if(res.code==1){
            // 提交成功后的操作
          }else{
            this.isDisabledSubmitBtn=false // 按钮恢复高亮
            this.$toast({
              message: res.message,
              duration: 2000
            });
          }
        })
      }
    }
    }
  };
</script>
<style>
.el-scrollbar__bar {
  opacity: 1 !important;
}
</style>
// 样式仅供参考
<style scoped> .allBox { width: 100%; height: 100%; background-size: 100% 100%; min-height: 100%; background-attachment: fixed; overflow-y: auto;   background-color: #097Caa; } .main { width: 100%; } .title { font-size: 20px; font-weight: 450; margin: 30px auto 50px; text-align: center; color: #fff; } .ipt { border-radius: 5px; width: 70%; margin-top: 10px; } .nextStepBtn { width: 70%; height: 44px; border-radius: 4px; margin: 60px auto 20px; color: #fff; font-size: 16px; border: 0; background-color: #11415F; } </style>

 

接口industry.js文件:

//查询省市区
export const getCity = (params={}) => {
  return request({
    url:后台接口,
    method: post,
    params: params
  })
}
//获取省市区
export const queryArea = (params={}) => {
  return request({
    url:后台接口‘,
method: post, params: params }) } //提交省市区 export const editArea = (params={}) => { return request({ url:后台接口‘,
method: post, params: params }) }

 

 

Vue中h5三个下拉框实现省级联动

标签:断点   disable   省市区   图片   ext   yar   duration   下拉框   label   

原文地址:https://www.cnblogs.com/xiaofang234/p/12795005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!