小言_互联网的博客

解决elementui 的省市区级联选择器数据不回显问题

495人阅读  评论(0)

     上周写了一个省市区三级联动的地址选择组件,今天测试发现了一个大问题,那就是我可以正常提交地址是没错,可是当我后端返回了数据,我要点击编辑的时候,它并不会自动就给我绑定上去。

vue实现省市区三级联动地址选择组件http://t.csdn.cn/lzuLM- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -  - - - - - - - - - - - -

    问题:地址选择器数据不回显。

    解决:用一个props,每从外面接收一个address(province,city,area),让它三个子项对应好三级,一个个绑上去就好啦!~

最终的写法:

一、 封装vue组件


  
  1. <!-- 地址选择 & 省市区三级联动 -->
  2. <template>
  3. <el-form ref="form" :model="form" label-width="120px">
  4. <el-row>
  5. <el-select
  6. v-model= "form.province"
  7. placeholder= "请选择省"
  8. @ change= "changePro"
  9. >
  10. <el-option
  11. v-for= "item in addressData"
  12. :key= "item.code"
  13. :label= "item.name"
  14. :value= "item.name"
  15. >
  16. </el-option>
  17. </el-select>
  18. <el-select
  19. v-model= "form.city"
  20. placeholder= "请选择市"
  21. @ change= "changeCity"
  22. >
  23. <el-option
  24. v-for= "item in cityData"
  25. :key= "item.code"
  26. :label= "item.name"
  27. :value= "item.name"
  28. >
  29. </el-option>
  30. </el-select>
  31. <el-select
  32. v-model= "form.district"
  33. placeholder= "请选择区"
  34. @ change= "changeArea"
  35. >
  36. <el-option
  37. v-for= "item in areaData"
  38. :key= "item.code"
  39. :label= "item.name"
  40. :value= "item.name"
  41. >
  42. </el-option>
  43. </el-select>
  44. <el-input placeholder="详细地址" v-model="form.detail"> </el-input>
  45. </el-row>
  46. <!-- <h3>测试当前选中地址:{{result}}</h3> -->
  47. </el-form>
  48. </template>
  49. <script>
  50. import address from "@/utils/address/address.json"; //全国省市区街道数据
  51. export default {
  52. data( ) {
  53. return {
  54. // 省数据
  55. addressData: [],
  56. // 市数据
  57. cityData: [],
  58. // 区数据
  59. areaData: [],
  60. };
  61. },
  62. props:{
  63. // 街道数据
  64. form:{
  65. province: "",
  66. // 市
  67. city: "",
  68. // 区
  69. district: "",
  70. //详细地址
  71. detail: ""
  72. }
  73. },
  74. created( ) {
  75. // 省份数据初始化
  76. this. addressData = address;
  77. console. log( "this.form", this. form)
  78. },
  79. computed:{
  80. //选择结果
  81. result( ){
  82. if(! this. form. district){
  83. let val= ''
  84. return ''
  85. } else if( this. form. district && this. form. detail){
  86. let val= ''+ this. form. province+ ","+ this. form. city+ ","+ this. form. district+ ","+ this. form. detail
  87. return ''+ this. form. province+ ","+ this. form. city+ ","+ this. form. district+ ","+ this. form. detail
  88. } else{
  89. let val= ''+ this. form. province+ ","+ this. form. city+ ","+ this. form. district
  90. return ''+ this. form. province+ ","+ this. form. city+ ","+ this. form. district
  91. }
  92. }
  93. },
  94. methods: {
  95. reset( ){
  96. this. form= {
  97. // 省
  98. province: "",
  99. // 市
  100. city: "",
  101. // 区
  102. district: "",
  103. //详细地址
  104. detail: "",
  105. }
  106. },
  107. // 省份更改
  108. changePro( e) {
  109. // 从省中过滤出市的数据
  110. this. cityData = this. addressData. filter( (item) => {
  111. return item. name == e;
  112. })[ 0]. children;
  113. // 省发生改变的时候 清空输入框市区街道的内容
  114. this. form. district = "";
  115. this. form. city = "";
  116. // 省发生更改时 该表空区街道数据的内容
  117. this. areaData = [];
  118. },
  119. // 市更改
  120. changeCity( e) {
  121. // 获取到区的数据
  122. this. areaData = this. cityData. filter(
  123. (item) => item. name == e
  124. )[ 0]. children;
  125. // 清空数据后面对应数组的数据
  126. this. form. district = "";
  127. },
  128. // 区更改
  129. changeArea( e) {
  130. let temp = this. areaData. filter( (item) => item. name == e);
  131. // 获取到区的code码
  132. this. form. regionalNumber = temp[ 0]. code;
  133. // 获取到街道的数据
  134. this. jdData = this. areaData. filter( (item) => item. name == e)[ 0]. children;
  135. },
  136. },
  137. };
  138. </script>
  139. <style lang="scss" rel="stylesheet/scss" scoped>
  140. .el-row{
  141. display: inline;
  142. }
  143. .el-row{
  144. display: inline-flex;
  145. flex: auto;
  146. .el-select {
  147. margin: 0 20px 0 0;
  148. }
  149. }
  150. </style>

