ProvinceCity.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <div class="province-city">
  3. <el-form-item
  4. label-width="25%"
  5. class="page-form-item"
  6. label="所属省:"
  7. prop="status"
  8. style="width: calc(50% - 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(50% - 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.province = this.provinceCode;
  52. this.provinceChanged(this.provinceCode);
  53. this.city = this.cityCode;
  54. },
  55. data() {
  56. return {
  57. loadingCity: false,
  58. province: "",
  59. city: "",
  60. provinces: [],
  61. cities: []
  62. };
  63. },
  64. methods: {
  65. provinceChanged(value) {
  66. if (value !== "") {
  67. this.loadingCity = true;
  68. for (var item of this.provinces) {
  69. if (item.code === value) {
  70. this.cities = item.cities;
  71. this.city = "";
  72. this.loadingCity = false;
  73. break;
  74. } else {
  75. continue;
  76. }
  77. }
  78. } else {
  79. this.cities = [];
  80. this.city = "";
  81. }
  82. this.$emit("selectChange", this.province, this.city);
  83. },
  84. cityChanged(value) {
  85. this.$emit("selectChange", this.province, this.city);
  86. },
  87. resetProviceCity(province, city) {
  88. this.cities = [];
  89. if (province && city) {
  90. this.province = province;
  91. this.provinceChanged(this.province);
  92. this.city = city;
  93. } else {
  94. this.province = "";
  95. this.city = "";
  96. }
  97. }
  98. },
  99. watch:{
  100. data(){
  101. this.province = this.provinceCode
  102. this.city = this.cityCode
  103. },
  104. },
  105. };
  106. </script>
  107. <style lang="less" scoped>
  108. .page-form-item {
  109. display: inline-block;
  110. }
  111. </style>