index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <div class="select-group">
  3. <div class="select-group__item" @click.stop="toggleMenu('province')">
  4. <div class="info select-click-type">
  5. <span>{{ province ? province.name : '省份' }}</span>
  6. <span
  7. class="el-icon-circle-close"
  8. v-if="province"
  9. @click.stop="onClear('province', $event)"
  10. ></span>
  11. <span class="icon el-icon-caret-bottom" v-else></span>
  12. </div>
  13. <el-select
  14. ref="provinceSelect"
  15. v-model="provinceId"
  16. @change="onProvinceChange"
  17. :clearable="true"
  18. >
  19. <template v-for="item in provinceList">
  20. <el-option :label="item.name" :value="item.id" :key="item.id">
  21. </el-option>
  22. </template>
  23. </el-select>
  24. </div>
  25. <div class="select-group__item" @click.stop="toggleMenu('city')">
  26. <div class="info select-click-type">
  27. <span>{{ city ? city.name : '城市' }}</span>
  28. <span
  29. class="el-icon-circle-close"
  30. v-if="city"
  31. @click.stop="onClear('city', $event)"
  32. ></span>
  33. <span class="icon el-icon-caret-bottom" v-else></span>
  34. </div>
  35. <el-select
  36. ref="citySelect"
  37. v-model="cityId"
  38. @change="onCityChange"
  39. :clearable="true"
  40. >
  41. <template v-for="item in cityList">
  42. <el-option :label="item.name" :value="item.id" :key="item.id">
  43. </el-option>
  44. </template>
  45. </el-select>
  46. </div>
  47. <div class="select-group__item" @click.stop="toggleMenu('town')">
  48. <div class="info select-click-type">
  49. <span>{{ town ? town.name : '县区' }}</span>
  50. <span
  51. class="el-icon-circle-close"
  52. v-if="town"
  53. @click.stop="onClear('town', $event)"
  54. ></span>
  55. <span class="icon el-icon-caret-bottom" v-else></span>
  56. </div>
  57. <el-select
  58. ref="townSelect"
  59. v-model="townId"
  60. @change="onTownChange"
  61. :clearable="true"
  62. >
  63. <template v-for="item in townList">
  64. <el-option :label="item.name" :value="item.id" :key="item.id">
  65. </el-option>
  66. </template>
  67. </el-select>
  68. </div>
  69. </div>
  70. </template>
  71. <script>
  72. export default {
  73. data() {
  74. return {
  75. provinceId: '',
  76. cityId: '',
  77. townId: '',
  78. provinceList: [],
  79. province: null,
  80. city: null,
  81. town: null,
  82. visibleMap: {
  83. province: false,
  84. city: false,
  85. town: false,
  86. },
  87. }
  88. },
  89. computed: {
  90. cityList() {
  91. return this.province ? this.province.children : []
  92. },
  93. townList() {
  94. return this.city ? this.city.children : []
  95. },
  96. },
  97. mounted() {
  98. window.addEventListener('click', () => {
  99. this.visibleMap = {
  100. province: false,
  101. city: false,
  102. town: false,
  103. }
  104. })
  105. },
  106. beforeDestroy() {
  107. window.removeEventListener('click', () => {})
  108. },
  109. created() {
  110. this.initSelectValue()
  111. },
  112. methods: {
  113. async initSelectValue(data) {
  114. try {
  115. const res = await this.$http.api.fetchAllCityList()
  116. this.provinceList = res.data
  117. if (data) {
  118. this.provinceId = data.provinceId
  119. this.cityId = data.cityId
  120. this.townId = data.townId
  121. if (this.provinceId) {
  122. this.province = this.provinceList.find(
  123. (item) => item.id === this.provinceId
  124. )
  125. }
  126. if (this.cityId) {
  127. this.city = this.cityList.find((item) => item.id === this.cityId)
  128. }
  129. if (this.townId) {
  130. this.town = this.townList.find((item) => item.id === this.townId)
  131. }
  132. }
  133. } catch (error) {
  134. console.log(error)
  135. }
  136. },
  137. resetVisiable(type, flag) {
  138. for (const key in this.visibleMap) {
  139. if (type === key) {
  140. this.visibleMap[key] = flag
  141. } else {
  142. this.visibleMap[key] = false
  143. }
  144. }
  145. },
  146. toggleMenu(type) {
  147. const action = {
  148. province: this.$refs.provinceSelect,
  149. city: this.$refs.citySelect,
  150. town: this.$refs.townSelect,
  151. }
  152. const target = action[type]
  153. if (!this.visibleMap[type]) {
  154. target.visible = true
  155. this.resetVisiable(type, true)
  156. } else {
  157. target.visible = false
  158. this.resetVisiable(type, false)
  159. }
  160. },
  161. onClear(type, $event) {
  162. const action = {
  163. province: this.$refs.provinceSelect.handleClearClick,
  164. city: this.$refs.citySelect.handleClearClick,
  165. town: this.$refs.townSelect.handleClearClick,
  166. }
  167. action[type]($event)
  168. },
  169. onProvinceChange(value) {
  170. console.log(this.$refs)
  171. this.cityId = ''
  172. this.townId = ''
  173. this.city = null
  174. this.town = null
  175. this.province = this.provinceList.find((item) => item.id === value)
  176. this.onEmit()
  177. },
  178. onCityChange(value) {
  179. this.townId = ''
  180. this.town = null
  181. this.city = this.cityList.find((item) => item.id === value)
  182. this.onEmit()
  183. },
  184. onTownChange(value) {
  185. this.town = this.townList.find((item) => item.id === value)
  186. this.onEmit()
  187. },
  188. onEmit() {
  189. this.$emit('change', {
  190. provinceId: this.provinceId,
  191. cityId: this.cityId,
  192. townId: this.townId,
  193. })
  194. },
  195. },
  196. }
  197. </script>
  198. <style lang="scss" scoped>
  199. // pc 端
  200. @media screen and (min-width: 768px) {
  201. .select-group {
  202. display: flex;
  203. justify-content: center;
  204. align-items: center;
  205. margin-top: 12px;
  206. color: #282828;
  207. .select-group__item {
  208. cursor: pointer;
  209. position: relative;
  210. width: 160px;
  211. text-align: center;
  212. .info {
  213. position: absolute;
  214. left: 0;
  215. top: 0;
  216. width: 160px;
  217. z-index: 1;
  218. line-height: 40px;
  219. }
  220. .icon {
  221. color: #999;
  222. }
  223. .el-icon-circle-close {
  224. &:hover {
  225. @include themify($themes) {
  226. color: themed('color');
  227. }
  228. }
  229. }
  230. .el-select {
  231. position: absolute;
  232. left: 30px;
  233. top: 0;
  234. width: 160px;
  235. opacity: 0;
  236. }
  237. }
  238. }
  239. }
  240. // 移动 端
  241. @media screen and (max-width: 768px) {
  242. .select-group {
  243. display: flex;
  244. justify-content: flex-start;
  245. align-items: center;
  246. margin-top: 12px;
  247. color: #282828;
  248. font-size: 3.4vw;
  249. .select-group__item {
  250. cursor: pointer;
  251. position: relative;
  252. width: 20vw;
  253. text-align: center;
  254. .info {
  255. position: absolute;
  256. left: 0;
  257. top: 0;
  258. width: 20vw;
  259. z-index: 1;
  260. line-height: 40px;
  261. }
  262. .icon {
  263. color: #999;
  264. }
  265. .el-select {
  266. position: absolute;
  267. left: 0;
  268. top: 0;
  269. width: 20vw;
  270. opacity: 0;
  271. }
  272. }
  273. }
  274. }
  275. </style>