WXL
2025-03-20 838d8b59fa42479a8afb3fea20eea7e5f3071162
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<template>
  <div style="display: flex">
    <el-select
      v-model="caddress.sheng"
      style="flex: 1"
      placeholder="请选择省份"
      size="small"
      @change="getCityData"
    >
      <el-option
        v-for="item in addressArray"
        :key="item.areacode"
        :label="item.areaname"
        :value="item.areaname"
      >
      </el-option>
    </el-select>
    <el-select
      v-model="caddress.shi"
      style="flex: 1; margin-left: 10px"
      placeholder="请选择市区"
      size="small"
      @change="getAreaData"
    >
      <el-option
        v-for="item in cityArray"
        :key="item.areacode"
        :label="item.areaname"
        :value="item.areaname"
      >
      </el-option>
    </el-select>
    <el-select
      v-model="caddress.qu"
      style="flex: 1; margin-left: 10px"
      placeholder="请选择县"
      size="small"
      @change="onAreaChanged"
    >
      <el-option
        v-for="item in areaArray"
        :key="item.areacode"
        :label="item.areaname"
        :value="item.areaname"
      >
      </el-option>
    </el-select>
  </div>
</template>
 
<script>
// 使用说明:v-model时,必须传带有带有省,市,区拼音的字段
import request from '@/utils/request'
export default {
  name: "li_area_select",
  //通过 model 选项配置子组件接收的 prop 名以及派发的事件名
  model: {
    prop: "caddress",
    event: "change",
  },
  props: {
    caddress: {
      type: Object,
    },
  },
  data() {
    return {
      areaJson: "/project/dict/treeselect", // 上传的图片服务器地址
      //areaJson: './../address.json',
      addressArray: [], //所有数据
      cityArray: [],
      areaArray: [],
    };
  },
  created() {
    this.getAddressData();
  },
  methods: {
    getAddressData() {
      var that = this;
      request({
        url: that.areaJson,
        method: "get",
      }).then(function (response) {
        if (response.code === 200) {
          //获取地址
 
          that.addressArray = response.data;
          //默认值赋值获取城市数组
          if (that.caddress.sheng) {
            for (let ad of that.addressArray) {
              if (ad.areaname === that.caddress.sheng) {
                that.cityArray = ad.subarea;
                //---
                //默认赋值获取区域数组
                if (that.caddress.shi) {
                  for (let area of that.cityArray) {
                    if (area.areaname === that.caddress.shi) {
                      that.areaArray = area.subarea;
                      break;
                    }
                  }
                }
              }
            }
          }
        }
      });
    },
    //选择省份
    getCityData(val) {
      //清空市,和区
      this.caddress.shi = "";
      this.caddress.qu = "";
      this.$emit("change", this.caddress); //发送改变
      for (let ad of this.addressArray) {
        if (ad.areaname === val) {
          this.cityArray = ad.subarea;
          return;
        }
      }
    },
    getAreaData(val) {
      //清空区
      this.caddress.qu = "";
      this.$emit("change", this.caddress); //发送改变
      for (let area of this.cityArray) {
        if (area.areaname === val) {
          this.areaArray = area.subarea;
          return;
        }
      }
    },
    //地区数据变动后
    onAreaChanged(val) {
      this.$emit("change", this.caddress); //发送改变
      this.$forceUpdate();
    },
 
    getSheng(){
      let list= this.addressArray.filter(r=>r.areaname ==  this.caddress.sheng);
      if(list.length>0){
        return list[0].areacode;
      }
      else{
        return '';
      }
    },
     getShi(){
      let list= this.cityArray.filter(r=>r.areaname ==  this.caddress.shi);
      if(list.length>0){
        return list[0].areacode;
      }
      else{
        return '';
      }
    },
     getQu(){
      let list= this.areaArray.filter(r=>r.areaname ==  this.caddress.qu);
      if(list.length>0){
        return list[0].areacode;
      }
      else{
        return '';
      } 
    },
    clean(){
      this.caddress.sheng="";
      this.caddress.shi="";
      this.caddress.qu="";
    }
  },
};
</script>
 
<style scoped>
</style>