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


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

    http://t.csdn.cn/qVLHEicon-default.png?t=N176http://t.csdn.cn/qVLHE- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -  - - - - - - - - - - - -

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

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

    最终的写法:

    一、 封装vue组件

    1. <template>
    2. <el-form ref="form" :model="form" label-width="120px">
    3. <el-row>
    4. <el-select v-model="form.province" placeholder="请选择省" @change="changePro" >
    5. <el-option v-for="item in addressData" :key="item.code" :label="item.name" :value="item.name">
    6. el-option>
    7. el-select>
    8. <el-select v-model="form.city" placeholder="请选择市" @change="changeCity">
    9. <el-option v-for="item in cityData" :key="item.code" :label="item.name" :value="item.name">
    10. el-option>
    11. el-select>
    12. <el-select v-model="form.district" placeholder="请选择区" @change="changeArea">
    13. <el-option v-for="item in areaData" :key="item.code" :label="item.name" :value="item.name">
    14. el-option>
    15. el-select>
    16. <el-input placeholder="详细地址" v-model="form.detail" style="width:200px;" clearable>el-input>
    17. el-row>
    18. el-form>
    19. template>
    20. <script>
    21. import address from "@/utils/address/address.json"; //全国省市区街道数据
    22. export default {
    23. data() {
    24. return {
    25. // 省数据
    26. addressData: [],
    27. // 市数据
    28. cityData: [],
    29. // 区数据
    30. areaData: [],
    31. };
    32. },
    33. props: {
    34. // 街道数据
    35. form: {
    36. province: "",
    37. // 市
    38. city: "",
    39. // 区
    40. district: "",
    41. //详细地址
    42. detail: "",
    43. }
    44. },
    45. created() {
    46. // 省份数据初始化
    47. this.addressData = address;
    48. // console.log("this.form",this.form)
    49. if (this.form.province != "") {
    50. let cityData = this.addressData.filter(item => item.name == this.form.province)
    51. if (cityData.length) {
    52. this.cityData = cityData[0].children
    53. let areaData = this.cityData.filter(item => item.name == this.form.city)
    54. if (areaData.length) {
    55. this.areaData = areaData[0].children
    56. }
    57. }
    58. }
    59. },
    60. computed: {
    61. //选择结果
    62. result() {
    63. if (!this.form.district) {
    64. return ''
    65. } else if (this.form.district && this.form.detail) {
    66. return '' + this.form.province + "," + this.form.city + "," + this.form.district + "," + this.form.detail
    67. } else {
    68. return '' + this.form.province + "," + this.form.city + "," + this.form.district
    69. }
    70. }
    71. },
    72. methods: {
    73. reset() {
    74. this.form = {
    75. // 省
    76. province: "",
    77. // 市
    78. city: "",
    79. // 区
    80. district: "",
    81. //详细地址
    82. detail: "",
    83. }
    84. },
    85. // 省份更改
    86. changePro(e) {
    87. // 从省中过滤出市的数据
    88. this.cityData = this.addressData.filter((item) => {
    89. return item.name == e;
    90. })[0].children;
    91. // 省发生改变的时候 清空输入框市区街道的内容
    92. this.form.district = "";
    93. this.form.city = this.cityData[0].name;
    94. // 省发生更改时 该表空区街道数据的内容
    95. this.areaData = this.cityData[0].children;
    96. this.form.district = this.areaData[0].name
    97. },
    98. // 市更改
    99. changeCity(e) {
    100. // 获取到区的数据
    101. this.areaData = this.cityData.filter(
    102. (item) => item.name == e
    103. )[0].children;
    104. // 清空数据后面对应数组的数据
    105. this.form.district = this.areaData[0].name;
    106. },
    107. // 区更改
    108. changeArea(e) {
    109. let temp = this.areaData.filter((item) => item.name == e);
    110. // 获取到区的code码
    111. this.form.regionalNumber = temp[0].code;
    112. // 获取到街道的数据
    113. this.jdData = this.areaData.filter((item) => item.name == e)[0].children;
    114. },
    115. },
    116. };
    117. script>
    118. <style lang="scss" rel="stylesheet/scss" scoped>
    119. .el-row {
    120. display: inline;
    121. }
    122. .el-row {
    123. display: inline-flex;
    124. flex: auto;
    125. .el-select {
    126. margin: 0 20px 0 0;
    127. width: 122px;
    128. }
    129. }
    130. 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. <checkAddress ref="address" v-model="item.address" :key="index" :form="item.address"/>
    5. el-form-item>
    6. el-col>
    7. 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)
  • 相关阅读:
    python入门I--基本概念--基本语法--变量和标识符--数据类型
    正向代理的反爬虫与防DDoS攻击:保护网站免受恶意行为
    PCL (一)点云的格式
    AutoGPT:自动化GPT原理及应用实践
    C 语言左移位操作在kernel驱动子系统中的特殊用途
    工作中总结的30个常用Linux指令,实在记不住就别硬记了,看这篇就够了
    初次邂逅 EasyExcel
    文件权限详解
    错误未找到concrt140.dll最详细的解决方法与修复教程
    HFI-脉振法
  • 原文地址:https://blog.csdn.net/Vivien_CC/article/details/127798566