ProvinceCity.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. })
  56. },
  57. data() {
  58. return {
  59. loadingCity: false,
  60. province: "",
  61. city: "",
  62. provinces: [],
  63. cities: []
  64. };
  65. },
  66. methods: {
  67. provinceChanged(value) {
  68. if (value !== "") {
  69. this.loadingCity = true;
  70. for (var item of this.provinces) {
  71. if (item.code === value) {
  72. this.cities = item.cities;
  73. this.city = "";
  74. this.loadingCity = false;
  75. break;
  76. } else {
  77. continue;
  78. }
  79. }
  80. } else {
  81. this.cities = [];
  82. this.city = "";
  83. }
  84. this.$emit("selectChange", this.province, this.city);
  85. },
  86. cityChanged(value) {
  87. this.$emit("selectChange", this.province, this.city);
  88. },
  89. resetProviceCity(province, city) {
  90. this.cities = [];
  91. if (province && city) {
  92. this.province = province;
  93. this.provinceChanged(this.province);
  94. this.city = city;
  95. } else {
  96. this.province = "";
  97. this.city = "";
  98. }
  99. }
  100. },
  101. watch:{
  102. data(){
  103. this.province = this.provinceCode
  104. this.city = this.cityCode
  105. },
  106. // provinceCode(val){
  107. // console.log("pro code change in pro city com",val)
  108. // this.provinceChanged(val)
  109. // },
  110. },
  111. };
  112. </script>
  113. <style lang="scss" scoped>
  114. .page-form-item {
  115. display: inline-block;
  116. }
  117. </style>