ProvinceCity.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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">
  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="请选择">
  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. },
  43. created() {
  44. this.provinces = provinceCity.provinces;
  45. },
  46. mounted() {
  47. this.province = this.provinceCode;
  48. this.provinceChanged(this.provinceCode);
  49. this.city = this.cityCode;
  50. },
  51. data() {
  52. return {
  53. loadingCity: false,
  54. province: "",
  55. city: "",
  56. provinces: [],
  57. cities: []
  58. };
  59. },
  60. methods: {
  61. provinceChanged(value) {
  62. if (value !== "") {
  63. this.loadingCity = true;
  64. for (var item of this.provinces) {
  65. if (item.code === value) {
  66. this.cities = item.cities;
  67. this.city = "";
  68. this.loadingCity = false;
  69. break;
  70. } else {
  71. continue;
  72. }
  73. }
  74. } else {
  75. this.cities = [];
  76. this.city = "";
  77. }
  78. this.$emit("selectChange", this.province, this.city);
  79. },
  80. cityChanged(value) {
  81. this.$emit("selectChange", this.province, this.city);
  82. },
  83. resetProviceCity(province, city) {
  84. this.cities = [];
  85. if (province && city) {
  86. this.province = province;
  87. this.provinceChanged(this.province);
  88. this.city = city;
  89. } else {
  90. this.province = "";
  91. this.city = "";
  92. }
  93. }
  94. },
  95. watch:{
  96. data(){
  97. this.province = this.provinceCode
  98. this.city = this.cityCode
  99. }
  100. }
  101. };
  102. </script>
  103. <style lang="less" scoped>
  104. .page-form-item {
  105. display: inline-block;
  106. }
  107. </style>