index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div>
  3. <el-form-item :rules="rules" label="*会所地址:" :label-width="'130px'" style="width: 640px;text-align: right;">
  4. <el-select
  5. v-model="sheng"
  6. placeholder="省级地区"
  7. style="width:167px;"
  8. @change="choseProvince"
  9. >
  10. <el-option
  11. v-for="item in province"
  12. :key="item.id"
  13. :label="item.value"
  14. :value="item.id"
  15. />
  16. </el-select>
  17. <el-select
  18. v-model="shi"
  19. placeholder="市级地区"
  20. style="width:167px;"
  21. @change="choseCity"
  22. >
  23. <el-option
  24. v-for="item in shi1"
  25. :key="item.id"
  26. :label="item.value"
  27. :value="item.id"
  28. />
  29. </el-select>
  30. <el-select
  31. v-model="qu"
  32. placeholder="区级地区"
  33. style="width:167px;"
  34. @change="choseBlock"
  35. >
  36. <el-option
  37. v-for="item in qu1"
  38. :key="item.id"
  39. :label="item.value"
  40. :value="item.id"
  41. />
  42. </el-select>
  43. </el-form-item>
  44. </div>
  45. </template>
  46. <script>
  47. import axios from 'axios'
  48. export default {
  49. name: 'Ctiy',
  50. data() {
  51. return {
  52. mapJson: 'http://localhost:9528/map.json',
  53. province: '',
  54. sheng: '',
  55. shi: '',
  56. shi1: [],
  57. qu: '',
  58. qu1: [],
  59. city: '',
  60. block: '',
  61. rules: {
  62. sheng: [{ required: true, message: '请选择省份', trigger: 'blur' }],
  63. shi: [{ required: true, message: '请选择市', trigger: 'blur' }],
  64. qu: [{ required: true, message: '请选择区县', trigger: 'blur' }]
  65. }
  66. }
  67. },
  68. created: function() {
  69. this.getCityData()
  70. },
  71. methods: {
  72. // 加载china地点数据,三级
  73. getCityData: function() {
  74. var that = this
  75. axios.get(this.mapJson).then(function(response) {
  76. if (response.status == 200) {
  77. var data = response.data
  78. that.province = []
  79. that.city = []
  80. that.block = []
  81. // 省市区数据分类
  82. for (var item in data) {
  83. if (item.match(/0000$/)) { // 省
  84. that.province.push({ id: item, value: data[item], children: [] })
  85. } else if (item.match(/00$/)) { // 市
  86. that.city.push({ id: item, value: data[item], children: [] })
  87. } else { // 区
  88. that.block.push({ id: item, value: data[item] })
  89. }
  90. }
  91. // 分类市级
  92. for (var index in that.province) {
  93. for (var index1 in that.city) {
  94. if (that.province[index].id.slice(0, 2) === that.city[index1].id.slice(0, 2)) {
  95. that.province[index].children.push(that.city[index1])
  96. }
  97. }
  98. }
  99. // 分类区级
  100. for (var item1 in that.city) {
  101. for (var item2 in that.block) {
  102. if (that.block[item2].id.slice(0, 4) === that.city[item1].id.slice(0, 4)) {
  103. that.city[item1].children.push(that.block[item2])
  104. }
  105. }
  106. }
  107. } else {
  108. console.log(response.status)
  109. }
  110. }).catch(function(error) { console.log(typeof +error) })
  111. },
  112. // 选省
  113. choseProvince: function(e) {
  114. for (var index2 in this.province) {
  115. if (e === this.province[index2].id) {
  116. this.shi1 = this.province[index2].children
  117. this.shi = this.province[index2].children[0].value
  118. this.qu1 = this.province[index2].children[0].children
  119. this.qu = this.province[index2].children[0].children[0].value
  120. this.E = this.qu1[0].id
  121. }
  122. }
  123. },
  124. // 选市
  125. choseCity: function(e) {
  126. for (var index3 in this.city) {
  127. if (e === this.city[index3].id) {
  128. this.qu1 = this.city[index3].children
  129. this.qu = this.city[index3].children[0].value
  130. this.E = this.qu1[0].id
  131. // console.log(this.E)
  132. }
  133. }
  134. },
  135. // 选区
  136. choseBlock: function(e) {
  137. this.E = e
  138. // console.log(this.E)
  139. }
  140. }
  141. }
  142. </script>
  143. <style scoped>
  144. </style>