ProvinceCity.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div class="province-city">
  3. <el-form-item
  4. label-width="20%"
  5. class="page-form-item"
  6. label="省:"
  7. prop="status"
  8. style="width: calc(20% - 20px)"
  9. >
  10. <el-select v-model="province" placeholder="请选择" @change="provinceChanged" :disabled="isModifyMode">
  11. <!-- <el-option :key="''" :label="'全部'" :value="''"></el-option>-->
  12. <el-option v-for="item in provinces" :key="item.code" :label="item.name" :value="item.code"></el-option>
  13. </el-select>
  14. </el-form-item>
  15. <el-form-item
  16. label-width="20%"
  17. class="page-form-item"
  18. label="市:"
  19. prop="status"
  20. style="width: calc(30% - 20px);margin-right: 0px"
  21. >
  22. <el-select v-model="city" :loading="loadingCity" @change="cityChanged" placeholder="请选择" :disabled="isModifyMode">
  23. <el-option :key="''" :label="'请选择'" :value="''"></el-option>
  24. <el-option v-for="item in cities" :key="item.code" :label="item.name" :value="item.code"></el-option>
  25. </el-select>
  26. </el-form-item>
  27. </div>
  28. </template>
  29. <script type="text/javascript">
  30. import provinceCity from "@/constants/provinceCity.json";
  31. export default {
  32. name: "provinceCity",
  33. props: {
  34. provinceCode: {
  35. type: String,
  36. default: ""
  37. },
  38. cityCode: {
  39. type: String,
  40. default: ""
  41. },
  42. isModifyMode:{
  43. type:Boolean,
  44. default:false
  45. }
  46. },
  47. created() {
  48. this.provinces = provinceCity.provinces;
  49. },
  50. mounted() {
  51. this.$nextTick(()=>{
  52. this.province = this.provinceCode;
  53. this.provinceChanged(this.provinceCode);
  54. this.city = this.cityCode;
  55. console.log(this.city)
  56. console.log(this.province)
  57. console.log(this.provinceCode)
  58. console.log(this.cityCode)
  59. })
  60. },
  61. data() {
  62. return {
  63. loadingCity: false,
  64. province: "",
  65. city: "",
  66. provinces: [],
  67. cities: []
  68. };
  69. },
  70. methods: {
  71. provinceChanged(value) {
  72. if (value !== "") {
  73. this.loadingCity = true;
  74. for (var item of this.provinces) {
  75. if (item.code === value) {
  76. this.cities = item.cities;
  77. this.city = "";
  78. this.loadingCity = false;
  79. break;
  80. } else {
  81. continue;
  82. }
  83. }
  84. } else {
  85. this.cities = [];
  86. this.city = "";
  87. }
  88. this.$emit("selectChange", this.province, this.city);
  89. },
  90. cityChanged(value) {
  91. this.$emit("selectChange", this.province, this.city);
  92. },
  93. resetProviceCity(province, city) {
  94. this.cities = [];
  95. if (province && city) {
  96. this.province = province;
  97. this.provinceChanged(this.province);
  98. this.city = city;
  99. } else {
  100. this.province = "";
  101. this.city = "";
  102. }
  103. }
  104. },
  105. watch:{
  106. data(){
  107. this.province = this.provinceCode
  108. this.city = this.cityCode
  109. },
  110. // provinceCode(val){
  111. // console.log("pro code change in pro city com",val)
  112. // this.provinceChanged(val)
  113. // },
  114. },
  115. };
  116. </script>
  117. <style lang="less" scoped>
  118. .page-form-item {
  119. display: inline-block;
  120. }
  121. </style>