二、在页面中引用 


  
  1. <el-row>
  2. <el-col>
  3. <el-form-item label="联系地址" prop="address" v-model="ruleForm.address">
  4. <checkAddress v-model="ruleForm.address" :form="ruleForm.address" ref="address"/>
  5. </el-form-item>
  6. </el-col>
  7. </el-row>

  
  1. <el-table-column prop="address" label="联系地址">
  2. <template slot-scope="scope">
  3. <span v-for="(item,index) in scope.row.address" :key="index" :label="item">
  4. <span>
  5. {{item}}
  6. </span>
  7. </span>
  8. </template>
  9. </el-table-column>

  
  1. address: {
  2. // 省
  3. province: "",
  4. // 市
  5. city: "",
  6. // 区
  7. district: "",
  8. //详细地址
  9. detail: ""
  10. }, //联系地址

  
  1. watch:{
  2. ruleForm:{
  3. // deep:true,
  4. handler( ){
  5. this. ruleForm. address= this. $refs. address. form
  6. console. log( this. ruleForm. address)
  7. }
  8. }

  
  1. created( ){
  2. if( this. rowData. contactType== "ADDRESS"){
  3. this. ruleForm. address. province= this. rowData. address. province
  4. this. ruleForm. address. city= this. rowData. address. city
  5. this. ruleForm. address. district= this. rowData. address. district
  6. this. ruleForm. address. detail= this. rowData. address. detail
  7. }
  8. }

 三、去网上找个Address.json文件放进去就好了

就这样愉快地解决问题啦!从此点击编辑,相对应的地址数据会回显。

四、如果是多个填写项,需要时让他们一一对应

那么使用代码也需要进行改进


  
  1. <el-row>
  2. <el-col>
  3. <el-form-item label="联系地址" prop="address" v-model="item.address">
  4. <!-- <el-input v-model="item.contactTypeFlex3"></el-input> -->
  5. <checkAddress ref="address" v-model="item.address" :key="index" :form="item.address"/>
  6. </el-form-item>
  7. </el-col>
  8. </el-row>

  
  1. address: {
  2. // 省
  3. province: "",
  4. // 市
  5. city: "",
  6. // 区
  7. district: "",
  8. //详细地址
  9. detail: ""
  10. }, //联系地址

  
  1. if(item. contactType== "ADDRESS"){
  2. current. address. province=item. contactTypeFlex1
  3. current. address. city=item. contactTypeFlex2
  4. current. address. district=item. contactTypeFlex3
  5. current. address. detail=item. contactTypeFlex34
  6. }
  7. this. tempList. push(current)

转载:https://blog.csdn.net/Vivien_CC/article/details/127798566
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场